Помогите с софтиной, должна устанавливать модем в систему. Вот код
// ModemInstall.cpp : Defines the entry point for the console application.
//
/*
Copyright (c) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Created By : Rohit Raina
Creation Date : May 27, 2001
*/
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <newdev.h>
#include <devguid.h>
#include <setupapi.h>
#include <cfgmgr32.h>
#include <windef.h>
#include <stdlib.h>
#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.
--*/
{
DWORD Err = GetLastError();
LPVOID lpMessageBuffer = NULL;
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
Err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMessageBuffer,
0,
NULL ))
_tprintf(_T("%s FAILURE: %s\n"),ErrorName,(TCHAR *)lpMessageBuffer);
else
_tprintf(_T("%s FAILURE: (0x%08x)\n"),ErrorName,Err);
if (lpMessageBuffer) LocalFree( lpMessageBuffer ); // Free system buffer
SetLastError(Err);
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)) {
DisplayError(_T("SetupDiGetSelectedDriver Failed: "));
}
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 is successful.
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)
{
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;
//
// 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.
//
long ll;
if(!SetupDiSetDeviceRegistryProperty(DeviceInfoSet,
&DeviceInfoData,
SPDRP_HARDWAREID,
(LPBYTE)HardwareId,
lstrlen(HardwareId)+1+1)*sizeof(TCHAR))
{
ll = GetLastError();
DisplayError(_T("SetDeviceRegistryProperty"));
break;
}
if(!RegisterModem(DeviceInfoSet, &DeviceInfoData, MdmPort))
{
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);
}
//
// Cleanup.
//
err = GetLastError();
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
SetLastError(err);
return (err == NO_ERROR && !Remove);
}
int main(int argc, char **argv)
/*++
Routine Discription:
Entry point to install.exe.
Parse the command line, call subroutines.
Arguments:
Standard console 'c' application arguments.
argv[1] - Full path of INF file.
argv[2] - Port
argv[3] - PnP HardwareID of device.
Return Value:
Standard Console ERRORLEVEL values:
0 - Install Successfull, no reboot required.
1 - Install Successfull, reboot required.
2 - Install Failure.
--*/
{
WIN32_FIND_DATA FindFileData;
BOOL RebootRequired = 0; // Must be cleared.
//
// Verify the Arguments.
//
if (argc != 4)
{
_tprintf("usage: %s <INF_File> COMP <Hardware_ID>\n", argv[0]);
return 2; // Install Failure
}
if(!PortExists(argv[2])) {
_tprintf(_T("\nInvalid Port... exiting"));
return 2; // Invalid Port supplied
}
if (FindFirstFile(argv[1],&FindFileData)==INVALID_HANDLE_VALUE) {
_tprintf(_T(" File not found.\n"));
_tprintf(_T("usage: install <INF_File> COMP <Hardware_ID>\n"));
return 2; // Install Failure
}
//InstallNewDevice(argc, argv);
//
// if this device allready exists
//
if (FindExistingDevice(argv[3])) {
//
// No Need to Create a Device Node, just update the current devnode.
//
if (!UpdateDriverForPlugAndPlayDevices(0, // No Window Handle
argv[3], // Hardware ID
argv[1], // 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
}
//
// Driver Does not exist, Create and call the API.
// HardwareID must be a multi-sz string, which argv[2] is.
//
if (!InstallRootEnumeratedDriver(argv[3], // HardwareID
argv[1], // FileName
argv[2], // Modem Port
&RebootRequired))
{
return 2; // 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.
}
Запускаю с параметрами "mc35_gprs.inf" "com1" "sia0002". Выдает ошибку при вызове SetupDiSetDeviceRegistryProperty в процедуре InstallRootEnumeratedDriver. Говорит ERROR_INVALID_PARAMETER, видимо ругается на HardwareId. Скажите, где ошибка?