How to convert void * to a type that can be used in C #? Interaction between C DLL and C #

I am a C / C ++ programmer, but I was asked to update a program written in C # to communicate with the device. My knowledge of C # is very simple.

The previous version was completely written in C #, but now the API that actually got access to the device has been changed to C. I found out that I can import the C API functions using:

[DllImport("myapi.dll")]
public static extern int myfunct( 
                                 [MarshalAs(UnmanagedType.LPTStr)] string lpDeviceName,
                                 IntPtr hpDevice);

In C, this function prototype:

int myFunct( LPTStr lpDeviceName, HANDLE* hpDevice );

Where the HANDLE is defined as:

typedef void *HANDLE;

However, this function does not work as expected. Actually, in C # code, what type of type should I declare and go to the C # method?

Thanks for the help and sorry for any stupid question.

+3
source share
2

IntPtr ref IntPtr, :

[DllImport("myapi.dll")]
public static extern int myfunct( 
    [MarshalAs(UnmanagedType.LPTStr)] string lpDeviceName,
    ref IntPtr hpDevice);
+1

, HANDLE *. , .

, , ( hpDevice int).

, . , HANDLE CloseHandle ( , HANDLE), , , , SafeHandleZeroOrMinusOneIsInvalid. , , SafeRegistryHandle; , SafeFileHandle.

- , ( CloseHandle ), , SafeHandleZeroOrMinusOneIsInvalid. - , CloseHandle, , , SafeHandle.

SafeHandle -derived, ( SafeFileHandle ):

[DllImport("myapi.dll")]
public static extern int myFunct(
    [MarshalAs(UnmanagedType.LPTStr)] string lpDeviceName,
    out SafeFileHandle hpDevice);
+4

Source: https://habr.com/ru/post/1754983/


All Articles