2

I am working on import C++ DLL Pointers-to-functions to C#.

My Code C++:

class __declspec(dllexport) deviceImport
{
public:
    deviceImport();
    virtual ~deviceImport();
    int __stdcall init(char* deviceName); 
    int __stdcall close();
    int __stdcall getDLLVersion(char* value); 

private:
    int handle;
    HINSTANCE hDLLDevice;
    bool __stdcall getProcAddresses( HINSTANCE *p_hLibrary, const char* p_dllName, int p_count, ... ); 
    int (__stdcall *Device_setPassword)(const char* value);
    int (__stdcall *Device_getDLLVersion)(int p_handle, char* value); 

};

My code C#:

[DllImport(dllLocation, CallingConvention = CallingConvention.Cdecl)]
public static extern int init([MarshalAs(UnmanagedType.LPStr)]string device);

//How to Import Device_setPassword and Device_getDLLVersion functions???

I can import init function but can you help me to import Device_setPassword and Device_getDLLVersion functions?

Akif
  • 41
  • 4
  • Does this answer your question? [using a class defined in a c++ dll in c# code](https://stackoverflow.com/questions/315051/using-a-class-defined-in-a-c-dll-in-c-sharp-code) – Blu3Souls Apr 08 '21 at 10:04
  • @Blu3Souls I have tried to marshall C++ as in the link. But I do not know how to call and parse `int (__stdcall *Device_setPassword)(const char* value);`. – Akif Apr 08 '21 at 12:02

1 Answers1

2

I solved my question. It might help others. Solution:

[DllImport(dllLocation, CallingConvention = CallingConvention.StdCall)]
public static extern int Device_setPassword(string password);

[DllImport(dllLocation, CallingConvention = CallingConvention.StdCall)]
public static extern int Device_getDLLVersion(int handle, out string value);
Akif
  • 41
  • 4