1

I need to use some SetupAPI functions in firefox extension. I need to retrieve device's friendly name. I figured it can be done by SetupDiGetClassDevs, SetupDiEnumDeviceInfo, SetupDiGetDeviceRegistryProperty and SetupDiDestroyDeviceInfoList.

BUT! I imported the setupapi.dll and declared three of the functions - no problem. Then I found out that SetupDiGetDeviceRegistryProperty simply isn't in the DLL at all and can only be statically linked with setupapi.lib. Is there any way I could substitute this function?

I cannot use WMI.

Ajay
  • 18,086
  • 12
  • 59
  • 105
Kobrar
  • 184
  • 1
  • 1
  • 10
  • You're right, there isn't an export with that exact name. That name is actually defined in the SetupApi header, like most of the functions in the windows api that have Unicode & ANSI variants. The function is in the export table as `SetupDiGetDeviceRegistryPropertyW` (ordinal: 373) or `SetupDiGetDeviceRegistryPropertyA` (ordinal: 372). – theB May 24 '16 at 14:52
  • That solved it. I messaged microsoft about the lacking documentation on that. Thanks. – Kobrar May 24 '16 at 15:03
  • I turned my comment into a real answer. – theB May 24 '16 at 15:15
  • That's entirely normal, winapi functions that take a string exist in two versions. The A version is meant for legacy code that still uses char*, the W version is for modern code that uses Unicode strings. This gets sorted out at compile time based on the UNICODE #define. Hopefully you don't use GetProcAddress(), at least favor the /DELAYLOAD linker option, but if you do then you have to know the real name. – Hans Passant May 24 '16 at 15:51

2 Answers2

1

You're right, there isn't an export with that exact name. That name is actually defined in the SetupApi header, like most of the functions in the Windows API that have Unicode & ANSI variants.

From SetupApi.h:

#ifdef UNICODE
#define SetupDiGetDeviceRegistryProperty SetupDiGetDeviceRegistryPropertyW
#else
#define SetupDiGetDeviceRegistryProperty SetupDiGetDeviceRegistryPropertyA
#endif

The function is in the export table as SetupDiGetDeviceRegistryPropertyW (ordinal: 373) or SetupDiGetDeviceRegistryPropertyA (ordinal: 372)

I found those using dumpbin /exports setupapi.dll.

theB
  • 6,450
  • 1
  • 28
  • 38
1

This function is indeed in SetupAPI.DLL, as I've confirmed using Dependency Walker. It's just that, it takes a character-pointer (string), it has to have two variants - one for ANSI (A), one for Unicode (W).

  • SetupDiGetDeviceRegistryPropertyA
  • SetupDiGetDeviceRegistryPropertyW

It is with any Windows API function - if function takes one or more of character-string as arguments, it would have two variants.

I see you are probably using GetProcAddress to locate it. Hence, you need to pass real name (not macro) to it. Following gets wide-variant of this function.

GetProcAddress(handleOfDLL, "SetupDiGetDeviceRegistryPropertyW"); // Wide
Ajay
  • 18,086
  • 12
  • 59
  • 105
  • In cases like this, where I have to use GetProcAddress, I like to use a stringize macro to generate the function name string, rather than typing it manually. That way, I not only get autocompletion and protection against typos, but also the correct A/W variant depending on how UNICODE is #defined. Something like _CRT_STRINGIZE is generally available if you're using the CRT, or you can roll your own. – Cody Gray - on strike May 26 '16 at 12:41