CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2002
    Location
    Israel
    Posts
    396

    Uninstall USB device drivers using API

    Hi,
    I use SetupCopyOEMInfA in my Wise installer to install a scanner device that works with USB connection and it works fine.
    These days i have new drivers for this device and i want to make sure that the old devices will be replaced so in order to do that i want to uninstall the current driver and its files and then use my installer to install the new driver.
    I try to use SetupUninstallOEMInfA but it not do the job and all driver files are in place (The API return 0 value)

    Any one have an idea how can i do it automatically without telling the user to do it by him self from the device manager?

    Thanks.

  2. #2
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Uninstall USB device drivers using API

    If you are doing this from an uninstall:

    Code:
    
    UINT _stdcall UninstallINF (MSIHANDLE hInstall)
    {
        WCHAR wszPath[MAX_PATH];
        DWORD dwPath = _countof(wszPath);
        UINT gp = MsiGetPropertyW(hInstall, L"CustomActionData", wszPath, &dwPath);
        WCHAR wszOutInf[MAX_PATH];
        dwPath = _countof(wszOutInf);
        ZeroMemory(wszOutInf, _countof(wszOutInf)*sizeof(WCHAR));
        WCHAR wszProductCode[64];
        dwPath = _countof(wszProductCode);
        gp = MsiGetPropertyW(hInstall, L"ProductCode", wszProductCode, &dwPath);
        WCHAR wszRegistryUninstallKey[1024];
        StringCchPrintf(wszRegistryUninstallKey, _countof(wszRegistryUninstallKey), L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%s", wszProductCode);
        HKEY hKeyFolder;
        DWORD dwDisp;
        if( ::RegCreateKeyEx(HKEY_LOCAL_MACHINE, wszRegistryUninstallKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE, NULL, &hKeyFolder, &dwDisp) == ERROR_SUCCESS )
        {
            DWORD dwType = REG_EXPAND_SZ;
            DWORD dwSize = _countof(wszOutInf);
            ::RegQueryValueEx(hKeyFolder, L"DestinationINF", 0, &dwType, (BYTE *)wszOutInf, &dwSize);
            ::RegCloseKey(hKeyFolder);
        }
        if( wszOutInf[0] )
        {
            //MessageBox(NULL, wszOutInf, L"OutINF", MB_OK);
            DeleteFile(wszOutInf);
            LPWSTR lpPtr = wszOutInf;
            while( *lpPtr && !((*(lpPtr) == 'I' || *(lpPtr) == 'i') && (*(lpPtr+1) == 'N' || *(lpPtr+1) == 'n') && (*(lpPtr+2) == 'F' || *(lpPtr+2) == 'f') && !*(lpPtr+3)) ) ++lpPtr;
            if( *lpPtr )
            {
                *(lpPtr) = 'c';
                *(lpPtr+1) = 'a';
                *(lpPtr+2) = 't';
                DeleteFile(wszOutInf);
            }
        }
    //else
            // MessageBox(NULL, L"No Out INF defined for removal", L"OutINF", MB_OK);
        return 0;
    }
     
    

  3. #3
    Join Date
    Sep 2002
    Location
    Israel
    Posts
    396

    Re: Uninstall USB device drivers using API

    ?
    What is this?

  4. #4
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Uninstall USB device drivers using API

    Well, read it.

    The INF filename is saved in the registry upon installation. Delete it to remove it. It is not necessarity the same as the original installation filename. It may be renamed to OEMXXXX.INF. You can also pass the name to SetupUninstallOEMInf if you want.

  5. #5
    Join Date
    Sep 2002
    Location
    Israel
    Posts
    396

    Re: Uninstall USB device drivers using API

    Thanks,
    I now understand but i am more a VB\VB.net developer but i want to do that from my installer and not by using external application that will do it since then i will have to install the .Net freamworks to make it work and i dont want to do that.
    Is there a way to just search for the right inf in the registry and then just get the inf file?

  6. #6
    Join Date
    Sep 2002
    Location
    Israel
    Posts
    396

    Re: Uninstall USB device drivers using API

    Another issue is that even if i to call SetupUninstallOEMInf and give it the oemXXX.inf it report as success and actually delete the inf file.
    I can then delete the driver files manually from the Systems32\Drivers folder and so far so good.
    But this is not the same as doing uninstall driver from the device manager since i can still see it in the list.
    Is there other action that i missing?

  7. #7
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Uninstall USB device drivers using API

    You are missing the concept of what those functions really are. They add/remove a driver from the installable driver list, they don't actually install/uninstall a driver.

    What you really want is the MSI API.

    http://msdn.microsoft.com/en-us/libr...26(VS.85).aspx

    -Erik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured