Function Declarations and CDecl

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all

I am relatively new to C# programming and have run into a problem that I believe is caused by my C# function declarations. I believe that my C# function must use a CDecl CallingConvention but I do not know how to set this

I am attempting to have a C style DLL perform a Callback to a given function in my C# class passing a pointer to a struct. Currently, I have the callback working ( that is I get my expected events ) but the problem is that my variable ( Pointer to Struct ) is always NULL

ex
private unsafe int my_event_handler(SK_Event *ptr_event, void *pvoid_tag

// ptr_event is always NUL
if ( ptr_event != null

Console.WriteLine("Handler: " + ptr_event->Data.ToString())




I have read numerous articles on managed / unmanaged code using the Marshal class etc. and have seen many different combinations of Attributes and directives that can be applied. My question is, is there a way to force your C# function to have a particular CallingConvention ? ...I have seen how to use this with my DLLImportAttribute but have not been able to apply this to a C# method / delegate function. ...Or Is this even possible

Any help would be appreicated

Thanks in advance
Boot
 
Boots,

I don't believe that tis is possible. By default, I believe that
managed callbacks exported by .NET use the STDCALL calling convention. I am
not sure if in the next version this will be addressed.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Boots said:
Hi all,

I am relatively new to C# programming and have run into a problem that I
believe is caused by my C# function declarations. I believe that my C#
function must use a CDecl CallingConvention but I do not know how to set
this.
I am attempting to have a C style DLL perform a Callback to a given
function in my C# class passing a pointer to a struct. Currently, I have
the callback working ( that is I get my expected events ) but the problem is
that my variable ( Pointer to Struct ) is always NULL.
ex:
private unsafe int my_event_handler(SK_Event *ptr_event, void *pvoid_tag)
{
// ptr_event is always NULL
if ( ptr_event != null )
{
Console.WriteLine("Handler: " + ptr_event->Data.ToString());
}

}

I have read numerous articles on managed / unmanaged code using the
Marshal class etc. and have seen many different combinations of Attributes
and directives that can be applied. My question is, is there a way to force
your C# function to have a particular CallingConvention ? ...I have seen
how to use this with my DLLImportAttribute but have not been able to apply
this to a C# method / delegate function. ...Or Is this even possible ?
 
I have read numerous articles on managed / unmanaged code using the
Marshal class etc. and have seen many different combinations of
Attributes and directives that can be applied. My question is, is
there a way to force your C# function to have a particular
CallingConvention ? ...I have seen how to use this with my
DLLImportAttribute but have not been able to apply this to a C#
method / delegate function. ...Or Is this even possible ?

Yes. But it's not so "nice" like adding an Attribute.

See http://groups.google.com/groups?selm=ew6mLkV6BHA.1932@tkmsftngp03 for
details.

Does it work now?

mfg GP
 
Thanx for the reply Gunter, you weren't kidding though - it's not a nice looking solution ! :
I'll try to stumble my way through this and I'll let you know how it goes

Thanx again for your help
 
Back
Top