Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...