C# pinvoke 编组结构包含向量<结构>

发布于 2024-10-13 04:01:24 字数 752 浏览 5 评论 0原文

我需要为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-10-20 04:01:24

这是不可能的。 P/Invoke 设计为仅封送 C 类型。您有几个选择:

  • 使用 C++/CLI 围绕 C 库构建托管包装器,然后从 C# 使用它
  • 围绕 C++ 类型编写 C 包装器,然后 P/调用 C 包装器
  • 围绕 C++ 类型编写 COM 包装器,然后然后生成一个 com-interop 存根。

最基本的 C 包装器将如下所示:

// creates/loads/whatever your vector<ApnInfo> and casts it to void*, and then returns it through 'handle'
int GetAppInfoHandle(void **handle);

// casts handle back to vector<ApnInfo> and calls .size()
int GetAppInfoLength(void *handle);   

// Load into 'result' the data at ((vector<ApnInfo>*)handle)[idx];
void GetAppInfo(void *handle, int idx, ApnInfo *result);  

This is not possible. P/Invoke is designed to marshal C types only. You have a few options:

  • Use C++/CLI to build a managed wrapper around your C library and then use it from C#
  • Write a C wrapper around your C++ types and then P/Invoke the C wrapper
  • Write a COM wrapper around the C++ types and then generate a com-interop stub.

The most basic C wrapper around this would go something like this:

// creates/loads/whatever your vector<ApnInfo> and casts it to void*, and then returns it through 'handle'
int GetAppInfoHandle(void **handle);

// casts handle back to vector<ApnInfo> and calls .size()
int GetAppInfoLength(void *handle);   

// Load into 'result' the data at ((vector<ApnInfo>*)handle)[idx];
void GetAppInfo(void *handle, int idx, ApnInfo *result);  
趁年轻赶紧闹 2024-10-20 04:01:24

只需使用常规 P/Invoke Interop,就可以在 C# 中包装 std::vector,但它很复杂。

从 .NET 世界实例化 C++ 对象的基本思想是从 .NET 分配 C++ 对象的精确大小,然后调用从 C++ DLL 导出的构造函数来初始化该对象,然后您将能够调用任何访问该 C++ 对象的函数,如果任何方法涉及其他 C++ 类,您还需要将它们包装在 C# 类中,对于具有原始类型的方法,您可以简单地 P/Invoke 它们。如果你只有几个方法需要调用,那会很简单,手动编码不会花很长时间。当您使用完 C++ 对象后,您将调用 C++ 对象的析构函数方法,该方法也是导出函数。如果没有,那么您只需从 .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);
    }
}

有关详细信息,请参阅以下链接,

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.

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,

C#/.NET PInvoke Interop SDK

(I am the author of the SDK tool)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文