用字符串数据C#调用Structure的C ++数组 [英] Call C++ array of Structure with string data C#
本文介绍了用字符串数据C#调用Structure的C ++数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我想将结构数组从vc6 dll传递到C#2010.我是C的新手
有人可以帮我提供示例代码吗?
谢谢
问候
ramesh
Hi
I want to pass a array of structure from vc6 dll to C# 2010. I am new to C sharp
can any one help me with a sample code
thanks
regards
ramesh
推荐答案
必要时在C#中定义一个具有[MarshalAs]属性的等效结构,并在C#中对方法MyStructure []进行签名.
Define an equivalent structure in C#, with [MarshalAs] attributes if necessary, and make the signature of the method MyStructure[] in C#.
namespace DLLTest
{
public class Program
{
[StructLayout(LayoutKind.Sequential), Serializable]
struct STdatapoint
{
int st_Exchange;
int st_product;
int st_High;
int st_Low;
int st_Tunover;
int st_Volume;
//[MarshalAsAttribute(UnmanagedType.ByValTStr)]
string st_SymbolId;
//[MarshalAsAttribute(UnmanagedType.ByValTStr)]
string st_USymbol;
}
[DllImport("C:\\TADBAccess.dll", CallingConvention = CallingConvention.Cdecl)]
rivate static extern void GetMasterData([Out, MarshalAs(UnmanagedType.SafeArray)] out STdatapoint[] strServerList, ref int NoOfElement, ref int Startpos);
static void Main(string[] args)
{
STdatapoint[] Obj = new STdatapoint[5];
//Marshal.StructureToPtr(
int No = 4;
int Ret = 1;
GetMasterData(out Obj, ref No, ref Ret);
// Console.ReadKey();
}
我收到此错误
尝试读取或写入受保护的内存.这通常表明其他内存已损坏.
我在访问VC dll时在VC dll中使用safearray,出现此错误.给我任何想法.
谢谢
问候
ramesh
I am getting this error
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I am using safearray in VC dll while accessing the VC dll , I am getting this error. Give me any idea.
thanks
regards
ramesh
这篇关于用字符串数据C#调用Structure的C ++数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文