Gustavo Litovsky
Gustavo Litovsky

Reputation: 2487

Calling functions from Unmanaged DLL

I have an unmanaged DLL with the following functions:

ReadLatch( HANDLE cyHandle,
                LPWORD  lpLatch);


WriteLatch(HANDLE cyHandle,
                WORD    mask,
                WORD    latch);



GetPartNumber(HANDLE cyHandle,
                   LPBYTE   lpbPartNum);

 GetDeviceProductString(HANDLE  cyHandle,
                            LPVOID  lpProduct,
                            LPBYTE  lpbLength,
                            BOOL    bConvertToASCII = TRUE
                            );


GetDeviceSerialNumber(HANDLE   cyHandle,
                           LPVOID   lpSerialNumber,
                           LPBYTE   lpbLength,
                           BOOL     bConvertToASCII = TRUE
                           );


 GetDeviceInterfaceString(HANDLE    cyHandle,
                              LPVOID lpInterfaceString,
                              LPBYTE lpbLength,
                              BOOL bConvertToASCII);

I'm trying to import these functions but haven't had much likle finding the right data types:

[DllImportAttribute("runtime.dll", EntryPoint = "ReadLatch", CallingConvention =     CallingConvention.Cdecl)]
    static extern int ReadLatch(HANDLE cyHandle, [MarshalAs(UnmanagedType. ??????)] ?????? lpLatch);

    [DllImportAttribute("runtime.dll", EntryPoint = "WriteLatch", CallingConvention = CallingConvention.Cdecl)]
    static extern int WriteLatch(HANDLE cyHandle,
                WORD mask,
                WORD latch);

    [DllImportAttribute("runtime.dll", EntryPoint = "GetPartNumber", CallingConvention = CallingConvention.Cdecl)]
    static extern int GetPartNumber(HANDLE cyHandle,
                   LPBYTE lpbPartNum);

    [DllImportAttribute("runtime.dll", EntryPoint = "GetDeviceProductString", CallingConvention = CallingConvention.Cdecl)]
    static extern int GetDeviceProductString(HANDLE cyHandle,
                            LPVOID lpProduct,
                            LPBYTE lpbLength,
                            BOOL bConvertToASCII = TRUE
                            );

    [DllImportAttribute("runtime.dll", EntryPoint = "GetDeviceSerialNumber", CallingConvention = CallingConvention.Cdecl)]
    static extern int GetDeviceSerialNumber(HANDLE cyHandle,
                           LPVOID lpSerialNumber,
                           LPBYTE lpbLength,
                           BOOL bConvertToASCII = TRUE
                           );

    [DllImportAttribute("runtime.dll", EntryPoint = "GetDeviceInterfaceString", CallingConvention = CallingConvention.Cdecl)]
    static extern int GetDeviceInterfaceString(HANDLE cyHandle,
                              LPVOID lpInterfaceString,
                              LPBYTE lpbLength,
                              BOOL bConvertToASCII);

Where can I find information about how to represent HANDLE, LPWORD and the others so I can call the functions?

Upvotes: 0

Views: 527

Answers (1)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137527

Unmanaged types and their managed counterparts:

  • HANDLE is usually represented by IntPtr.
  • WORD - UInt16

For the other ones, we may need to know a bit more about how they are used.

Hopefully there is some accompanying documentation with your API that explains what the parameters do, as some of them are not completely obvious.

For this function, we can make some assumptions:

ReadLatch(HANDLE cyHandle, LPWORD  lpLatch);

Assuming that lpLatch is really an "out" parameter (and your return type is int):

[DllImportAttribute("runtime.dll", EntryPoint = "ReadLatch", CallingConvention = CallingConvention.Cdecl)]
static extern int ReadLatch(IntPtr cyHandle, out UInt16 lpLatch);

Upvotes: 1

Related Questions