Author Topic: dllImport in C++  (Read 14458 times)

UmuSBmad

  • Member
  • ***
  • Posts: 3
dllImport in C++
« on: September 28, 2011, 07:54:00 pm »
Hi, in C# the functions to interface with the USB devices come from Dll import statements, I'd like to know how how I can get the same functions in C++ where DllImport doesn't seem to work?
I've tried to understand the C++ code example on http://www.lvr.com/hidpage.htm but I couldn't get it to work and I'm not experienced enough to dissect it. Just a simple example of HidD_GetHidGuid would be great.
Thanks

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: dllImport in C++
« Reply #1 on: September 28, 2011, 10:01:28 pm »
#include <windows.h>

extern "C"
{
  #include <hidsdi.h>
}

GUID   HidGuid;
HidD_GetHidGuid(&HidGuid);   

Jan

UmuSBmad

  • Member
  • ***
  • Posts: 3
Re: dllImport in C++
« Reply #2 on: September 29, 2011, 08:09:48 am »
Unfortunately I've still got the same problem. I've downloaded the WDK, installed it to the C: directory and linked the libraries into a fresh C++ project and added "C:\WinDDK\7600.16385.1\inc\api;" (which holds hidsdi.h) to the 'C/C++ additonal includes' in the project properties.
Theres no red lines but when I compile I get a long list of errors, the first ones being:

1>c:\program files\microsoft visual studio 10.0\vc\include\crtdefs.h(543): error C2065: '_In_opt_z_' : undeclared identifier
1>c:\program files\microsoft visual studio 10.0\vc\include\crtdefs.h(543): error C2143: syntax error : missing ')' before 'const'
1>c:\program files\microsoft visual studio 10.0\vc\include\crtdefs.h(543): warning C4229: anachronism used : modifiers on data are ignored
1>c:\program files\microsoft visual studio 10.0\vc\include\crtdefs.h(543): error C2182: '_invalid_parameter' : illegal use of type 'void'
...

This being all my code:
Code: [Select]
#include <windows.h>

extern "C"
{
#include <hidsdi.h>
}

int main()
{
GUID HidGuid;
HidD_GetHidGuid(&HidGuid);
return 0;
}

So I'm a bit stumped.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: dllImport in C++
« Reply #3 on: September 29, 2011, 10:30:52 am »

UmuSBmad

  • Member
  • ***
  • Posts: 3
Re: dllImport in C++
« Reply #4 on: September 29, 2011, 05:12:14 pm »
Aha thanks for the pointer. There was also a problem with iostream and missing functions (cout) for the same reason as theres an iostream file in the WinDDK directories so got that sorted too.
I just put 'C:\Program Files\Microsoft Visual Studio 10.0\VC\include' above the WinDDK include's in the additional includes property which cleared up the stream of errors. Also had to add hid.lib to the property Linker-Input-Additional Dependencies and then it all worked.
Cheers, you are indeed worthy of your Hero Member status.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: dllImport in C++
« Reply #5 on: September 29, 2011, 10:26:27 pm »
I'm glad to hear you got it working. Thanks for reporting your solution. I wrote that application quite some time ago!

Jan