C#如何调用c++中定义的函数并传入结构体?

时间:2022-08-30 19:13:15
以下为C++中定义的函数及结构体:
typedef struct
{
char cSerial[8];
char userNo[7]; 
char userName[33]; 
char unitNo[13]; 
char unitName[33]; 
}Y_INFO;

以下是在VC中定义的方法
unsigned char WriteInfo(Y_INFO *info,unsigned short int *iRet)

参数说明:
Y_INFO *info:输入参数
*iRet :为输出参数

以下为我在c#中定义的代码
public struct Y_INFO
{
 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
 public string cSerial;
 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]
 public string userNo;
 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
 public string userName;
 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]
 public string unitNo;
 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
 public string unitName;
}

这是调用的方法声明:

[DllImport("EDV.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "WriteZFYInfo")]
        public static extern bool WriteZFYInfo(ZFY_INFOX info, string sPwd, ref int iRet);

以下为方法的调用:
                Y_INFO wIn;
                wIn.cSerial = "W00000";
                wIn.userNo = "123456";
                wIn.userName = "";
                wIn.unitNo = "";
                wIn.unitName = "";
                var winfo = WriteZFYInfo(wIn, pwd, ref iRet);

执行时出错了,错误提示为:
其他信息: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
 
各位大大帮我看一下,我是哪个地方出了问题?是赋值吗,还是什么原因的?
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
结构体的类型我全部都试过了,什么:ByValTStr,BStr,LPTStr,LPWStr,LPStr全部试过一次了,还是不行!!!实在很崩溃!

2 个解决方案

#1



public struct Y_INFO {
    
    /// char[8]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=8)]
    public string cSerial;
    
    /// char[7]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=7)]
    public string userNo;
    
    /// char[33]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=33)]
    public string userName;
    
    /// char[13]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=13)]
    public string unitNo;
    
    /// char[33]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=33)]
    public string unitName;
}





    [System.Runtime.InteropServices.DllImport("EDV.dll", EntryPoint="WriteInfo")]
public static extern  byte WriteInfo(ref Y_INFO info, ushort param1, ref int iRet) ;

#2


真的太厉害了!赞!

#1



public struct Y_INFO {
    
    /// char[8]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=8)]
    public string cSerial;
    
    /// char[7]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=7)]
    public string userNo;
    
    /// char[33]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=33)]
    public string userName;
    
    /// char[13]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=13)]
    public string unitNo;
    
    /// char[33]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=33)]
    public string unitName;
}





    [System.Runtime.InteropServices.DllImport("EDV.dll", EntryPoint="WriteInfo")]
public static extern  byte WriteInfo(ref Y_INFO info, ushort param1, ref int iRet) ;

#2


真的太厉害了!赞!