reanchear
驱动小牛
驱动小牛
  • 注册日期2004-06-17
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分40分
  • 威望253点
  • 贡献值0点
  • 好评度102点
  • 原创分0分
  • 专家分0分
阅读:2221回复:0

急救驱动安装包制作

楼主#
更多 发布于:2007-04-25 17:12
  请大家给我一些指点啊:

我现在写了一驱动安装包程序,但是有个很麻烦的问题一直不能解决
当我插上设备后,安装驱动OK,但是当我在不插上USB设备时安装驱动的时候,到快了安装完成的时候就重起了,请问是怎么会事啊?

具体来讲就是在调用UpdateAveoUsbCamDriver函数时蓝屏重起了

请大家帮忙看看

谢谢

dll代码如下:


BOOL RecordError(TCHAR* ErrorTitle)
{
#ifdef    _DEBUG
    
    DWORD Err = GetLastError();
    FILE* f = fopen("C:\\MFC_InstDrvDLL_DEBUG.txt","a");
    TCHAR sTime[9];        // to record current time
    _strtime(sTime);
    fprintf(f,TEXT("\r\n%s:\r\n"),sTime);

    //    simply record "ErrorTitle" while no err at all
    if(Err==NO_ERROR) {
        fprintf(f,TEXT("%s FAILURE\r\n"),ErrorTitle);
        goto MissionDone;
    }
    
    {
        LPVOID lpMessageBuffer = NULL;
        if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM
            ,NULL,Err,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT)
            ,(LPTSTR)&lpMessageBuffer,0,NULL ))
            fprintf(f,TEXT("%s FAILURE: %s\r\n"),ErrorTitle,(TCHAR *)lpMessageBuffer);
        else
            fprintf(f,TEXT("%s FAILURE: (0x%08x)\r\n"),ErrorTitle,Err);
        
        if (lpMessageBuffer)
            LocalFree(lpMessageBuffer); // Free system buffer
    }
MissionDone:
    fclose(f);
    SetLastError(Err);
    
#endif    //    _DEBUG

    return FALSE;
}

/*        
 *            FindExistingDevice()
 *    purpose:
 *        to see whether specified device is currently existing
 *        , that is, being plugged or not
 *
 *    paramters:
 *    LPTSTR HardwareId        hw-id of specified device
 *
 *    return:
 *        TRUE if it's existing, FALSE otherwise
 */
BOOL FindExistingDevice(IN LPCSTR HardwareId)
{
    HDEVINFO DeviceInfoSet;
    SP_DEVINFO_DATA DeviceInfoData;
    DWORD i,err;
    BOOL Found;

    // Create a Device Information Set with all present devices.
    DeviceInfoSet = SetupDiGetClassDevs(NULL,0,0,DIGCF_ALLCLASSES|DIGCF_PRESENT); // All devices present on system

    if (DeviceInfoSet == INVALID_HANDLE_VALUE)
        return RecordError(TEXT("FindExistingDevice()    GetClassDevs(All Present Devices)"));

    //  Enumerate through all Devices.
    _tprintf(TEXT("FindExistingDevice()    Search for Device ID: [%s]\n"),HardwareId);
    Found = FALSE;
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    for (i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++) {
        DWORD DataT;
        LPTSTR p,buffer = NULL;
        DWORD buffersize = 0;

        // We won't know the size of the HardwareID buffer until we call
        // this function. So call it with a null to begin with, and then
        // use the required buffer size to Alloc the nessicary space.
        // Keep calling we have success or an unknown failure.
        while (!SetupDiGetDeviceRegistryProperty(
            DeviceInfoSet,&DeviceInfoData,SPDRP_HARDWAREID
            ,&DataT,(PBYTE)buffer,buffersize,&buffersize)) {
            if (GetLastError()==ERROR_INVALID_DATA)
                break;
            else if(GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
                // We need to change the buffer size.
                if (buffer)
                    LocalFree(buffer);
                buffer = (char *)LocalAlloc(LPTR,buffersize);
            } else {
                // Unknown Failure.
                RecordError(TEXT("FindExistingDevice()    GetDeviceRegistryProperty"));
                goto cleanup_DeviceInfo;
            }
        }

        if (GetLastError() == ERROR_INVALID_DATA)
            continue;

        // Compare each entry in the buffer multi-sz list with our HardwareID.
        for (p=buffer;*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR)) {
            if (0==_stricmp(HardwareId,p)) {
                Found = TRUE;
                break;
            }
        }

        if (buffer) LocalFree(buffer);
        if (Found) break;
    }

    if (GetLastError()!=NO_ERROR)
        RecordError(TEXT("FindExistingDevice()    EnumDeviceInfo"));

    //  Cleanup.
cleanup_DeviceInfo:
    err = GetLastError();
    SetupDiDestroyDeviceInfoList(DeviceInfoSet);
    SetLastError(err);

    return err==NO_ERROR;
}

BOOL InstallRootEnumeratedDriver(IN LPTSTR HardwareId,IN LPTSTR INFFile
                                 ,OUT PBOOL RebootRequired OPTIONAL)
{
    HDEVINFO DeviceInfoSet = 0;
    SP_DEVINFO_DATA DeviceInfoData;
    GUID ClassGUID;
    TCHAR ClassName[32];
    DWORD err;

    // Use the INF File to extract the Class GUID.
    if(!SetupDiGetINFClass(INFFile,&ClassGUID,ClassName,sizeof(ClassName),0))
        return RecordError(TEXT("GetINFClass"));

    // Create the container for the to-be-created Device Information Element.
    DeviceInfoSet = SetupDiCreateDeviceInfoList(&ClassGUID,0);
    if(DeviceInfoSet == INVALID_HANDLE_VALUE)
        return RecordError(TEXT("CreateDeviceInfoList"));

    // Now create the element.
    // Use the Class GUID and Name from the INF file.
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    if (!SetupDiCreateDeviceInfo(DeviceInfoSet,ClassName,&ClassGUID
        ,NULL,0,DICD_GENERATE_ID,&DeviceInfoData)) {    //    DICD_INHERIT_CLASSDRVS
        RecordError(TEXT("CreateDeviceInfo"));
        goto cleanup_DeviceInfo;
    }

    // Add the HardwareID to the Device's HardwareID property.
    if(!SetupDiSetDeviceRegistryProperty(DeviceInfoSet,&DeviceInfoData,SPDRP_HARDWAREID
        ,(LPBYTE)HardwareId,(lstrlen(HardwareId)+1+1)*sizeof(TCHAR))) {
        RecordError(TEXT("SetDeviceRegistryProperty"));
        goto cleanup_DeviceInfo;
    }

    // Transform the registry element into an actual devnode
    // in the PnP HW tree.
    if (!SetupDiCallClassInstaller(DIF_REGISTERDEVICE,DeviceInfoSet,&DeviceInfoData)) {
        RecordError(TEXT("CallClassInstaller(REGISTERDEVICE)"));
        goto cleanup_DeviceInfo;
    }

    // The element is now registered. We must explicitly remove the
    // device using DIF_REMOVE, if we encounter any failure from now on.

    // Install the Driver.
    if (!UpdateDriverForPlugAndPlayDevices(0,HardwareId,INFFile
        ,INSTALLFLAG_FORCE,RebootRequired)) {
        DWORD err = GetLastError();
        RecordError(TEXT("UpdateDriverForPlugAndPlayDevices"));

        if (!SetupDiCallClassInstaller(DIF_REMOVE,DeviceInfoSet,&DeviceInfoData))
            RecordError(TEXT("CallClassInstaller(REMOVE)"));
        SetLastError(err);
    }
    return TRUE;

    //  Cleanup.
cleanup_DeviceInfo:
    err = GetLastError();
    SetupDiDestroyDeviceInfoList(DeviceInfoSet);
    SetLastError(err);

    return err == NO_ERROR;
}


CONST LPTSTR g_csCamId[] = {
    _T("USB\\Vid_0547&Pid_2102")
    ,_T("USB\\Vid_1459&Pid_88c6")
    ,_T("USB\\Vid_1459&Pid_88c7")
    ,_T("USB\\Vid_1459&Pid_88c2")
    ,_T("USB\\Vid_1459&Pid_88cb")
};

CONST INT g_nCamIdCnt = sizeof(g_csCamId)/sizeof(LPTSTR);

UpdateAveoUsbCamDriver(LPSTR sInfPath,INT nHwIdx)
{
    //if(nHwIdx<0||nHwIdx>=g_nCamIdCnt)
    //    return UD_F_UnkwnHwIdx;

    WIN32_FIND_DATA FindFileData;
    BOOL RebootRequired = 0; // Must be cleared.
    _TCHAR *FName, *HWID;
    FName = sInfPath;
    HWID = g_csCamId[nHwIdx];

    if(FindFirstFile(FName,&FindFileData)==INVALID_HANDLE_VALUE)
        return UD_F_InfUnfound;    //    inf file unfound  

    if (FindExistingDevice(HWID)) {
        //    whether it's USB composite device or not, we just
        //    "update" it to what we exactly need -- image device
        if (!UpdateDriverForPlugAndPlayDevices(0,HWID,FName
            ,INSTALLFLAG_FORCE,&RebootRequired)) {
            RecordError(TEXT("UpdateDriverForPlugAndPlayDevices"));
            return UD_F_UpdateDrv;
        }
    }
    else
    {
        if (GetLastError()!= ERROR_NO_MORE_ITEMS)
            return UD_F_FindExist;

        /*if(!(SetupCopyOEMInf(FName,NULL,SPOST_NONE,0,NULL,MAX_PATH,NULL,NULL)))          
            return UD_F_CpyOemInf;
        if (!InstallRootEnumeratedDriver(HWID,FName,&RebootRequired))
            return UD_F_InstRootEnumDrv;
*/        
        //    while the device is unplugged at the time, we first
        //    kill the potentially existed traces of USB composite
        //    device (in fact, some certain regedit entries under
        //    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB)
        _TCHAR HWexID[MAX_PATH];
        strcpy(HWexID,HWID);
        strcat(HWexID,"&Mi_00");
        RemoveDriver(HWexID);
        
        strcpy(HWexID,HWID);
        strcat(HWexID,"&Mi_01");
        RemoveDriver(HWexID);
        RemoveDriver(HWID);
        
        //    then assume our driver has passed the Windows Logo test
        //    already, we copy inf to oemXX.inf
        //    as a result of that, while the device is being plugged,
        //    New-Hareware-Found rountine (Windows Call) will directly
        //    use this inf update the device.
        //
        //    (say, we guess this is the perfect solution, but......)
        if(!(SetupCopyOEMInf(FName,NULL,SPOST_NONE,0,NULL,MAX_PATH,NULL,NULL)))          
            return UD_F_CpyOemInf;


        // Driver Does not exist, Create and call the API.
        if (!InstallRootEnumeratedDriver(HWID,FName,&RebootRequired))
            return UD_F_InstRootEnumDrv;

        //return UD_F_DvcUnfound;
    }

    return UD_S; // Install Success, no reboot required.    
}
游客

返回顶部