Connecting Tech Pros Worldwide Forums | Help | Site Map

Need help with converting C struct containing function pointers to C#

Thomas Connolly
Guest
 
Posts: n/a
#1: Nov 16 '05
Hello list,
No one seems to be replying to my request on the interop group so I thought I would try here.
Can someone please help me with this? I have a C struct containing function pointers like so:

typedef struct _tagLiffeResponseHandlerList
{

void (*OnAccessTransfer) (LiffeStatus eStatus, const char* pszTrader, LiffeBoolean bContinuationFlag,
int nNumOrders, const LiffeOrderEntryList* pcLiffeOrderEntryList, time_t nTimeStamp);

****** other function pointers omitted for brevity *******

} LiffeResponseHandlerList;

Would the following be correct?

public delegate void OnAccessTransfer(LiffeStatus eStatus, [MarshalAs(UnmanagedType.LPStr)] string pszTrader, LiffeBoolean bContinuationFlag, int nNumOrders, ref LiffeOrderEntryList pcLiffeOrderEntryList, uint nTimeStamp);

[StructLayout(LayoutKind.Sequential)]
public class LiffeResponseHandlerList
{
public OnAccessTransfer OnAccessTransfer;
}

Thanks for the help



Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Need help with converting C struct containing function pointers to C#


Thomas,

That looks fine, the only thing is that I think that you ^might^ have to
add the MarshalAs attribute for the pointer itself, like this:

[StructLayout(LayoutKind.Sequential)]
public class LiffeResponseHandlerList
{
[MarshalAs(UnmanagedType.FunctionPtr)]
public OnAccessTransfer OnAccessTransfer;
}

I'm not sure if delegates are marshaled like this by default (although I
would assume they are). Also, if something is setting this structure up,
(assining the function pointer and returning it), I am not sure if .NET will
marshal it back correctly. I think in 2.0 it will do it correctly, while I
am unsure about 1.1 and before (it's easy enough to test).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"Thomas Connolly" <tomc@cqg.com> wrote in message
news:eKF3tvH6EHA.128@TK2MSFTNGP15.phx.gbl...[color=blue]
> Hello list,
> No one seems to be replying to my request on the interop group so I
> thought I would try here.
> Can someone please help me with this? I have a C struct containing
> function pointers like so:
>
> typedef struct _tagLiffeResponseHandlerList
> {
>
> void (*OnAccessTransfer) (LiffeStatus eStatus, const char* pszTrader,
> LiffeBoolean bContinuationFlag,
> int nNumOrders, const LiffeOrderEntryList* pcLiffeOrderEntryList,
> time_t nTimeStamp);
>
> ****** other function pointers omitted for brevity *******
>
> } LiffeResponseHandlerList;
>
> Would the following be correct?
>
> public delegate void OnAccessTransfer(LiffeStatus eStatus,
> [MarshalAs(UnmanagedType.LPStr)] string pszTrader, LiffeBoolean
> bContinuationFlag, int nNumOrders, ref LiffeOrderEntryList
> pcLiffeOrderEntryList, uint nTimeStamp);
>
> [StructLayout(LayoutKind.Sequential)]
> public class LiffeResponseHandlerList
> {
> public OnAccessTransfer OnAccessTransfer;
> }
>
> Thanks for the help
>
>[/color]


Brian Brown
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Need help with converting C struct containing function pointers to C#


Nicholas Paldino [.NET/C# MVP] wrote:[color=blue]
> Thomas,
>
> That looks fine, the only thing is that I think that you ^might^ have to
> add the MarshalAs attribute for the pointer itself, like this:
>
> [StructLayout(LayoutKind.Sequential)]
> public class LiffeResponseHandlerList
> {
> [MarshalAs(UnmanagedType.FunctionPtr)]
> public OnAccessTransfer OnAccessTransfer;
> }
>[/color]

So the million dollar question (For me)... is there any way to do this
in the compact framework, since MarshalAs doesn't exist (among other
things...)?

:)

Brian

[color=blue]
> I'm not sure if delegates are marshaled like this by default (although I
> would assume they are). Also, if something is setting this structure up,
> (assining the function pointer and returning it), I am not sure if .NET will
> marshal it back correctly. I think in 2.0 it will do it correctly, while I
> am unsure about 1.1 and before (it's easy enough to test).
>
> Hope this helps.
>
>[/color]
Bruce Wood
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Need help with converting C struct containing function pointers to C#


The other million-dollar question, Thomas, is are you trying to pass
data back and forth between C and C#, and so need to reproduce the
structure exactly, or are you trying to translate a program from C to
C#, and so want to reproduce the behaviour.

I assume from the fact that you posted to the interop group to start
with that it's the former. If it's the latter then there's probably a
whole other way to go about this in C#.

Closed Thread