C#调用外部vc++ dll ,如何传递引用型字符串数组?
在VC++下定义:
#define DLLEXPORT extern "C" __declspec(dllexport)
extern "C" __declspec(dllexport) int __stdcall StringTest( wchar_t **& str1,wchar_t ** tr2,int iarray);
DLLEXPORT int __stdcall StringTest( wchar_t **& str1,wchar_t ** str2,int iarray)
{
for (int i=0;i<iarray;i++)
{
wcscat(str1[i],str2[i]);
}
return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
C#下调用:
//外部dll函数申明
[DllImport("csdll.dll", EntryPoint = "StringTest", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
private static extern int StringTest([MarshalAs(UnmanagedType.LPArray)]ref string[] str1, string[] str2, int iarray);
//主函数调用
string[] sParmIds = new String[4];
string[] sParmValues = new String[4];
sParmIds[0] = "炉膛宽度";
sParmIds[1] = "炉膛高度";
sParmIds[2] = "字符串";
sParmIds[3] = "小数";
sParmValues[0] = "4500";
sParmValues[1] = "3500";
sParmValues[2] = "长江黄河";
sParmValues[3] = "3.6";
StringTest(ref sParmIds, sParmValues,4);//在这一步,报错。其他信息: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
for (int i = 0; i < 3; i++)
MessageBox.Show(sParmIds[i]);
//我的意图是,返回的sParmIds数组为:
//sParmIds[0]=="炉膛宽度4500"
//sParmIds[1]== "炉膛高度3500";
//sParmIds[2]== "字符串长江黄河";
//sParmIds[3]== "小数3.6";
请教诸位,此问题如何解决?
#define DLLEXPORT extern "C" __declspec(dllexport)
extern "C" __declspec(dllexport) int __stdcall StringTest( wchar_t **& str1,wchar_t ** tr2,int iarray);
DLLEXPORT int __stdcall StringTest( wchar_t **& str1,wchar_t ** str2,int iarray)
{
for (int i=0;i<iarray;i++)
{
wcscat(str1[i],str2[i]);
}
return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
C#下调用:
//外部dll函数申明
[DllImport("csdll.dll", EntryPoint = "StringTest", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
private static extern int StringTest([MarshalAs(UnmanagedType.LPArray)]ref string[] str1, string[] str2, int iarray);
//主函数调用
string[] sParmIds = new String[4];
string[] sParmValues = new String[4];
sParmIds[0] = "炉膛宽度";
sParmIds[1] = "炉膛高度";
sParmIds[2] = "字符串";
sParmIds[3] = "小数";
sParmValues[0] = "4500";
sParmValues[1] = "3500";
sParmValues[2] = "长江黄河";
sParmValues[3] = "3.6";
StringTest(ref sParmIds, sParmValues,4);//在这一步,报错。其他信息: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

for (int i = 0; i < 3; i++)
MessageBox.Show(sParmIds[i]);
//我的意图是,返回的sParmIds数组为:
//sParmIds[0]=="炉膛宽度4500"
//sParmIds[1]== "炉膛高度3500";
//sParmIds[2]== "字符串长江黄河";
//sParmIds[3]== "小数3.6";
请教诸位,此问题如何解决?
...全文
相关推荐
C#调用C++的dll实例,回调函数,string和int数组参数传递
C#调用C++的dll实例,回调函数,string和int数组参数传递,在vs2010下调试通过,总结在这里,供大家参考。
C#调用C++DLL传递结构体数组的终极解决方案
主要介绍了C#调用C++DLL传递结构体数组的终极解决方案的相关资料,需要的朋友可以参考下
C#调用C++ Dll关于结构体数组引用的传递及解析使用的展示代码
资源代码演示的是c#代码调用c++ DLL 的方式。该演示为原创,绝非搬砖。解决了c# 调用 C++ Dll获取相关信息之如何传递结构体数组引用以及如何处理获取到的结构体数组数据的问题。
c#中如何调用外部DLL
每种编程语言调用DLL的方法都不尽相同,在此只对用C#调用DLL的方法进行介绍。
C#调用 C++dll(包含传递自定义结构数组)
当C#调用C++dll时,需要传递自定义结构的数组,该怎么办? 示例给出了方案


