Search code examples
c#c++dlldllimport

Import C++ DLL Pointers-to-functions to C#


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?


Solution

  • 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);