我正在尝试使用 C 枚举连接的设备,并在 Windows 10 上使用 VS 2015 编写代码。
我的头文件如下所示:
#pragma once
#include <PortableDeviceApi.h>
#include <stdio.h>
#include <objbase.h>
/*
Asks the user what registry key he would like to get and then output the key to a file.
@param - None
@return - None
*/
void showDevices();
我的源代码如下所示:
#include "DeviceHandler.h"
void showDevices()
{
IPortableDeviceManager *deviceManager;
HRESULT result;
IID const *interfaceId = &IID_IPortableDeviceManager;
CLSID const *classId = &CLSID_PortableDeviceManager;
deviceManager = malloc(sizeof(IPortableDeviceManager));
result = CoCreateInstance(classId, NULL, CLSCTX_INPROC_SERVER, interfaceId, &deviceManager);
if (result != S_OK)
{
wprintf(L"%s\n", L"Class failed to be created");
}
free(deviceManager);
return;
}//end showDevices
我收到 IID_IPortableDeviceManager 和 CLSID_PortableDeviceManager 的链接错误。
我查看了包含的 PortableDeviceApi.h 文件,这两个变量定义为:
EXTERN_C const CLSID CLSID_PortableDeviceManager;
EXTERN_C const IID IID_IPortableDeviceManager;
我需要做什么才能让链接器识别这些外部变量?
您需要与
PortableDeviceGuids.lib
链接,它包括代码中引用的实际 GUID。
奇怪,但我无法在 Microsoft 文档中找到有关此内容的提及,不得不通过谷歌搜索并来到这里。尽管@hans-passant有正确的评论,但我看不到任何正确的答案,所以我认为发布正式答案是正确的。