C# 发中调用一个采用C++封装好的dll, 其结构体中的二维数转换
来源:互联网 发布:肇庆单片机工程师招聘 编辑:程序博客网 时间:2024/05/24 23:16
想在C# WinForm开发中调用一个采用C++封装好的dll,但其结构体中的二维数组不知道应当如何转换,网上搜索了一把P/Invoke资料相对来说比较少,没有找到切实有用的文章,现在调用虽不会报错了,但没有产生预期效果.
C/C++ code
#define MAX_STRM_LAYER 3 //最多几级流媒体
//服务器信息
typedefstruct tagServerInfo
{
long uID;
char csStrMIP[MAX_STRM_LAYER][16]; //这个不知道如何转换
unsigned short nStrMPort[MAX_STRM_LAYER];
char csDdtIP[16];
unsigned short nDdtPort;
unsigned short bIsWebUser;
unsigned short protocolType;
constchar *pcUserName;
constchar *pcPassword;
char csStoreFileSvrIP[16];
unsigned short nStoreFileSvrPort;
char csDevFileSvrIP[16];
unsigned short nDevFileSvrPort;
}TServerInfo, *LPServerInfo;
LONG __stdcall StrM_Login(LPServerInfo mts,int iLayer =1);
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct TServerInfo
{
public int uID;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 3 * 16)]
public char[] csStrMIP;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 3)] // 这里原先忘记指定了
public ushort[] nStrMPort;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 16)]
public string csDdtIP;
public ushort nDdtPort;
public ushort bIsWebUser;
public ushort protocolType;
public string pcUserName;
public string pcPassword;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 16)]
public string csStoreFileSvrIP;
public ushort nStoreFileSvrPort;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 16)]
public string csDevFileSvrIP;
public ushort nDevFileSvrPort;
}
测试代码:
C++的多维数组,转成C#的一维数组处理
C# code
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
publicstruct TServerInfo
{
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 3*16]
publicchar[] csStrMIP;
}
刚自己写的试验代码
C/C++ code
typedefstruct tagSvrInfo
{
char csStrMIP[3][16];
}TSvrInfo,*LPSvrInfo
//assign char value 'a','b','c'
PIDLL_APIvoid Login(LPSvrInfo info)
{
info->csStrMIP[0][0] = 'a';
info->csStrMIP[1][0] = 'b';
info->csStrMIP[2][0] = 'c';
}
extern"C"
{
PIDLL_API void Login(LPSvrInfo info);
};
C# code
[DllImport("pidll.dll")]
staticexternint Login(ref SvrInfo info);
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
publicstruct SvrInfo
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3 *16)]
publicchar[] csStrMIP;
}
staticvoid Main(string[] args)
{
SvrInfo si = new SvrInfo();
si.csStrMIP = newchar[48];
Login(ref si);
Console.WriteLine(si.csStrMIP[0]);
Console.WriteLine(si.csStrMIP[16]);
Console.WriteLine(si.csStrMIP[32]);
}
/*---------------输出为----------
a
b
c
------------------------------*/
文献来源:http://topic.csdn.net/u/20100915/22/5861d644-d3e9-43eb-9168-b19e7287dc43.html
- C# 发中调用一个采用C++封装好的dll, 其结构体中的二维数转换
- C# 调用C++dll中的结构体的定义
- C#调用C++dll的类型转换
- C#调用c++Dll结构体数组指针的问题
- C#调用c++Dll结构体数组指针的问题
- C#调用c++Dll结构体数组指针的问题
- C#调用c++Dll结构体数组指针的问题
- C#调用c++Dll结构体数组指针的问题
- C++ dll 结构体中的二维数组 转为C#
- C#调用C++封装的DLL传递结构体数组的终极解决方案
- C#调用C++DLL数据封送结构体转换工具
- C#调用C++Dll封装时遇到的一系列问题
- C#调用C++Dll封装时遇到的一系列问题(2)
- C#调用C++Dll封装时遇到的一系列问题
- C#调用C++Dll封装时遇到的一系列问题
- C#调用C++Dll封装时遇到的一系列问题
- C#调用C++Dll封装时遇到的一系列问题
- C#调用C++Dll封装时遇到的一系列问题
- 走向大牛之-----linux编程一
- 入门——android全局设置的两张方式
- c1-3-main-frm_12800-12900渲染日志
- 有类似数组(1,2,3,1,1), 若相同元素大于一半以上则输出True, 反之返回False. (可不必输入具体值)
- 汇编 输入几个数子进行比较 不同条输出不同的结果 三个相同输出2 两个同输出1 不同输出0
- C# 发中调用一个采用C++封装好的dll, 其结构体中的二维数转换
- 成为江苏卫视《一站到底》节目站神之策略
- JQuery Tree插件——zTree v3.2 正式版发布
- 汇编发声程序示例
- 9 个重量级的云计算(PaaS)平台
- NoSQL 数据建模技术
- 数据结构C语言版 哈希表
- 对C51单片机的初步认识
- jsp应用中参数(字符串)过滤、防sql注入问题