两个函数CM_Get_DevNode_Status和CM_Request_Device_Eject都在cfgmgr32.h中被定义,编译时都要Link cfgmgr32.lib,调用CM_Get_DevNode_Status可以通过,而调用
CM_Request_Device_Eject就是link不过,真是见鬼了.
出错及代码如下:
--------------------Configuration: MoveUsbDevice - Win32 Release--------------------
Compiling...
move.cpp
Linking...
move.obj : error LNK2001: unresolved external symbol __imp__CM_Request_Device_EjectA@20
Release/MoveUsbDevice.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Creating browse info file...MoveUsbDevice.exe - 2 error(s), 0 warning(s)#include <tchar.h>
#include <stdio.h>
#include <windows.h>
#include <devguid.h>
#define DWORD_PTR DWORD
#define ULONG_PTR DWORD
extern "C" {
#include "hidsdi.h"
} // 需加入hid.lib
#include <setupapi.h>
// 需加入setupapi.lib
#include <regstr.h>
#include <winbase.h>
#include <cfgmgr32.h>
// 需要加入cfgmgr32.lib
#include <initguid.h>
//#include <usbiodef.h>#pragma comment(lib,"hid.lib")
#pragma comment(lib,"setupapi.lib")
#pragma comment(lib,"cfgmgr32.lib")
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE,
0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
#define GUID_CLASS_USB_DEVICE GUID_DEVINTERFACE_USB_DEVICE
int main(int argc, _TCHAR* argv[])
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
//--------------------------------------------------------------------------
// 获取设备信息
hDevInfo = SetupDiGetClassDevs((LPGUID)&GUID_CLASS_USB_DEVICE,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE );
if (hDevInfo == INVALID_HANDLE_VALUE) {
// 查询信息失败
printf("ERROR - SetupDiGetClassDevs()");
return 1;
}
//--------------------------------------------------------------------------
// 枚举每个USB设备
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData);i++)
{
LPTSTR buffer = NULL;
PVOID buffer2 = NULL;
DWORD buffersize = 0;
ULONG len;
CONFIGRET cr;
PNP_VETO_TYPE pnpvietotype;
CHAR vetoname[MAX_PATH];
// LPWSTR vetoname=NULL;
ULONG ulStatus;
ULONG ulProblemNumber;
cr = CM_Get_DevNode_Status( &ulStatus,
&ulProblemNumber,
DeviceInfoData.DevInst,
0);
if ( CR_SUCCESS == cr ) {
printf("OK - CM_Get_DevNode_Status()[%d]\n", cr);
printf("OK - CM_Get_DevNode_Status() sts [%x]\n", ulStatus);
printf("OK - CM_Get_DevNode_Status() pro [%x]\n", ulProblemNumber);
} else {
printf("ERROR - CM_Get_DevNode_Status()[%d]\n", cr);
printf("ERROR - CM_Get_DevNode_Status()[%d]\n", GetLastError());
}
// DN_DISABLEABLE or DN_REMOVABLE
if ((DN_DISABLEABLE & ulStatus ) != 0 ) {
printf("HAS - DN_DISABLEABLE()[%x]\n", DN_DISABLEABLE & ulStatus);
} else {
continue;
}
if ((DN_REMOVABLE & ulStatus ) != 0 ) {
printf("HAS - DN_REMOVABLE()[%x]\n", DN_REMOVABLE & ulStatus);
} else {
continue;
}
len = MAX_PATH;
/* // pnpvietotype = PNP_VetoDevice;
CM_Request_Device_EjectW(
IN DEVINST dnDevInst,
OUT PPNP_VETO_TYPE pVetoType,
OUT LPWSTR pszVetoName,
IN ULONG ulNameLength,
IN ULONG ulFlags
); CMAPI CONFIGRET WINAPI
CM_Request_Device_Eject_Ex(
IN DEVINST dnDevInst,
OUT PPNP_VETO_TYPE pVetoType,
OUT LPTSTR pszVetoName,
IN ULONG ulNameLength,
IN ULONG ulFlags
IN HMACHINE hMachine
);
CMAPI CONFIGRET WINAPI
CM_Request_Device_Eject(
IN DEVINST dnDevInst,
OUT PPNP_VETO_TYPE pVetoType,
OUT LPTSTR pszVetoName,
IN ULONG ulNameLength,
IN ULONG ulFlags
);
*/
cr = CM_Request_Device_Eject( DeviceInfoData.DevInst,
&pnpvietotype,
vetoname,
len,
0
);
if ( CR_SUCCESS == cr ) {
printf("OK - CM_Request_Device_Eject()[%d]\n", cr);
} else {
printf("ERROR - CM_Request_Device_Eject()[%d]\n", cr);
printf("ERROR - CM_Request_Device_Eject()[%d]\n", GetLastError());
}
CM_Request_Device_Eject就是link不过,真是见鬼了.
出错及代码如下:
--------------------Configuration: MoveUsbDevice - Win32 Release--------------------
Compiling...
move.cpp
Linking...
move.obj : error LNK2001: unresolved external symbol __imp__CM_Request_Device_EjectA@20
Release/MoveUsbDevice.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Creating browse info file...MoveUsbDevice.exe - 2 error(s), 0 warning(s)#include <tchar.h>
#include <stdio.h>
#include <windows.h>
#include <devguid.h>
#define DWORD_PTR DWORD
#define ULONG_PTR DWORD
extern "C" {
#include "hidsdi.h"
} // 需加入hid.lib
#include <setupapi.h>
// 需加入setupapi.lib
#include <regstr.h>
#include <winbase.h>
#include <cfgmgr32.h>
// 需要加入cfgmgr32.lib
#include <initguid.h>
//#include <usbiodef.h>#pragma comment(lib,"hid.lib")
#pragma comment(lib,"setupapi.lib")
#pragma comment(lib,"cfgmgr32.lib")
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE,
0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
#define GUID_CLASS_USB_DEVICE GUID_DEVINTERFACE_USB_DEVICE
int main(int argc, _TCHAR* argv[])
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
//--------------------------------------------------------------------------
// 获取设备信息
hDevInfo = SetupDiGetClassDevs((LPGUID)&GUID_CLASS_USB_DEVICE,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE );
if (hDevInfo == INVALID_HANDLE_VALUE) {
// 查询信息失败
printf("ERROR - SetupDiGetClassDevs()");
return 1;
}
//--------------------------------------------------------------------------
// 枚举每个USB设备
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData);i++)
{
LPTSTR buffer = NULL;
PVOID buffer2 = NULL;
DWORD buffersize = 0;
ULONG len;
CONFIGRET cr;
PNP_VETO_TYPE pnpvietotype;
CHAR vetoname[MAX_PATH];
// LPWSTR vetoname=NULL;
ULONG ulStatus;
ULONG ulProblemNumber;
cr = CM_Get_DevNode_Status( &ulStatus,
&ulProblemNumber,
DeviceInfoData.DevInst,
0);
if ( CR_SUCCESS == cr ) {
printf("OK - CM_Get_DevNode_Status()[%d]\n", cr);
printf("OK - CM_Get_DevNode_Status() sts [%x]\n", ulStatus);
printf("OK - CM_Get_DevNode_Status() pro [%x]\n", ulProblemNumber);
} else {
printf("ERROR - CM_Get_DevNode_Status()[%d]\n", cr);
printf("ERROR - CM_Get_DevNode_Status()[%d]\n", GetLastError());
}
// DN_DISABLEABLE or DN_REMOVABLE
if ((DN_DISABLEABLE & ulStatus ) != 0 ) {
printf("HAS - DN_DISABLEABLE()[%x]\n", DN_DISABLEABLE & ulStatus);
} else {
continue;
}
if ((DN_REMOVABLE & ulStatus ) != 0 ) {
printf("HAS - DN_REMOVABLE()[%x]\n", DN_REMOVABLE & ulStatus);
} else {
continue;
}
len = MAX_PATH;
/* // pnpvietotype = PNP_VetoDevice;
CM_Request_Device_EjectW(
IN DEVINST dnDevInst,
OUT PPNP_VETO_TYPE pVetoType,
OUT LPWSTR pszVetoName,
IN ULONG ulNameLength,
IN ULONG ulFlags
); CMAPI CONFIGRET WINAPI
CM_Request_Device_Eject_Ex(
IN DEVINST dnDevInst,
OUT PPNP_VETO_TYPE pVetoType,
OUT LPTSTR pszVetoName,
IN ULONG ulNameLength,
IN ULONG ulFlags
IN HMACHINE hMachine
);
CMAPI CONFIGRET WINAPI
CM_Request_Device_Eject(
IN DEVINST dnDevInst,
OUT PPNP_VETO_TYPE pVetoType,
OUT LPTSTR pszVetoName,
IN ULONG ulNameLength,
IN ULONG ulFlags
);
*/
cr = CM_Request_Device_Eject( DeviceInfoData.DevInst,
&pnpvietotype,
vetoname,
len,
0
);
if ( CR_SUCCESS == cr ) {
printf("OK - CM_Request_Device_Eject()[%d]\n", cr);
} else {
printf("ERROR - CM_Request_Device_Eject()[%d]\n", cr);
printf("ERROR - CM_Request_Device_Eject()[%d]\n", GetLastError());
}
#pragma comment(lib,"hid.lib")
#pragma comment(lib,"setupapi.lib")
#pragma comment(lib,"cfgmgr32.lib")
真不知道为什么啊?
大侠麻烦再帮看看,行吗?
pfnHotPlugEjectDevice HotPlugEjectDevice = NULL; HMODULE hHotPlug = LoadLibrary("HotPlug.dll");
if(hHotPlug == NULL)
{
printf("LoadLibrary(HotPlug.dll) Error:%d\n", GetLastError());
return FALSE;
}
//get functions
HotPlugEjectDevice = (pfnHotPlugEjectDevice)GetProcAddress(hHotPlug, "HotPlugEjectDevice");
BOOL bResult = HotPlugEjectDevice(NULL, "USB\\Vid_0d7d&Pid_0150\\4D3B190303C0");
的确是版本的问题,我将路径改为winxp ddk的路径就可以了,vc目录下的不行.
不知它们有什么区别呢?
所以你在release出去的时候,要连setupapi.dll一起发出去,不然会找不到接口