Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

c# 3.0 - C# pinvoke marshalling structure containg vector<structure>

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...