Best way to marshal a structure array pointer

I call a function from C ++ that returns a pointer to an array of structures, and I'm having problems since I'm new to this operation / implementation.

My C ++ Codes:

// My C++ Structs
typedef struct _MainData {
 double  dCount;
 DataS1         *DS1;
 int  iCount1;
 DataS2         *DS2;
 int  iCount2;
}MainData;

typedef struct _DataS1 {

 unsigned int uiCount1; 
 unsigned int uiCount2; 
 int  iCount;
 void  *pA; 
 void  *pB; 

} DataS1;

typedef struct _DataS2 {
 unsigned int uiCount1; 
 unsigned int uiCount2;    
 unsigned int uiCount3;    
 unsigned int uiCount4;   
 double  dCount; 
 int  iCount1;     
 char  strLbl[64];
} DataS2;

// My C++ Function
MainData* GetData(const int ID)
{
        MainData* mData;
        int iLength = Get_Count();
        mData = new MainData[iLength];
        for(int x = 0;x < VarCounter; x++)
        {
            // Codes here assign data to mData[x]
        }
        return mData;
}

Question: How can I call the C ++ GetData function in C #?

My current C # codes are:

[DllImport(".\\sdata.dll")]
[return: MarshalAs(UnmanagedType.LPArray)]
private static unsafe extern MainData[] GetData(int ID);

// The struct MainData in my C# side is already "Marshalled"...

//My function call is here:
MainData[] SmpMapData = GetData(ID);

When I compiled it, there is an exception: "Cannot marshal the" return value ": invalid combination of managed / unmanaged types.

Sorry for the bad coding ... Please help ...

+1
source share
2 answers

I tried to do the same, but could not succeed due to lack of time, but I learned something in the process:

, .

0

, , , ++. , ++ - , , , MainData .

#?

++, #, ++/CLI PInvoke.

-1

Source: https://habr.com/ru/post/1728411/


All Articles