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 / .NET SDK / September 2003

Tip: Looking for answers? Try searching our database.

DllImportAttribute CallingConvention

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mr.Tickle - 19 Sep 2003 16:40 GMT
I am calling an unmanaged DLL like follows in a Class wrapper.

namespace blah
{
 [DllImport("DLLName.DLL", EntryPoint="BlahEntryPoint",
CallingConvention=CallingConvention.ThisCall]
 public static extern void blahFn(out long blah, out long blah2);

 class sealed ClassBlah
{
   private ClassBlah()
   {
    }

}
}

Why have I to call this  with the CallingConvention of ThisCall, its just
normal "C" functions in a DLL. Why wont StdCall work?

What happens with other calling conventions is that the out parameters are
NOT set, yet with ThisCall, they are set ok, or appear to be. I find it odd.

Thanks
Nicholas Paldino [.NET/C# MVP] - 19 Sep 2003 17:39 GMT
Mr. Tickle,

   What is the declaration of the original function?  If it takes a
pointer, then you should be using "ref" for the parameters, and not out.
Also, does the function take a 64-bit integer?  Long in C is a 32-bit
integer, where in .NET it is a 64-bit integer.

   The STDCALL mechanism should work if you are using it.

   Hope this helps.

Signature

              - Nicholas Paldino [.NET/C# MVP]
              - nicholas.paldino@exisconsulting.com

> I am calling an unmanaged DLL like follows in a Class wrapper.
>
[quoted text clipped - 20 lines]
>
> Thanks
Mr.Tickle - 19 Sep 2003 18:40 GMT
Nop, I tried ref and it doesnt help, the only way i can get it to return
values is with thiscall.

> Mr. Tickle,
>
[quoted text clipped - 36 lines]
> >
> > Thanks
Mr.Tickle - 19 Sep 2003 18:41 GMT
So i should use Int32 for the parameters instead of long?

> Mr. Tickle,
>
[quoted text clipped - 36 lines]
> >
> > Thanks
Mr.Tickle - 19 Sep 2003 18:42 GMT
I also have a problem with "ref" paramaters, it says they need to be
assigned to before used

Yet i have them used as "ref" in dll extern calls and checked for non NULL
after yet it complains for non assignment so i use a local copy.

Isnt it smart enough to detect that its being assinged in an extern dll call
as declared in DllImport?

> Mr. Tickle,
>
[quoted text clipped - 36 lines]
> >
> > Thanks
Mr.Tickle - 19 Sep 2003 18:46 GMT
i mean "out" oops.

ref in the same dll wrapper still gets the output but only with thiscall
convention.

> I also have a problem with "ref" paramaters, it says they need to be
> assigned to before used
[quoted text clipped - 47 lines]
> > >
> > > Thanks
Nicholas Paldino [.NET/C# MVP] - 19 Sep 2003 19:16 GMT
   If you could post the original function declaration, it would help
tremendously.

Signature

              - Nicholas Paldino [.NET/C# MVP]
              - nicholas.paldino@exisconsulting.com

> i mean "out" oops.
>
[quoted text clipped - 56 lines]
> > > >
> > > > Thanks
Mr.Tickle - 19 Sep 2003 20:28 GMT
It would but I dont have it, its in a dll i have to call into. Not mine.

>     If you could post the original function declaration, it would help
> tremendously.
[quoted text clipped - 66 lines]
> > > > >
> > > > > Thanks
Mr.Tickle - 19 Sep 2003 20:30 GMT
typedef SOME_ID long

SOME_ID someFn(SOME_ID* someIDhere, SOME_ID* someIDhere2, SOME_ID*
someIDhere3);

something like that.

>     If you could post the original function declaration, it would help
> tremendously.
[quoted text clipped - 66 lines]
> > > > >
> > > > > Thanks
Nicholas Paldino [.NET/C# MVP] - 19 Sep 2003 21:35 GMT
Mr.Tickle,

   In this case, you definitely want to use the int type, and pass it by
reference.  Your declaration should look like this:

[DllImport("somedll.dll")]
public static extern int someFn(ref int someIDhere, ref int someIDhere2, ref
int someIDhere3);

Signature

              - Nicholas Paldino [.NET/C# MVP]
              - nicholas.paldino@exisconsulting.com

> typedef SOME_ID long
>
[quoted text clipped - 77 lines]
> > > > > >
> > > > > > Thanks
Mr.Tickle - 19 Sep 2003 22:25 GMT
But I still dont understand why Thiscall works and the others dont.

> Mr.Tickle,
>
[quoted text clipped - 98 lines]
> > > > > > >
> > > > > > > Thanks
Willy Denoyette [MVP] - 20 Sep 2003 12:04 GMT
Because in you definition you only pass two arguments, while you need tree (an int, and to poiters).
It works while using the thiscall calling convention, because the interop layer passes as first argument an int for the this pointer
(three int's are reserved on the stack).

Willy.

> But I still dont understand why Thiscall works and the others dont.
>
[quoted text clipped - 106 lines]
> > > > > > > >
> > > > > > > > Thanks
Mr.Tickle - 20 Sep 2003 12:42 GMT
Well its suppost to match up, its just something i typed in a hurry but it
is the same number of arguments in the definition and call. on teh real
code.

the dll definition would be

typedef SOMEVAL long
typedef SOME_ID long

SOME_ID someFn(SOMEVAL someVal, SOME_ID* someIDhere, SOME_ID* someIDhere2,
SOME_ID* someIDhere3);

so im calling with for example out of my memory

DllImport("somedll.dll", EntryPoint="blahFn",
CallingConvention=CallingConvention.ThisCall)]
public static extern int someFn(long someVal, ref someIDhere, ref long
someIDhere2, ref long someIDhere3);
// i know this should be int instead of long but curious why the long works
with thiscall and not the others or just not work at all.

long someVal;
long ID someID,
      anotherID,
      yetAnohterID,
      andAnotherID;

someID = blahFn(someVal, ref anotherID, ref yetAnotherID, ref andAnotherID);

Only thiscall works, others dont, so if i understand it right I change the
definition from long to int for 32bit instead of the 64bit on the .net side,
and then i can use stdcall as normal.

> Because in you definition you only pass two arguments, while you need tree (an int, and to poiters).
> It works while using the thiscall calling convention, because the interop layer passes as first argument an int for the this pointer
[quoted text clipped - 112 lines]
> > > > > > > > >
> > > > > > > > > Thanks

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.