typedef struct _COMMPORTCONFIG {
DWORD BaudRate; // baud rate
DWORD ByteSize; // number of bits/byte, 4-8
DWORD Parity; // 0-4=no,odd,even,,space
DWORD StopBits; // 0,1,2 = 1, 1.5, 2
DWORD fOutxCtsFlow; // CTS Flow Control
} COMMPORTCONFIG;
C/C++:
BOOL MetrocomInitCommunication(int i_port, COMMPORTCONFIG * p_config);
现在我如何转成C#可以引用的,还有要怎么调用?

解决方案 »

  1.   

    DWORD可以使用Int类型,在C#里声明Struct来对应_COMMPORTCONFIG ,然后像调用API一样来调用这个C++的函数,使用Ref传递结构。
      

  2.   

    类似的,看看吧
    DLL  传递结构   (见代码)
    BOOL PtInRect(const RECT *lprc, POINT pt); using System.Runtime.InteropServices; 
    [StructLayout(LayoutKind.Sequential)] 
    public struct Point {
    public int x; 
    public int y;

    [StructLayout(LayoutKind.Explicit)] 
    public struct Rect 

    [FieldOffset(0)] public int left; 
    [FieldOffset(4)] public int top;
    [FieldOffset(8)] public int right; 
    [FieldOffset(12)] public int bottom;

    Class XXXX { 
    [DllImport("User32.dll")] 
    public static extern bool PtInRect(ref  Rect r, Point p); 

      

  3.   

      public struct COMMPORTCONFIG
            {
                int BaudRate; // baud rate
                int ByteSize; // number of bits/byte, 4-8
                int Parity; // 0-4=no,odd,even,,space
                int StopBits; // 0,1,2 = 1, 1.5, 2
                int fOutxCtsFlow; // CTS Flow Control
            }        [DllImport("DLL文件名")]
            public static extern bool MetrocomInitCommunication(int i_port, ref COMMPORTCONFIG p_config);
      

  4.   


    [DllImport("你DLL名称")] 
    public static extern bool  MetrocomInitCommunication(int i_port, ref COMMPORTCONFIG  p_config);[StructLayout(LayoutKind.Sequential)] 
    public struct _COMMPORTCONFIG {
    int  BaudRate; // baud rate
    int  ByteSize; // number of bits/byte, 4-8
    int  Parity; // 0-4=no,odd,even,,space
    int  StopBits; // 0,1,2 = 1, 1.5, 2
    int  fOutxCtsFlow; // CTS Flow Control
    }int i_port;
    COMMPORTCONFIG  p_config=new COMMPORTCONFIG ();
    MetrocomInitCommunication(i_port, ref  p_config);