1

I am attempting to enumerate connected devices using C and writing the code with VS 2015 on Windows 10.

My header file looks like this:

#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();

My source code looks like this:

#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

I receive a linking error for both IID_IPortableDeviceManager and CLSID_PortableDeviceManager.

I looked through the PortableDeviceApi.h file that is included, and those two variables are defined as:

EXTERN_C const CLSID CLSID_PortableDeviceManager;
EXTERN_C const IID IID_IPortableDeviceManager;

What do I need to do to make the linker recognize these external variables?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Gregory Basior
  • 300
  • 1
  • 9
  • 1
    `EXTERN_C const CLSID CLSID_PortableDeviceManager;` is a declaration, not a definition. – wimh Jul 18 '16 at 15:09
  • 4
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – wimh Jul 18 '16 at 15:10
  • 1
    Project > Properties > Linker > Input > Additional Dependencies settings, add PortableDeviceGuids.lib – Hans Passant Jul 18 '16 at 15:22
  • Thank you @Hans Passant, that allowed the code to compile without the link error. – Gregory Basior Jul 18 '16 at 15:31

0 Answers0