C# pinvoke 编组结构包含向量<结构>结构>
我需要为 Windows CE 6.0 项目调用一个函数,该函数返回一个包含 int 和 C# 中其他结构的向量的结构:
该函数由第三方提供商(中国的 pda 制造商)提供,并且他们只给我提供了 .h 文件、dll 和 lib。
我尝试在 C# 中调用的函数在 .h 文件中定义为:
DLLGSMADAPTER ApnInfoData* GetAvailApnList();
ApnInfoData 结构如下:
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 是一个
#define DLLGSMADAPTER _declspec(dllexport)
我的问题是如何在 .net cf 中调用此函数,因为它使用向量类,我不知道如何整理这个。
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:
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.
The function i'm trying to call in C# is defined in the .h file as :
DLLGSMADAPTER ApnInfoData* GetAvailApnList();
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;
the DLLGSMADAPTER is a
#define DLLGSMADAPTER _declspec(dllexport)
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的。 P/Invoke 设计为仅封送 C 类型。您有几个选择:
最基本的 C 包装器将如下所示:
This is not possible. P/Invoke is designed to marshal C types only. You have a few options:
The most basic C wrapper around this would go something like this:
只需使用常规 P/Invoke Interop,就可以在 C# 中包装
std::vector
,但它很复杂。从 .NET 世界实例化 C++ 对象的基本思想是从 .NET 分配 C++ 对象的精确大小,然后调用从 C++ DLL 导出的构造函数来初始化该对象,然后您将能够调用任何访问该 C++ 对象的函数,如果任何方法涉及其他 C++ 类,您还需要将它们包装在 C# 类中,对于具有原始类型的方法,您可以简单地 P/Invoke 它们。如果你只有几个方法需要调用,那会很简单,手动编码不会花很长时间。当您使用完 C++ 对象后,您将调用 C++ 对象的析构函数方法,该方法也是导出函数。如果没有,那么您只需从 .NET 中释放内存即可。
这是一个例子。
有关详细信息,请参阅以下链接,
C#/.NET PInvoke Interop SDK
(我是SDK工具的作者)
Wrapping a
std::vector<your_struct>
in C# is possible with just regular P/Invoke Interop, it is complicated though.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.
Here is an example.
For the detail, please see the below link,
C#/.NET PInvoke Interop SDK
(I am the author of the SDK tool)