包含vector< structure>的C#pinvoke编组结构
我需要为Windows ce 6.0项目调用一个函数,该函数返回一个结构,该结构包含int和其他结构的向量(在C#中):
I'm in need to call an function that return an structure that contains an int and an vector of other structures in C# for a windows ce 6.0 project:
该功能由第三方提供商(中国的pda制造商)提供,他们只向我提供了.h文件,dll和lib.
The function is provided by an 3rd party provider (Chinese manufacturer of the pda), and they only delivered me the .h files, the dll and lib.
我要在C#中调用的函数在.h文件中定义为:
The function i'm trying to call in C# is defined in the .h file as :
DLLGSMADAPTER ApnInfoData* GetAvailApnList();
ApnInfoData结构如下:
the ApnInfoData structure is as follows:
typedef struct ApnInfoData
{
int m_iDefIndex;
ApnInfoArray m_apnList;
}
typedef struct ApnInfo
{
DWORD m_dwAuthType;
TCHAR m_szName[64];
TCHAR m_szTel[32];
TCHAR m_szUser[32];
TCHAR m_szPassword[32];
TCHAR m_szApnName[32];
}*LPAppInfo;
typedef vector<ApnInfo> ApnInfoArray;
DLLGSMADAPTER是
the DLLGSMADAPTER is a
#define DLLGSMADAPTER _declspec(dllexport)
我的问题是,如何在.net cf中使用该函数,因为它使用了vector类,并且我不知道如何将其编组.
My question is how can i pinvoke this function in the .net cf, since it uses the vector class, and i don't know how to marshal this.
仅使用常规P/Invoke Interop即可在C#中包装std::vector<your_struct>
,但这很复杂.
Wrapping a std::vector<your_struct>
in C# is possible with just regular P/Invoke Interop, it is complicated though.
从.NET世界实例化C ++对象的基本思想是从.NET分配C ++对象的确切大小,然后调用从C ++ DLL导出的构造函数来初始化该对象,然后您将能够调用任何函数来访问该C ++对象,如果任何方法涉及其他C ++类,则也需要将它们包装在C#类中,对于具有原始类型的方法,只需简单地P/Invoke它们即可.如果只有几种方法可以调用,那将很简单,手动编码不会花费很长时间.完成C ++对象后,您将调用C ++对象的destructor方法,该方法也是一个导出函数.如果没有,则只需要从.NET中释放内存即可.
The basic idea of instantiating a C++ object from .NET world is to allocate exact size of the C++ object from .NET, then call the constructor which is exported from the C++ DLL to initialize the object, then you will be able to call any of the functions to access that C++ object, if any of the method involves other C++ classes, you will need to wrap them in a C# class as well, for methods with primitive types, you can simply P/Invoke them. If you have only a few methods to call, it would be simple, manual coding won't take long. When you are done with the C++ object, you call the destructor method of the C++ object, which is a export function as well. if it does not have one, then you just need to free your memory from .NET.
这里是一个例子.
public class SampleClass : IDisposable
{
[DllImport("YourDll.dll", EntryPoint="ConstructorOfYourClass", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.ThisCall)]
public extern static void SampleClassConstructor(IntPtr thisObject);
[DllImport("YourDll.dll", EntryPoint="DoSomething", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.ThisCall)]
public extern static void DoSomething(IntPtr thisObject);
[DllImport("YourDll.dll", EntryPoint="DoSomethingElse", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.ThisCall)]
public extern static void DoSomething(IntPtr thisObject, int x);
IntPtr ptr;
public SampleClass(int sizeOfYourCppClass)
{
this.ptr = Marshal.AllocHGlobal(sizeOfYourCppClass);
SampleClassConstructor(this.ptr);
}
public void DoSomething()
{
DoSomething(this.ptr);
}
public void DoSomethingElse(int x)
{
DoSomethingElse(this.ptr, x);
}
public void Dispose()
{
Marshal.FreeHGlobal(this.ptr);
}
}
有关详细信息,请参见下面的链接,
For the detail, please see the below link,
(我是SDK工具的作者)
(I am the author of the SDK tool)