C # line pointers

I have a Windows Forms application in C # that uses a function in C ++. This was done using the C ++ shell. However, the function requires the use of pointers, and C # does not allow the use of pointers with string arrays. What can be done to overcome this? I read about using a marshal, but I'm not sure if this is suitable for this case, and if so, how it should be integrated with my code. Below is the C # code:

int elements = 10; string [] sentence = new string[elements]; unsafe { fixed (string* psentence = &sentence[0]) { CWrap.CWrap_Class1 contCp = new CWrap.CWrap_Class1(psentence, elements); contCp.getsum(); } } 

C ++ function description: funct::funct(string* sentence_array, int sentence_arraysize)

C ++ wrapper: CWrap::CWrap_Class1::CWrap_Class1(string *sentence_array, int sentence_arraysize) { pcc = new funct(sentence_array, sentence_arraysize); } CWrap::CWrap_Class1::CWrap_Class1(string *sentence_array, int sentence_arraysize) { pcc = new funct(sentence_array, sentence_arraysize); }

+6
source share
1 answer

If I understand you correctly, you want to call the C function with a string as parameters.
For this, you usually use PInvoke (invoke platform), which uses sorting at hand.

This example takes a string and returns a string.

 [DllImport(KMGIO_IMPORT,CallingConvention=CallingConvention.Cdecl)] //DLL_EXPORT ushort CALLCONV cFunction(char* sendString, char* rcvString, ushort rcvLen); private static extern UInt16 cFunction(string sendString, StringBuilder rcvString, UInt16 rcvLen); public static string function(string sendString){ UInt16 bufSize = 5000; StringBuilder retBuffer = new StringBuilder(bufSize); cFunction(sendString, retBuffer, bufSize); return retBuffer.ToString(); } 
+1
source

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


All Articles