原函数的定义如下:
int WINAPI SendData(const char *ChannelName, const void *Buffer, const long Size );
void *Buffer是要发送的结构体变量.
那么,C#中该如何使用这个函数呢?

解决方案 »

  1.   

            [System.Runtime.InteropServices.DllImport("dllname", SetLastError = true)]
            static extern unsafe int SendData
            (
                char *ChannelName,
                void *Buffer,
                long size
            );
      

  2.   

    c++    => c#
    char * => char[]
    void * => object
    long   => int
    int    => short
      

  3.   

    “void *Buffer是要发送的结构体变量”,那就给结构体变量,不要加ref。
      

  4.   

    IntPrt[System.Runtime.InteropServices.DllImport("dllname", SetLastError = true)]
            static extern unsafe int SendData
            (
                [MarshalAs(UnmanagedType.LPStr)]  //究竟是LPStr还是LPTStr还是LPWStr要看你的dll
                string ChannelName,
                [MarshalAs(UnmanagedType.AsAny)]
                ref Buffer结构体类型 Buffer,
                int size
            );
      

  5.   


    [System.Runtime.InteropServices.DllImport("dllname", SetLastError = true)]
            static extern unsafe int SendData
            (
                [MarshalAs(UnmanagedType.LPStr)]  //究竟是LPStr还是LPTStr还是LPWStr要看你的dll
                string ChannelName,
                [MarshalAs(UnmanagedType.AsAny)]
                ref Buffer结构体类型 Buffer,
                int size
            );
      

  6.   

    你可以根椐你的参数类型决定用什么去替换void*,这是一个万能的指针,放什么都可以。
      

  7.   

    没试过,希望对LZ有帮助:
    http://topic.csdn.net/t/20050809/09/4197232.html
      

  8.   

    这段代码使用了我说的那个函数,在C里面运行通过,在C#中应该如何修改?如果我想使用Arraylist tel_l3,该怎样调用这个函数?
    typedef struct Tel_Head_L3
    {
    int Len;
    int ID;
    }Tel_Head_L3, *PTel_Head_L3;typedef struct SlabIdent_L3
    {
    Tel_Head_L3 Head; //电文头
    char CoilNo[COILNO_LEN+1];    
    }SLABIDENT_L3,*PSLABIDENT_L3;

    SlabIdent_L3 tel_l3;
    memset(&tel_l3, 0x00, sizeof(tel_l3));
    tel_l3.Head.ID=TELID_SLABIDENT_L3;
    tel_l3.Head.Len=sizeof(tel_l3);
    sprintf( tel_l3.CoilNo,coilno);
    int rect=SendData("send", &tel_l3, sizeof(tel_l3));
      

  9.   


    struct Tel_Head_L3 

    public int Len; 
    public int ID; 
    }struct SlabIdent_L3 

    public Tel_Head_L3 Head; //电文头 
    public byte[] CoilNo;
    }SlabIdent_L3 tel_l3;tel_l3.CoilNo=new byte[COILNO_LEN+1];  //常量自己获取
    tel_l3.Head.ID=TELID_SLABIDENT_L3;      //常量自己获取
    tel_l3.Head.Len=sizeof(tel_l3);int rect=SendData("send", ref tel_l3, sizeof(tel_l3));
      

  10.   


    [System.Runtime.InteropServices.DllImport("dllname", SetLastError = true)] //dllname改成你那dll的名称
    static extern unsafe int SendData 

        [MarshalAs(UnmanagedType.LPStr)]  //究竟是LPStr还是LPTStr还是LPWStr要看你的dll 
        string ChannelName, 
        [MarshalAs(UnmanagedType.AsAny)] 
        ref SlabIdent_L3 Buffer, 
        int size 
    ); 
      

  11.   

    使用IntPtr 和 Marshal.Copy(.....)。
    自己查下。
      

  12.   

    或者定义成IntPtr然后用Marshal.StructureToPtr把内容复制过指针也行
    总之方法很多呀