Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing C# structure with string array to c++ function which accepts void * for c# structure and char** for c# string array
    primarykey
    data
    text
    <p>I want to send a <strong>C# structure with string array to a C++ function</strong> which accepts void * for the c# structure and char** for the c# structure string array member.</p> <p>I was able to send the structure to c++ function,but the issue is , <strong>not able to access the string array data member of c# structure from c++ function</strong>. When sending the string array separately,i was able to access the array elements.</p> <p>Sample code is-</p> <pre><code>C# Code: [StructLayout(LayoutKind.Sequential)] public struct TestInfo { public int TestId; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public String[] Parameters; } [DllImport("TestAPI.dll", CallingConvention = CallingConvention.StdCall, EntryPoint "TestAPI")] private static extern void TestAPI(ref TestInfo data); static unsafe void Main(string[] args) { TestInfo testinfoObj = new TestInfo(); testinfoObj.TestId = 1; List&lt;string&gt; names = new List&lt;string&gt;(); names.Add("first"); names.Add("second"); names.Add("third"); testinfoObj.Parameters=names.ToArray(); TestAPI(ref testinfoObj); } VC++ Code: /*Structure with details for TestInfo*/ typedef struct TestInfo { int TestId; char **Parameters; }TestInfo_t; //c++ function __declspec(dllexport) int TestAPI(void *data) { TestInfo *cmd_data_ptr= NULL; cmd_data_ptr = (TestInfo) data; printf("ID is %d \r\n",cmd_data_ptr-&gt;TestId);//Working fine for(i = 0; i &lt; 3; i++) printf("value: %s \r\n",((char *)cmd_data_ptr-&gt;Parameters)[i]);/*Error-Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt*/ } </code></pre> <p>When analyzing the memory stack, it is observed that,when i print ((char *)cmd_data_ptr->Parameters), the first array element("first") is getting printed, but using ((char *)cmd_data_ptr->Parameters)[i], not able access elements and above mentioned exception is coming.</p> <p>The structure memory address contains address of all the structure elements,but while accessing the data from c++,it is accessing only the first element of the string array.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload