views:

366

answers:

3

I have a dll for a c++ class (SLABHIDDevice.dll). I am trying to use the functions of this dll in a C#.net application. The dll contains several methods which I can use easily with statements such as this...

(I appolagize if i get some of the terminology wrong here I am new to using dlls)

[DllImport("SLABHIDDevice.dll")]
        public static extern byte GetHidString
            (Int32 deviceIndex, Int32 vid, Int32 pid,
            Byte hidStringType, String deviceString, 
            Int32 deviceStringLength);

The documentation for SLABHIDDevice.dll says that it also contains a class object, CHIDDevice. and that object has a whole list of member functions such as Open();

If I try to import Open() using the same syntax as above, I get an error saying that it can not find an entry point for the Open() function. Is this because Open() is a member of CHIDDevice?

This is the makeup of the dll from DUMPBIN... The bottom three functions are the only ones I am able to get to work? Does anyone know what syntax I need to use to get the other ones? What do the question marks mean that precede the function names?

Dump of file SLABHIDDEVICE.dll

File Type: DLL

  Section contains the following exports for SLABHIDDevice.dll

    00000000 characteristics
    47E13E0F time date stamp Wed Mar 19 12:23:43 2008
        0.00 version
           1 ordinal base
          26 number of functions
          26 number of names

    ordinal hint RVA      name

          4    0 00001000 ??0CHIDDevice@@QAE@ABV0@@Z
          5    1 00001330 ??0CHIDDevice@@QAE@XZ
          6    2 00001430 ??1CHIDDevice@@UAE@XZ
          7    3 00001080 ??4CHIDDevice@@QAEAAV0@ABV0@@Z
          8    4 00020044 ??_7CHIDDevice@@6B@
          9    5 00001460 ?Close@CHIDDevice@@QAEEXZ
         10    6 00001C70 ?FlushBuffers@CHIDDevice@@QAEHXZ
         11    7 00001CA0 ?GetFeatureReportBufferLength@CHIDDevice@@QAEGXZ
         12    8 00001850 ?GetFeatureReport_Control@CHIDDevice@@QAEEPAEK@Z
         13    9 00001C80 ?GetInputReportBufferLength@CHIDDevice@@QAEGXZ
         14    A 00001BE0 ?GetInputReport_Control@CHIDDevice@@QAEEPAEK@Z
         15    B 00001A20 ?GetInputReport_Interrupt@CHIDDevice@@QAEEPAEKGPAK@Z
         16    C 00001CB0 ?GetMaxReportRequest@CHIDDevice@@QAEKXZ
         17    D 00001C90 ?GetOutputReportBufferLength@CHIDDevice@@QAEGXZ
         18    E 00001730 ?GetString@CHIDDevice@@QAEEEPADK@Z
         19    F 00001CC0 ?GetTimeouts@CHIDDevice@@QAEXPAI0@Z
         20   10 00001700 ?IsOpened@CHIDDevice@@QAEHXZ
         21   11 000014A0 ?Open@CHIDDevice@@QAEEKGGG@Z
         22   12 00001360 ?ResetDeviceData@CHIDDevice@@AAEXXZ
         23   13 00001810 ?SetFeatureReport_Control@CHIDDevice@@QAEEPAEK@Z
         24   14 00001B80 ?SetOutputReport_Control@CHIDDevice@@QAEEPAEK@Z
         25   15 000018C0 ?SetOutputReport_Interrupt@CHIDDevice@@QAEEPAEK@Z
         26   16 00001CE0 ?SetTimeouts@CHIDDevice@@QAEXII@Z
          3   17 00001320 GetHidGuid
          2   18 00001230 GetHidString
          1   19 00001190 GetNumHidDevices

  Summary

        6000 .data
        7000 .rdata
        5000 .reloc
        4000 .rsrc
       1C000 .text
+1  A: 

C++ uses name mangling. All the weird symbols around your function names are a way for the compiler/linker to know the calling convention, parameters, return type, etc.

If you don't want to use name mangling with your functions, you need to surround them with a

extern "C" {
}

statement.

See http://en.wikipedia.org/wiki/Name_mangling

Kevin
+1  A: 

You cannot use P/Invoke to call instance methods of a C++ class. The primary hang-up is that you can't create an object of the class, you cannot discover the required memory allocation size. Passing the implicit "this" pointer to the instance method is another problem, it needs to be passed in a register.

You'll need to create a managed wrapper for the class, that requires using the C++/CLI language. Google "C++/CLI wrapper" for good hits.

Hans Passant
Passing `this` isn't a problem as there is `CallingConvention.ThisCall` (which is compatible with VC++, at least). Required memory allocation side can be determined if headers are available for the DLL (and they should be, otherwise no-one would be able to use it from C++, either).
Pavel Minaev
Hmm, not sure how the C# compiler is going to parse that header.
Hans Passant