Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / August 2004

Tip: Looking for answers? Try searching our database.

pinvoke and the Cdecl calling convention

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike - 12 Aug 2004 17:05 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.