简体   繁体   中英

Porting C++ Structure into C# (from an Unmanaged dll)

I am a Automotive Engineer and my company decided to buy a hardware for which the API was only in C++ ( I got unmanged dll and header files). The issue is that I have only coded in C# and most of our applications are very easy to build as the hardware provider always gives us the API in C# (managed dll). I now need to convert all the functions in the unmanged dll to C# functions. It was going smooth till I came across

typedef struct can_msg
{
    unsigned short ide;                         // Standard/extended msg
    unsigned int id;                            // 11 or 29 bit msg id
    unsigned short dlc;                         // Size of data
    unsigned char data[CAN_MSG_DATA_LEN];       // Message pay load
    unsigned short rtr;                         // RTR message
} can_msg_t;

I have no idea how to use it as this structure is an argument for a function for example:

VTC1010_CAN_BUS_API int  CAN_Transmission(can_msg_t *msg);

Please help guys. Do't start bashing me for my lack of knowledge. I tried to find but its all too hard for me.

Assuming that you don't have knowledge about how to use unamaged dlls in .net managed applications I can tell you some very basic things. The way to go is to create a "wrapper" for the unmanaged classes using Visual C++, then you can use the classes defined in this wrapper to operate with your unamanaged code.

You can find a good tutorial here: http://www.codeproject.com/Articles/14180/Using-Unmanaged-C-Libraries-DLLs-in-NET-Applicatio

Sorry for don't be more specific, but you need to start to study before create the code. Good luck!

Here is a quick and dirty conversion through the P/Invoke Interop Assistant sieve.

Input code:

#define CAN_MSG_DATA_LEN 100 // adjust correct value here

typedef struct can_msg
{
    unsigned short ide;                         // Standard/extended msg
    unsigned int id;                            // 11 or 29 bit msg id
    unsigned short dlc;                         // Size of data
    unsigned char data[CAN_MSG_DATA_LEN];       // Message pay load
    unsigned short rtr;                         // RTR message
} can_msg_t;

int  CAN_Transmission(can_msg_t *msg);

Output code:

public partial class NativeConstants {

    /// CAN_MSG_DATA_LEN -> 100
    public const int CAN_MSG_DATA_LEN = 100;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct can_msg {

    /// unsigned short
    public ushort ide;

    /// unsigned int
    public uint id;

    /// unsigned short
    public ushort dlc;

    /// unsigned char[100]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=100)]
    public string data;

    /// unsigned short
    public ushort rtr;
}

public partial class NativeMethods {

    /// Return Type: int
    ///msg: can_msg_t*
    [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="CAN_Transmission")]
public static extern  int CAN_Transmission(ref can_msg msg) ;

}
static class NativeMethods
{  


    // To  load dll
    [DllImport("kernel32.dll")]
    public static extern IntPtr LoadLibrary(string dllToLoad);
    // To get the Address of the Function
    [DllImport("kernel32.dll")]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
    // Freeing up the Library for other usage. 
    [DllImport("kernel32.dll")]
    public static extern bool FreeLibrary(IntPtr hModule);
}
class manageCAN
{
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    // Declaration of function to get
    private delegate int CAN_Initial(int baudRate);
    private delegate int Library_Release();

    // Getting the String for .dll Address
    static readonly string dllfile = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\VTC1010_CAN_Bus.dll";

    public int IntialiseCAN (int baudrate)
    {
        // Loading dll using Native Methods
        IntPtr pDll = NativeMethods.LoadLibrary(dllfile);
        if (pDll== IntPtr.Zero)
        {
            MessageBox.Show("Loading Failed");
        }   
        // Getting the Adress method
        IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "CAN_Initial");
        CAN_Initial initialiseCAN = (CAN_Initial)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(CAN_Initial));
        int result = initialiseCAN(baudrate);
        bool iresult = NativeMethods.FreeLibrary(pDll);
        return result;
    }
}

It works but I got confused when I got a structure as an argument, that's why I asked this question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM