Click here to Skip to main content
15,900,474 members
Home / Discussions / C#
   

C#

 
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 
GeneralRe: C# for Internet Explorer Pin
sreejith ss nair22-Aug-04 20:24
sreejith ss nair22-Aug-04 20:24 
GeneralDataView.RowFilter question Pin
WillemM22-Aug-04 6:49
WillemM22-Aug-04 6:49 
GeneralRe: DataView.RowFilter question Pin
sreejith ss nair22-Aug-04 21:45
sreejith ss nair22-Aug-04 21:45 
QuestionHow to set fontsize in inch to print exactly Pin
I2A4U22-Aug-04 5:43
I2A4U22-Aug-04 5:43 
AnswerRe: How to set fontsize in inch to print exactly Pin
Werdna23-Aug-04 7:33
Werdna23-Aug-04 7:33 
GeneralRe: How to set fontsize in inch to print exactly Pin
Werdna23-Aug-04 7:34
Werdna23-Aug-04 7:34 

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.