Click here to Skip to main content
15,440,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,

I am Passing a structure array from C# to (written in c++)com dll,
I am passing as ref a[1], array contains multiple values(a[2],a[3] contains data), while i am reading the data from com dll its only giving a[1] data if i try to read a[2] it is giving some garbage value.

Note: i allocated the memory using new operator( (structure type) []a = new (structure type) [5];)

Ex: int []a = new int[5];

Can anyone help me how to pass data from C# to com dll.

What I have tried:

I have tried by passing it as function(a[1], selecteditem, 0)

Above function is com dll loaded into c# by reference, for that function i need to pass the data from c#.
Updated 6-Sep-16 23:40pm

The primary problem is the memory layout in the different runtime, so my first and hard coded advice is to use only native values (int, double and char[]) in such structures. You cant transfer objects!!!
The second is to transfer read-only, that mean that each runtime copies the values one by one.

Read my article for such interfaces und dig into the source. You havent provided enough details of your structure.
Share this answer
Vijay533 29-Jul-16 7:21am     CRLF
Thanks for the reply karstenk, Following are the details related to code. typedef struct tagOFFICEITEM { BSTR MS_TITLE; BSTR MS_AUTHOR; BSTR MS_COMPANY; BSTR MS_DATE_CREATED; BSTR MS_DATE_MODIFIED; BSTR MS_TIME_MODIFIED; BSTR MS_FROM; BSTR MS_TO; BSTR MS_CC; BSTR MS_BCC; BSTR MS_SUBJECT; BSTR MS_SENT; BSTR MS_TIME_SENT; BSTR MS_RECEIVED; BSTR MS_TIME_RECEIVED; BSTR MS_TIME_CREATED; BSTR Name; BSTR Attachments; long bReturn; } OFFICEITEM; This is my strucure. I am developing vsto adding using C#, From the c# i need to pass the above structure values to the COM dlls(written in C++). Below is the function i am using in c++ passMsOffice(tagOFFICEITEM* docs, LONG nTotalItems,LONG hWnd (not rewured this is 0))
Please see this Blog
Modifying an array of structures (default marshaling)[^]

hope it helps.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900