zoukankan      html  css  js  c++  java
  • Passing structures between C# and C/C++

    Data structures can be passed between managed C# code and unmanaged C/C++ dynamic link libraries but it can be a little complicated. The trick is to define structures that match between the two different languages; and perhaps write wrapper functions that hide away the complexity of the memory marshaling code. I will show a couple of simple examples of passing a system time data structure from C/C++ to C# and vice versa here.

    The C/C++ definition of the system time structure is as follows:

    typedef struct _SYSTEMTIME {
        WORD wYear;
        WORD wMonth;
        WORD wDayOfWeek;
        WORD wDay;
        WORD wHour;
        WORD wMinute;
        WORD wSecond;
        WORD wMilliseconds;
    } SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;


    The matching C# equivalent of the system time structure can be defined as shown below.

    private struct SYSTEMTIME
    {
        public UInt16 wYear;
        public UInt16 wMonth;
        public UInt16 wDayOfWeek;
        public UInt16 wDay;
        public UInt16 wHour;
        public UInt16 wMinute;
        public UInt16 wSecond;
        public UInt16 wMilliseconds;
    }


    Example 1: Passing a structure from C/C++ to C#
    If the C/C++ function has the following signature:

    HRESULT GetDateTime( HANDLE hClient, SYSTEMTIME* pSysTime );


    Then the following wrapper functions can be used to call the C/C++ function.

    [DllImport("MyDynamicLinkLib.dll")]
    private static extern int GetDateTime(IntPtr hClient, IntPtr pSysTime);
    public static int GetDateTime(IntPtr hClient, out DateTime sysTime)
    {
        int hr = 0;
        IntPtr pSysTime = IntPtr.Zero;
        SYSTEMTIME theTime = new SYSTEMTIME();
        sysTime = new DateTime();
        pSysTime = Marshal.AllocHGlobal( Marshal.SizeOf(theTime));
        hr = GetDateTime(hClient, pSysTime);
        theTime = (SYSTEMTIME) Marshal.PtrToStructure(pSysTime, typeof(SYSTEMTIME));
        Marshal.FreeHGlobal(pSysTime);
        sysTime = new DateTime(theTime.wYear, theTime.wMonth, theTime.wDay, theTime.wHour, theTime.wMinute, theTime.wSecond, theTime.wMilliseconds);
        return hr;
    }


    To use the wrapper functions, simply do something like the following:

    IntPtr hClient = IntPtr.Zero;
    DateTime dt = new DateTime();
    int ret = GetDateTime( hClient, out dt);

      


    Example 2: Passing a structure from C# to C/C++
    If the C/C++ function has the following signature,

    HRESULT SetDateTime( HANDLE hClient, SYSTEMTIME* pSysTime );


    Then the following wrapper functions can be used to call the C/C++ function.

    [DllImport("MyDynamicLinkLib.dll")]
    private static extern int SetDateTime(IntPtr hClient, IntPtr pSysTime);
    public static int SetDateTime(IntPtr hClient, DateTime sysTime)
    {
        int hr = 0;
        IntPtr pSysTime = IntPtr.Zero;
        SYSTEMTIME theTime = new SYSTEMTIME();
        theTime.wYear = (ushort) sysTime.Year;
        theTime.wMonth = (ushort) sysTime.Month;
        theTime.wDay = (ushort) sysTime.Day;
        theTime.wDayOfWeek = (ushort) sysTime.DayOfWeek;
        theTime.wHour = (ushort) sysTime.Hour;
        theTime.wMinute = (ushort) sysTime.Minute;
        theTime.wSecond = (ushort) sysTime.Second;
        theTime.wMilliseconds = (ushort) sysTime.Millisecond;
        pSysTime = Marshal.AllocHGlobal(Marshal.SizeOf(theTime));
        hr = SetDateTime(hClient, pSysTime);
        Marshal.FreeHGlobal(pSysTime);
        return hr;
    }


    Do the following to call the wrapper functions.

    DateTime dt = new DateTime();
    int hr = SetDateTime(hClient, dt);
  • 相关阅读:
    arm SecurCore 处理器【转】
    ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57【转】
    arm处理器中a5 a8 a9,v6 v7,arm7 arm9 arm11都是依据什么来分类的【转】
    platform型设备在/dev目录下自动创建设备节点的分析【转】
    linux下bus、devices和platform的基础模型 【转】
    kernel 中 sscanf和sprintf()函数使用说明【转】
    Linux内核中的printf实现【转】
    sprintf,snprintf的用法(可以作为linux中itoa函数的补充)【转】
    Linux设备模型(3)_Uevent【转】
    Linux内核中的GPIO系统之(3):pin controller driver代码分析--devm_kzalloc使用【转】
  • 原文地址:https://www.cnblogs.com/stoneG/p/6689938.html
Copyright © 2011-2022 走看看