Good day,
Perhaps this should be on the Interop newsgroup. Sorry if it should.
I'm trying to PInvoke a function in a native DLL via its ordinal number.
The native C++ function, ordinal number 1, has the declaration:
int __cdecl CalcAverage(int,int,int);
My C# declaration for that function is:
[DllImport("pinvoke_tests.dll", EntryPoint = "#1", CallingConvention =
CallingConvention.Cdecl)]
public static extern int CalcAverage(int first, int second, int third);
When I run the code I get a NullRef exception at the following line of code:
Assert(NativeFunctionWrapper.CalcAverage(-400, -500, -600) == -500);
In inspecting the first few lines of the disassembly for the above
code-line I see:
push 0FFFFFDA8h
mov edx,0FFFFFE0Ch
mov ecx,0FFFFFE70h
call dword ptr ds:[00905510h]
Either I misunderstand the "C" calling convention, or the JIT has missed
my request to use it.
I understand the "C" calling convention to mean:
- args are pushed right to left
- caller must free args
and most importantly,
- all inbound args passed on the stack, none in registers
Any thoughts? If I'm not wrong about the calling convention, how do I
persuade the JIT to use it?
Thanks
Mike
Mattias Sj?gren - 12 Aug 2004 19:19 GMT
Mike,
>In inspecting the first few lines of the disassembly for the above
>code-line I see:
[quoted text clipped - 6 lines]
>Either I misunderstand the "C" calling convention, or the JIT has missed
> my request to use it.
A similar question was posted in the framework newsgroup the other
day. See my answer in this thread.
http://groups.google.com/groups?threadm=ewtMQcyfEHA.4092%40TK2MSFTNGP10.phx.gbl
Basicly you're looking at the stack before the marshaling thunk is
called, not the way it looks when the actual native function is
called.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mike - 13 Aug 2004 08:34 GMT
I didn't think of that!
Thanks a lot
Mike