473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

P/Invoke and **struct

Anyone have an example of calling DLL functions that take a pointer to a
pointer to a struct. In C it would be:

some_struct_t *param;
func(&param);

in C#, I've got:

[StructLayout(LayoutKind.Sequential)]
public struct ppStruct
{
public IntPtr ptr;
}
public class libwrapper
{
[DllImport("my.dll")]
public static extern void func([MarshalAs(UnmanagedType.LPStruct)]ppStruct
param);
}

Then I try:

ppStruct param = new ppStruct();
IntPtr ppParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(param));
Marshal.StructureToPtr(param,ppParam,false);
func(param);

Nothing seems to work, I've been looking for a good (working) example.
Nov 15 '05 #1
3 9600


"Gary Desrosiers" <de************@cox.net> wrote in message
news:uh**************@TK2MSFTNGP12.phx.gbl...
Anyone have an example of calling DLL functions that take a pointer to a pointer to a struct. In C it would be:

some_struct_t *param;
func(&param);

in C#, I've got:

[StructLayout(LayoutKind.Sequential)]
public struct ppStruct
{
public IntPtr ptr;
}
public class libwrapper
{
[DllImport("my.dll")]
public static extern void func([MarshalAs(UnmanagedType.LPStruct)]ppStruct param);
}

Then I try:

ppStruct param = new ppStruct();
IntPtr ppParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(param));
Marshal.StructureToPtr(param,ppParam,false);
func(param);

Nothing seems to work, I've been looking for a good (working) example.


Don't marshal the "param" to the func as an LPStruct, pass it as
a ref IntPtr.

[DllImport("my.dll")]
private static extern void func(ref IntPtr structPtr);

....

IntPtr ptr = IntPtr.Zero;
try
{
ppStruct param = new ppStruct();
Marshal.AllocHGlobal(param, ptr, true);

func( ref ptr );

param = Marshal.PtrToStructure(ptr, typeof(ppStruct));
}
finally
{
if( ptr != IntPtr.Zero )
{
Marshal.FreeHGlobal(ptr);
}
}
My only concern is, the only reason to pass a pointer to
a pointer is if you plan on changing the original pointer.

This could lead to a memory leak because what happens
to the original pointer?

In the example I showed you, there could be a critical
flaw if func() changes the pointer because the pointer
returned may not be freeable by Marshal.FreeHGlobal()
and the pointer you originally Alloc'd is unretrievable.

It all depends on exactly what func() is doing.

-c


Nov 15 '05 #2
Thanks!, that worked. The func() actually sets the IntPtr passed to memory
within itself and the kernel and shouldn't be free'd/FreeHGlobal'ed. func()
manages the memory. We just get to look at it. The only thing I should need
to FreeCoTaskMem now is the IntPtr being passed. I'm going to assume that if
I Marshal.PtrToStructure() something being returned, that garbage collection
will eventually remove the stucture referenced. But maybe that's a bad
assumption.

"Chad Myers" <cm****@N0.SP.AM.austin.rr.com> wrote in message
news:uW**************@TK2MSFTNGP10.phx.gbl...

Don't marshal the "param" to the func as an LPStruct, pass it as
a ref IntPtr.

[DllImport("my.dll")]
private static extern void func(ref IntPtr structPtr);

...

IntPtr ptr = IntPtr.Zero;
try
{
ppStruct param = new ppStruct();
Marshal.AllocHGlobal(param, ptr, true);

func( ref ptr );

param = Marshal.PtrToStructure(ptr, typeof(ppStruct));
}
finally
{
if( ptr != IntPtr.Zero )
{
Marshal.FreeHGlobal(ptr);
}
}
My only concern is, the only reason to pass a pointer to
a pointer is if you plan on changing the original pointer.

This could lead to a memory leak because what happens
to the original pointer?

In the example I showed you, there could be a critical
flaw if func() changes the pointer because the pointer
returned may not be freeable by Marshal.FreeHGlobal()
and the pointer you originally Alloc'd is unretrievable.

It all depends on exactly what func() is doing.

-c

Nov 15 '05 #3
Thanks!, that worked. The func() actually sets the IntPtr passed to memory
within itself and the kernel and shouldn't be free'd/FreeHGlobal'ed. func()
manages the memory. We just get to look at it. The only thing I should need
to FreeCoTaskMem now is the IntPtr being passed. I'm going to assume that if
I Marshal.PtrToStructure() something being returned, that garbage collection
will eventually remove the stucture referenced. But maybe that's a bad
assumption.

"Chad Myers" <cm****@N0.SP.AM.austin.rr.com> wrote in message
news:uW**************@TK2MSFTNGP10.phx.gbl...

Don't marshal the "param" to the func as an LPStruct, pass it as
a ref IntPtr.

[DllImport("my.dll")]
private static extern void func(ref IntPtr structPtr);

...

IntPtr ptr = IntPtr.Zero;
try
{
ppStruct param = new ppStruct();
Marshal.AllocHGlobal(param, ptr, true);

func( ref ptr );

param = Marshal.PtrToStructure(ptr, typeof(ppStruct));
}
finally
{
if( ptr != IntPtr.Zero )
{
Marshal.FreeHGlobal(ptr);
}
}
My only concern is, the only reason to pass a pointer to
a pointer is if you plan on changing the original pointer.

This could lead to a memory leak because what happens
to the original pointer?

In the example I showed you, there could be a critical
flaw if func() changes the pointer because the pointer
returned may not be freeable by Marshal.FreeHGlobal()
and the pointer you originally Alloc'd is unretrievable.

It all depends on exactly what func() is doing.

-c

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel Jin | last post by:
I'm an absolute newb to P/Invoke, and got stuck on this little thing, what would be the correct P/Invoke syntax to import the following dll function NTSTATUS TdiRegisterProvider IN...
16
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
0
by: brckcc | last post by:
I have a function in C which takes a pointer to a pointer to a structure. It then returns a linked list. How do I call, via P/Invoke, from C# to C. struct LinkedList { char *value; struct...
1
by: Xia Wei | last post by:
Hi group, I'v got something wrong with P/Invoke. If I have two structs defined like this: typedef struct{ DWORD F1; }A, *PA; typedef struct{ A Data;
0
by: Ffelagund | last post by:
Hello I'm trying to load dinamically some dll's but when I try to invoke one method, I ever get ArgumentException if I'm not using native types. Here are the .h of the dll: #pragma once...
1
by: geri.gan | last post by:
I have C API just like this: enum void getinfor(const struct inputinfor *a, const struct outputinfor ** b) i use p/invok to translate it to internal static extern void getinfor(ref...
19
by: Sharath A.V | last post by:
I had an argument with someone on wheather this piece of code can invoke undefined bahaviour. I think it does not invoke any undefined behaviour since there is sufficient memory space of 9...
5
by: Richard | last post by:
Hello, I'm working on an application to allow our network team to use a small application to make DHCP reservations on our Microsoft DHCP Server. The problem is you have to use P/Invoke to do...
2
by: kevou | last post by:
hi, I'm currently trying to deal with this struct from C lib using Platform invoke in C#. Here is the C struct: typedef struct { short myshort;
2
by: rdilipk | last post by:
I am posting at the end of this post some code that P/Invoke's SetSystemTime to set the local system time. This call fails -- i.e the time is not set and the API returns false. However calling...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.