Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Callback function using C calling convention.

459 views
Skip to first unread message

Nicolas Faucher

unread,
Apr 20, 2002, 10:19:24 PM4/20/02
to
Hi,

I hope you can help me on this. I'm calling a unmanaged C DLL from my C#
application. I'm able to call the function correctly. This function take a
pointer to a fonction (like callback functions in Win32). I have declared a
delegate in my code and pass it to the unmanaged C function. My callback is
called some times and after that I'm receiving a NullReference exception. I
searched what's the problem and find that the callback function must use C
calling convention. I searched the documentation to find if there is a way
to specify the calling convention of a delegate (which I suppose is stdcall
by default) but without success. I really think it's the cause of the
exception thrown.

So, how to change the calling convention of a delegate when used in
unmanaged code? If not, what I can do for this? I know I can use Managed
C++ to wrap this DLL but I really appreciate to have a totally C# solution.

Thanks

Eliyahu (Vyacheslav) Biktagirov

unread,
Apr 21, 2002, 4:54:42 AM4/21/02
to
You can specify CallingConvention.CDecl for your callback
delegate

Nicolas Faucher

unread,
Apr 21, 2002, 7:31:20 AM4/21/02
to
Ok, but how? Using a attribute, which one?

"Eliyahu (Vyacheslav) Biktagirov" <el...@clal-ins.co.il> wrote in message
news:06ee01c1e912$2d8cf460$19ef2ecf@tkmsftngxa01...

Eliyahu (Vyacheslav) Biktagirov

unread,
Apr 21, 2002, 10:50:58 AM4/21/02
to
Oh, you are right.

Unfortunately, we are not able to specify the Calling
Convention for
Delegates and C# Class Methods.

If your legacy DLL is used by a number of applications and
cannot be
modified, I suggest you implement a Wrapper DLL with
appropriate Calling
Convention.

Hope this helps!

Jan Kotas [MS]

unread,
Apr 21, 2002, 1:21:28 PM4/21/02
to
The C# does not allow you to specify the calling convention of the callback.
It is just one of the C# limitations. IL, managed C++ and the runtime itself
supports the cdecl calling convention for delegates through
modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) on the
internal Invoke method of the delegate. Run ildasm on a small example in
managed C++ if you want to know the exact syntax.

You can use the following trick if you want to write all your code in C#:

- Create a private placeholder attribute to mark your C# delegates that
needs cdecl calling convention
- Compile your C# to an assembly
- Use the ildasm dissasembler to store the resulting assembly as raw IL.
- Modify the IL to change the calling convention on the delegates marked
with the placeholder attribute
- Use ilasm to compile the modified raw IL again.

The whole process can be automated. You can find example of how to automate
the process in the Shared Source CLI samples. The Shared Source CLI (aka
Rotor) can be downloaded from http://msdn.microsoft.com/net/sscli . The
following files have the interesting stuff about the cdecl calling
convention for delegates:

sscli\docs\techinfo\native_managed_interop.html
sscli\samples\pigui\tk\makefile.inc
sscli\samples\pigui\tk\callconvattribute.cs
sscli\samples\pigui\tk\callconvhack.pl
sscli\samples\pigui\tk\tclnative.cs

You will need perl (e.g. from http://www.activestate.com) if you want to use
the script as is. It should be fairly easy to rewrite callconvhack.pl in any
other language though.

Here are the code snipets if you are on a slow link and don't have time to
download 10+MB Rotor tarball at the moment:

makefile.inc:
--------------------
...
csc /out:myassembly.dll callconvattribute.cs mydelegate.cs ...
ildasm myassembly.dll /out:myassembly.il1
perl callconvhack.pl <myassembly.il1 >myassembly.il2
ilasm /DLL /QUIET myassembly.il2
...
--------------------


mydelegate.cs:
--------------------
...
[CallConvCdecl] internal delegate int MyDelegate(int param);
...
--------------------

callconvattribute.cs:
--------------------
// cdecl calling convetion for delegates - the signature of delegates
// marked with this attribute is changed to cdecl in callconvhack.pl

using System;

[Serializable, AttributeUsage (AttributeTargets.Delegate)]
public sealed class CallConvCdeclAttribute : Attribute
{
public CallConvCdeclAttribute()
{
}
}
--------------------

callconvhack.pl:
--------------------
# change references to CallConvAttribute into
modopt([mscorlib]System.Runtime.CompilerServices.*)

while(<>) {
if (m/\.class .* CallConv.*Attribute/) {
$incallconvattributeimpl=1;
next;
}
if(m/\/\/ end of class CallConv.*Attribute/) {
$incallconvattributeimpl=0;
next;
}

if(m/\.custom instance void CallConv(.*)Attribute/) {
$pendingcallconv = $1;
next;
}

if ($pendingcallconv) {
if(m/.*Invoke\(.*/) {
print
"modopt([mscorlib]System.Runtime.CompilerServices.CallConv" .
$pendingcallconv . ")\n";
$pendingcallconv = "";
}
}

print unless $incallconvattributeimpl;
}
--------------------

This posting is provided "AS IS" with no warranties, and confers no rights.


"Nicolas Faucher" <nicfa...@hotmail.com> wrote in message
news:uwmLrrN6BHA.2360@tkmsftngp04...

Eric Gunnerson [MS]

unread,
Apr 22, 2002, 12:03:30 PM4/22/02
to
If you create a delegate and pass it off to the unmanaged code and it works
a while a then bombs, my guess is that you aren't keeping a reference to the
delegate around, and the GC is coming along and cleaning it up for you.

You can test this by calling GC.Collect() right after you do the delegate.
If you do that, the first call should fail.

You can fix this by keeping a reference to the delegate where the GC can
find it (ie as an instance variable).

--
Visit the C# product team at http://www.gotdotnet.com/team/csharp

This posting is provided "AS IS" with no warranties, and confers no rights.

"Nicolas Faucher" <nicfa...@hotmail.com> wrote in message
news:uwmLrrN6BHA.2360@tkmsftngp04...

0 new messages