Click here to Skip to main content
15,913,467 members
Home / Discussions / C#
   

C#

 
GeneralRe: Best way to calculate hash Pin
Heath Stewart23-Aug-04 6:30
protectorHeath Stewart23-Aug-04 6:30 
GeneralRe: Best way to calculate hash Pin
Radoslav Bielik23-Aug-04 8:53
Radoslav Bielik23-Aug-04 8:53 
GeneralSorting an Arraylist Pin
malik1022-Aug-04 19:59
malik1022-Aug-04 19:59 
GeneralRe: Sorting an Arraylist Pin
sreejith ss nair22-Aug-04 20:11
sreejith ss nair22-Aug-04 20:11 
Generalproblem implementing special gradient Pin
obby22-Aug-04 19:52
obby22-Aug-04 19:52 
GeneralRe: problem implementing special gradient Pin
leppie22-Aug-04 21:03
leppie22-Aug-04 21:03 
GeneralProblem in HTTP POST Pin
khchan22-Aug-04 18:49
khchan22-Aug-04 18:49 
GeneralRe: Problem in HTTP POST Pin
LongRange.Shooter23-Aug-04 7:40
LongRange.Shooter23-Aug-04 7:40 
GeneralWinForms -> Submit (POST) to a web page and get results Pin
theoutlander22-Aug-04 18:36
theoutlander22-Aug-04 18:36 
GeneralRe: WinForms -> Submit (POST) to a web page and get results Pin
Mazdak22-Aug-04 19:30
Mazdak22-Aug-04 19:30 
GeneralProfessional database advice Pin
steve_rm22-Aug-04 18:03
steve_rm22-Aug-04 18:03 
GeneralRe: Professional database advice Pin
Mazdak22-Aug-04 19:18
Mazdak22-Aug-04 19:18 
GeneralRe: Professional database advice Pin
steve_rm23-Aug-04 5:25
steve_rm23-Aug-04 5:25 
GeneralMarshalling struct with array member Pin
rana7422-Aug-04 15:41
rana7422-Aug-04 15:41 
I have 2 sample structures in the unmanaged code(c++ dll)

typedef struct
{
char* lpszString1,
long ulField1;
}SubStruct;


typedef struct
{
int szlen;
SubStruct* arrsubstruct;
}arrstruct;

And I declare a method in the unmanaged dll

/*
*Method Structwithstructarray will print out the contents
* the members of the array sent from managed code and
* and reassign the array to point to a new set of element.
*/
void Structwithstructarray(arrstruct* mstruct)
{

cout << " Structwithstructarray "<< endl;
SubStruct* oldmanagedarray = mstruct->arrsubstruct;
for(int elem=0;elem<mstruct->szlen;elem++)
cout << oldmanagedarray[elem].lpszString1 << endl;


SubStruct* newunmanagedarray=new SubStruct[4];
newunmanagedarray[0].lpszString1="UnManaged0";
...
....

mstruct->szlen=4;
mstruct->arrsubstruct=newunmanagedarray;
}

The corresponding definitions in managed code

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct submanagedStructure
{
public string sx;
public long szlen;
public submanagedStructure(string _sx,long _szlen)
{
sx=_sx;
szlen=_szlen;
}
}

structure 2:

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct arrStructure
{
public int szlen;
public IntPtr mbuf;
}

[DllImport("........dll")]
public static extern int Structwithstructarray(ref arrStructure ama);

Now I invoke the function in the following sequence

//Populate structure members to send to unmanaged
submanagedStructure[] arrmanagedStructure = new submanagedStructure[3];
arrmanagedStructure[0]=new submanagedStructure("submanagedstructure1",15);
.....
....


arrStructure MyStruct = new arrStructure();
MyStruct.szlen=arrmanagedStructure.Length;
MyStruct.mbuf = Marshal.AllocCoTaskMem(
Marshal.SizeOf(arrmanagedStructure[0]) * arrmanagedStructure.Length
);

//Copy each structure into our allocated block
int iCurOffset = 0;
foreach(submanagedStructure item in arrmanagedStructure)
{
Marshal.StructureToPtr(
item,
(IntPtr)(MyStruct.mbuf.ToInt32() + iCurOffset) ,
false );
iCurOffset += Marshal.SizeOf(arrmanagedStructure[0]) ;

}

//Finally send to unmanaged module
Structwithstructarray(ref MyStruct);

//retrieve the returned structure elements
int outval = MyStruct.szlen;
IntPtr rudt=MyStruct.mbuf;
submanagedStructure[] recvmanagedarray = new submanagedStructure[outval];

for ( int i = 0 ; i < outval; i++ )
{
recvmanagedarray[i] = (submanagedStructure)Marshal.PtrToStructure (
(IntPtr )rudt, typeof ( submanagedStructure ) );
Console.WriteLine("The string element {0} of the struct {1} ",i,recvmanagedarray[i].sx );
rudt = (IntPtr)((int)rudt + Marshal.SizeOf ( typeof ( submanagedStructure ) ));
}

Now I find that just the 1 structure instance gets marshalled through the IntPtr into the unmanaged module.

So.in the unmanaged function
for(int elem=0;elem<mstruct->szlen;elem++)
cout << oldmanagedarray[elem].lpszString1 << endl;

It prints out the first structure and gives a null reference exception thereafter.(Why do the other 2 do not get marshalled?)

Similarly when the structure reference returns,the line "Console.WriteLine("The string element ....." prints out UnManaged0 and UnManaged2.
What happened to the structure elements UnManaged1 and UnManaged3?

Please advise.
GeneralRe: Marshalling struct with array member Pin
leppie23-Aug-04 3:51
leppie23-Aug-04 3:51 
QuestionHow to export data from datagrid to excel? Pin
tzewei22-Aug-04 14:15
tzewei22-Aug-04 14:15 
AnswerRe: How to export data from datagrid to excel? Pin
Jay Shankar22-Aug-04 16:49
Jay Shankar22-Aug-04 16:49 
GeneralRe: How to export data from datagrid to excel? Pin
tzewei22-Aug-04 20:29
tzewei22-Aug-04 20:29 
GeneralRe: How to export data from datagrid to excel? Pin
tzewei22-Aug-04 22:02
tzewei22-Aug-04 22:02 
GeneralRe: How to export data from datagrid to excel? Pin
Jay Shankar24-Aug-04 0:33
Jay Shankar24-Aug-04 0:33 
GeneralDataColumn question Pin
blankg22-Aug-04 10:14
blankg22-Aug-04 10:14 
GeneralRe: DataColumn question Pin
sreejith ss nair22-Aug-04 18:56
sreejith ss nair22-Aug-04 18:56 
GeneralRe: DataColumn question Pin
blankg22-Aug-04 21:12
blankg22-Aug-04 21:12 
GeneralRe: DataColumn question Pin
sreejith ss nair22-Aug-04 21:37
sreejith ss nair22-Aug-04 21:37 
GeneralC# for Internet Explorer Pin
pmasknguyen22-Aug-04 7:11
pmasknguyen22-Aug-04 7:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.