Click here to Skip to main content
16,013,207 members
Home / Discussions / C#
   

C#

 
GeneralRe: Problem with Stored Procedure Pin
Heath Stewart22-Jun-04 3:03
protectorHeath Stewart22-Jun-04 3:03 
GeneralRe: Problem with Stored Procedure Pin
Anonymous22-Jun-04 3:29
Anonymous22-Jun-04 3:29 
Generalmarshalling a pointer member in struct from c# Pin
rana7421-Jun-04 14:16
rana7421-Jun-04 14:16 
GeneralRe: marshalling a pointer member in struct from c# Pin
Heath Stewart22-Jun-04 2:56
protectorHeath Stewart22-Jun-04 2:56 
GeneralRe: marshalling a pointer member in struct from c# Pin
rana7422-Jun-04 14:23
rana7422-Jun-04 14:23 
GeneralRe: marshalling a pointer member in struct from c# Pin
Heath Stewart22-Jun-04 15:14
protectorHeath Stewart22-Jun-04 15:14 
GeneralRe: marshalling a pointer member in struct from c# Pin
rana7422-Jun-04 16:28
rana7422-Jun-04 16:28 
GeneralRe: marshalling a pointer member in struct from c# Pin
Heath Stewart22-Jun-04 16:46
protectorHeath Stewart22-Jun-04 16:46 
It's already allocated - why allocate it again? For a reference object, all you need to do is pin it in memory (a value type doesn't need (and can't be) pinned since it will not be moved by the GC, although it could be reclaimed so still use a GCHandle or HandleRef to reference it).

You could try:
// Excuse the names - I don't remember off-hand what you used
[StructLayout(LayoutKind.Sequential)]
internal struct S1
{
  public int x;
  public int y;
}
[StructLayout(LayoutKind.Sequential)]
internal struct S2
{
  [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)]public S1[] arr;
  public int size;
}
This will help the marshaler create the array when marshaling from unmanaged code.

Otherwise, you can use the GC handle to pin the S1[] array in memory till after the P/Invoke method returns:
S1[] arr = new S1[];
// Fill S1
GCHandle handle = GCHandle.Alloc(arr, GCHandleType.Pinned);
S2 s2 = new S2();
s2.arr = (IntPtr)handle; // S2.arr is defined as IntPtr
s2.size = arr.Length; // Probably not required here, but oh well
CallMyPInvokeMethod(ref s2);
// Get new data (if any)
handle.Free();
It's crude, but should hopefully give you a good idea.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: marshalling a pointer member in struct from c# Pin
rana7422-Jun-04 21:03
rana7422-Jun-04 21:03 
GeneralRe: marshalling a pointer member in struct from c# Pin
Heath Stewart22-Jun-04 21:06
protectorHeath Stewart22-Jun-04 21:06 
GeneralActive Directory & Multiple Connections Pin
inyoursadachine21-Jun-04 14:02
inyoursadachine21-Jun-04 14:02 
GeneralRe: Active Directory & Multiple Connections Pin
Heath Stewart22-Jun-04 2:51
protectorHeath Stewart22-Jun-04 2:51 
GeneralEncoding Pin
eggie521-Jun-04 12:39
eggie521-Jun-04 12:39 
GeneralRe: Encoding Pin
Christian Graus21-Jun-04 14:29
protectorChristian Graus21-Jun-04 14:29 
GeneralRe: Encoding Pin
eggie521-Jun-04 18:21
eggie521-Jun-04 18:21 
GeneralRe: Encoding Pin
Christian Graus21-Jun-04 18:23
protectorChristian Graus21-Jun-04 18:23 
GeneralRe: Encoding Pin
eggie522-Jun-04 8:54
eggie522-Jun-04 8:54 
GeneralRe: Encoding Pin
Steven Campbell21-Jun-04 15:39
Steven Campbell21-Jun-04 15:39 
GeneralRe: Encoding Pin
Heath Stewart22-Jun-04 2:48
protectorHeath Stewart22-Jun-04 2:48 
GeneralRe: Encoding Pin
eggie514-Sep-04 5:49
eggie514-Sep-04 5:49 
GeneralDataSet - saving rows that are calculated Pin
myNameIsRon21-Jun-04 11:24
myNameIsRon21-Jun-04 11:24 
GeneralRe: DataSet - saving rows that are calculated Pin
Dave Kreskowiak21-Jun-04 11:42
mveDave Kreskowiak21-Jun-04 11:42 
GeneralRe: DataSet - saving rows that are calculated Pin
myNameIsRon22-Jun-04 13:54
myNameIsRon22-Jun-04 13:54 
GeneralRe: DataSet - saving rows that are calculated Pin
myNameIsRon22-Jun-04 17:41
myNameIsRon22-Jun-04 17:41 
GeneralDirectoryServices ADSI Provider Support Pin
Anonymous21-Jun-04 11:13
Anonymous21-Jun-04 11:13 

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.