dxmxxh
驱动牛犊
驱动牛犊
  • 注册日期2002-05-29
  • 最后登录2005-08-08
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:3163回复:4

急,用installshield做modem的驱动的安装程序

楼主#
更多 发布于:2004-12-14 14:15
要求用installshield自动安装modem。用ddk自带的install编译后运行出错:重叠I/0操作进行中。
哪位大侠能指点一下吗?

最新喜欢:

force_eagleforce_...
lxxmmd
驱动牛犊
驱动牛犊
  • 注册日期2004-04-14
  • 最后登录2005-02-02
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-12-14 17:00
下面是我修改的dll的代码,经测试可以用。
// ModIns.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
)
{
      switch(ul_reason_for_call)
 {

      case DLL_PROCESS_ATTACH:
     break;

      case DLL_PROCESS_DETACH:
         // Perform any DLL cleanup here
         break;

 }
 return TRUE;

}


#define MAX_CLASS_NAME_LEN 32
#define MAX__DESC 256

void DisplayError(TCHAR * ErrorName)
/*++

Routine Description:

    Formats and displays erros in a readable format.

Arguments:

    ErrorName - Supplies a string containing any additional comments to be printed.

Return Value:

    None.
--*/
{
    return;
}

BOOL PortExists(TCHAR* szPortName)
/*++

Routine Description:

    This routine checks if the port name supplied is a valid one or not

Arguments:

    szPortName - Supplies a string containing a port name (like COM1, COM2)

Return Value:

    The function returns TRUE if it finds a match for port name,
otherwise it returns a FALSE.

--*/
{
TCHAR szDevDesc[MAX__DESC];
BOOL match = FALSE;
// GUID const CLASS_GUID = GUID_DEVCLASS_PORTS;
GUID const CLASS_GUID = {0x4d36e978L, 0xe325, 0x11ce, {0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}};

HDEVINFO hDevInfo;

hDevInfo = SetupDiGetClassDevs((LPGUID)&CLASS_GUID, NULL, NULL, DIGCF_PRESENT);

if ( INVALID_HANDLE_VALUE != hDevInfo)
{
SP_DEVINFO_DATA  devInfoElem;
int index = 0;
devInfoElem.cbSize = sizeof(SP_DEVINFO_DATA);

while ( SetupDiEnumDeviceInfo(hDevInfo, index++, &devInfoElem))
{
HKEY hKeyDev = SetupDiOpenDevRegKey(hDevInfo, &devInfoElem, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);
if (INVALID_HANDLE_VALUE != hKeyDev) {
WORD length = sizeof(szDevDesc);
if(ERROR_SUCCESS == RegQueryValueEx(hKeyDev, _T("PortName"), NULL, NULL, (unsigned char *)szDevDesc, (LPDWORD)&length) ) {
RegCloseKey(hKeyDev);
//printf("\t\tComparing :  %s\twith\t%s\n", szPortName, szDevDesc);
if (! _tcsicmp(szPortName, szDevDesc)) {
match = TRUE;
break;
}
}
}
} // while
SetupDiDestroyDeviceInfoList(hDevInfo);
}
return match;
}

BOOL
RegisterModem(
    IN  HDEVINFO          hdi,
    IN  PSP_DEVINFO_DATA  pdevData,
    IN  LPCTSTR           pszPort)
/*++

Routine Description:

    This routine registers a modem device and writes "AttachedTo" string to registry
-- this value represents the Port to which Modem is attached.

Arguments:

hdi -- Device Info Set -- containing the modem Device Info element.
    pdevData -- Pointer the the modem device information element.
    szPortName -- Supplies a string containing port name the modem is to be attached to (like COM1, COM2).

Return Value:

    The function returns TRUE if it was able to register the modem and write the "AttachedTo" string to registry,
otherwise it returns a FALSE.

Note : Once the device is registered using SetupDiRegisterDeviceInfo(...), we need to remove
it explicitly in case of an error here after -- may be call SetupDiCallClassInstaller(...) with a DIF_REMOVE function.

--*/
{
BOOL bRet;
SP_DRVINFO_DATA drvData;
DWORD nErr = NO_ERROR;
DWORD dwRet;
TCHAR const c_szAttachedTo[] = _T("AttachedTo");
HKEY hKeyDev;
bRet = FALSE;

if( !SetupDiRegisterDeviceInfo(hdi, pdevData, 0, NULL, NULL, NULL) )
{
DisplayError("Register Device 1\n");
return bRet;
}

hKeyDev = SetupDiOpenDevRegKey(hdi, pdevData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_ALL_ACCESS); //// This call fails....

if( (INVALID_HANDLE_VALUE == hKeyDev) && ( ERROR_KEY_DOES_NOT_EXIST == GetLastError()) )
{
hKeyDev = SetupDiCreateDevRegKey(hdi, pdevData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, NULL, NULL);

if( INVALID_HANDLE_VALUE == hKeyDev )
{
DisplayError(_T("SetupDi Open+Create DevRegKey failed: "));
return FALSE;
}
}
else {
DisplayError(_T("Can not open DriverKey: "));
return FALSE;
}

    if (ERROR_SUCCESS != (dwRet = RegSetValueEx (hKeyDev, c_szAttachedTo, 0, REG_SZ,
(PBYTE)pszPort, (lstrlen(pszPort)+1)*sizeof(TCHAR))))
{
DisplayError(_T("RegSetValueEx :"));

            SetLastError (dwRet);
            bRet = FALSE;
}
RegCloseKey (hKeyDev);


if( !SetupDiRegisterDeviceInfo(hdi, pdevData, 0, NULL, NULL, NULL) )
    {
DisplayError(_T("Register Device 2"));
        return FALSE;
}

if ( !SetupDiGetSelectedDriver(hdi, pdevData, &drvData)) {

if((GetLastError()!=NO_ERROR)&&(GetLastError()!=ERROR_NO_DRIVER_SELECTED))
DisplayError(_T("SetupDiGetSelectedDriver Failed: "));
else
SetLastError(NO_ERROR);
}
return TRUE;
}

BOOL FindExistingDevice(IN LPTSTR HardwareId)
/*++

Routine Description:

    This routine finds an existing devnode if present.

Arguments:

    HardwareIdList - Supplies a string containing a hardware ID to
    be associated with the device.

Return Value:

    The function returns TRUE if it find a devnode with the HardwareID

    Otherwise it returns FALSE and the logged error can be retrieved
    with a call to GetLastError.

    The most common error will be ERROR_NO_MORE_ITEMS, which means the
    function could not find a devnode with the HardwareID.

--*/
{
    HDEVINFO DeviceInfoSet;
    SP_DEVINFO_DATA DeviceInfoData;
    DWORD i,err;
    BOOL Found;

//
// Create a Device Information Set with all present devices.
//

DeviceInfoSet = SetupDiGetClassDevs(NULL, // All Classes
       0,
      0,
       DIGCF_ALLCLASSES | DIGCF_PRESENT ); // All devices present on system

if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
DisplayError(_T("GetClassDevs(All Present Devices)"))  ;
return FALSE;
}

//_tprintf(_T("\n\nSearch for Device ID: [%s]\n\n"),HardwareId);

//
//  Enumerate through all Devices.
//

Found = FALSE;
i = err = 0;

DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

//for (i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++)
while ( !Found && SetupDiEnumDeviceInfo(DeviceInfoSet, i++, &DeviceInfoData))
{
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) {
//
// May be a Legacy Device with no HardwareID. Continue.
//
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.
      //
DisplayError(_T("GetDeviceRegistryProperty"));
err = 1;
break;
}
}
   
if (GetLastError() == ERROR_INVALID_DATA)
   continue;

if( err) break;
   
   //
   // Compare each entry in the buffer multi-sz list with our HardwareID.
   //
for (p=buffer;*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR))
{
//_tprintf(_T("Comparing device ID: [%s]\n"),p);

if (!_tcscmp(HardwareId,p))
{
//_tprintf(_T("Found! [%s]\n"),p);
Found = TRUE;
break;
}
}

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

if ((GetLastError() != NO_ERROR)&&(GetLastError() !=ERROR_NO_MORE_ITEMS))
{
DisplayError(_T("EnumDeviceInfo"));
}



    //
    //  Cleanup.
    //

    err = GetLastError();
    SetupDiDestroyDeviceInfoList(DeviceInfoSet);
    SetLastError(err);

    return err == NO_ERROR;
}


BOOL
InstallRootEnumeratedDriver(IN  LPTSTR HardwareId,
    IN  LPTSTR INFFile,
    IN LPTSTR MdmPort,
    OUT PBOOL  RebootRequired  OPTIONAL
    )
/*++

Routine Description:

    This routine creates and installs a new root-enumerated devnode.

Arguments:

    HardwareIdList - Supplies a multi-sz list containing one or more hardware
    IDs to be associated with the device.  These are necessary in order
    to match up with an INF driver node when we go to do the device
    installation.

    InfFile - Supplies the full path to the INF File to be used when
    installing this device.

    RebootRequired - Optionally, supplies the address of a boolean that is
    set, upon successful return, to indicate whether or not a reboot is
    required to bring the newly-installed device on-line.

Return Value:

    The function returns TRUE if it is successful.

    Otherwise it returns FALSE and the logged error can be retrieved
    with a call to GetLastError.

--*/
{
    HDEVINFO DeviceInfoSet = 0;
    SP_DEVINFO_DATA DeviceInfoData;
    GUID ClassGUID;
    TCHAR ClassName[MAX_CLASS_NAME_LEN];
    DWORD err;
//HKEY hKeyDev;
BOOL Remove = FALSE;

    SetLastError(NO_ERROR);
//
    // Use the INF File to extract the Class GUID.
    //

    if (!SetupDiGetINFClass(INFFile,&ClassGUID,ClassName,sizeof(ClassName),0))
    {
        DisplayError(_T("GetINFClass"));
return FALSE;
    }
    
    //
    // Create the container for the to-be-created Device Information Element.
    //
    DeviceInfoSet = SetupDiCreateDeviceInfoList(&ClassGUID,0);
    if(DeviceInfoSet == INVALID_HANDLE_VALUE)
    {
        DisplayError(_T("CreateDeviceInfoList"));
return FALSE;
    }


do {
//
// 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))
{
DisplayError(_T("CreateDeviceInfo"));
break;
}

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

if ( !RegisterModem(DeviceInfoSet, &DeviceInfoData, MdmPort) )
{
DisplayError(_T("RegisterModem"));
Remove = TRUE;
break;
}

//
// Install the Driver.
//
if (!UpdateDriverForPlugAndPlayDevices(0,
HardwareId,
INFFile,
INSTALLFLAG_FORCE,
RebootRequired))
{
DisplayError(_T("UpdateDriverForPlugAndPlayDevices"));
Remove = TRUE;
break;
}

} while (FALSE);

if(Remove) {

//Delete Device Instance that was registered using SetupDiRegisterDeviceInfo
// May through an error if Device not registered -- who cares??

SetupDiCallClassInstaller(
           DIF_REMOVE,
           DeviceInfoSet,
           &DeviceInfoData);
DisplayError(_T("Remove"));
}

    //
    //  Cleanup.
    //    

    err = GetLastError();

if(err==ERROR_NO_MORE_ITEMS)
err=NO_ERROR;
    SetupDiDestroyDeviceInfoList(DeviceInfoSet);
    SetLastError(err);
    
    return (err == NO_ERROR && !Remove);
}

extern "C" __declspec(dllexport) int InstallModem(_TCHAR* INFFILENAME,_TCHAR* PORTNAME,_TCHAR* HardwareID)
{
WIN32_FIND_DATA FindFileData;
    BOOL RebootRequired = 0;


if (FindFirstFile(INFFILENAME,&FindFileData)==INVALID_HANDLE_VALUE)    
{

        return 2; // Install Failure
    }
if (FindExistingDevice(HardwareID))
{
        //
        // No Need to Create a Device Node, just update the current devnode.
        //
        if (!UpdateDriverForPlugAndPlayDevices(0, // No Window Handle
            HardwareID, // Hardware ID
            INFFILENAME, // FileName
            INSTALLFLAG_FORCE,
            &RebootRequired))
        {
            DisplayError(_T("UpdateDriverForPlugAndPlayDevices"));
            return 2; // Install Failure
        }
    }
else
    {
        if (GetLastError()!= ERROR_NO_MORE_ITEMS) {
            //
            // An unknown failure from FindExistingDevice()
            //
            return 2; // Install Failure
        }
else
SetLastError(NO_ERROR);

        //
        // Driver Does not exist, Create and call the API.
        // HardwareID must be a multi-sz string, which argv[2] is.
        //

//test

   if (!InstallRootEnumeratedDriver(HardwareID, // HardwareID
           INFFILENAME, // FileName
           PORTNAME, // Modem Port
           &RebootRequired))
   {
           return GetLastError();//3; // Install Failure
   }
    }

    //_tprintf(_T("Driver Installed successfully.\n"));

    if (RebootRequired) {
        //_tprintf(_T("(Reboot Required)\n"));
        return 1; // Install Success, reboot required.
    }

    return 0; // Install Success, no reboot required.
}
dxmxxh
驱动牛犊
驱动牛犊
  • 注册日期2002-05-29
  • 最后登录2005-08-08
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-12-15 17:30
谢谢大侠,结贴,给分!
HopeCao
驱动牛犊
驱动牛犊
  • 注册日期2004-12-14
  • 最后登录2010-03-09
  • 粉丝0
  • 关注0
  • 积分259分
  • 威望152点
  • 贡献值0点
  • 好评度44点
  • 原创分0分
  • 专家分0分
地板#
发布于:2004-12-16 23:16
我的modem设备是一个USB的虚拟modem,我安装时要怎么样指定串口号?
HopeCao
zhoujiamurong
驱动小牛
驱动小牛
  • 注册日期2006-03-20
  • 最后登录2009-05-06
  • 粉丝4
  • 关注0
  • 积分1081分
  • 威望360点
  • 贡献值0点
  • 好评度215点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2007-02-08 16:07
yufguj
游客

返回顶部