AutoHotkey Community

It is currently May 27th, 2012, 1:38 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: DllCall parameter error
PostPosted: September 11th, 2005, 5:37 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
When a function in a DLL has a 64 bit integer parameter not in the last position of the parameter list, the DLL call fails with access violation. I experienced this with f(str,Int64,Int64) and f(str,Int64,str) parameter types. If the Int64 parameter is the last one, everything seems to be OK, like in f(str,Int64) or f(str,Int,Int64). All these functions are "CDecl".


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2005, 2:08 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
I couldn't reproduce it. Perhaps the DLL function in question is expecting something other than a pure __int64 coming in. Some functions might expect a 4-byte pointer to an 8-byte structure containing the 64-bit value.

Here is the test I ran:

Define a function in a DLL file:
Code:
extern "C" __declspec(dllexport) __int64 TestInt64(int i1, __int64 i2, __int64 i3)
{
   return i1 + i2 + i3;
}

Call the above function from a script:
Code:
TestVar = 4
result := DllCall("TestDLL\TestInt64", int, 2, int64, 0x0FFFFFFFFFFFFFF0, int64, TestVar, "CDecl Int64")
if ErrorLevel
   MsgBox ErrorLevel = %ErrorLevel%
else if result <> 0x0FFFFFFFFFFFFFF6
   MsgBox Unexpected result: %result%


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2005, 2:49 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
I wrote an AHK function library to interface to GMP. The DLL is downloaded from http://www.cs.nyu.edu/exact/core/gmp/, but seeing the errors I recompiled it, with the same results. The bad functions don't return a value. Here is the list of the three DllCalls causing access violation:
Code:
DllCall("gmp\__gmpz_powm_ui","Str",z,"Str",b,"UInt64", I,"Str",m,"CDecl")
DllCall("gmp\__gmpz_ui_pow_ui","Str",z,"Uint64",U,"UInt64",V,"CDecl")
DllCall("gmp\__gmpz_bin_uiui","Str",z,"UInt64",U,"UInt64",V,"CDecl")
Most of the other long integer functions work. If I replace the offending (not the last) UInt64 types with UInt, there is no more access violation. It is hard to believe that the DLL is wrong, because so many people access it from their C programs. Still, is there a way to verify the correctness of the DLL?

In the source code (gmp.h) the declaration is OK:
Code:
void __GMP_DECLSPEC mpz_pow_ui _PROTO ((mpz_ptr, mpz_srcptr, unsigned long int));
void __GMP_DECLSPEC mpz_powm_ui _PROTO ((mpz_ptr, mpz_srcptr, unsigned long int, mpz_srcptr));
Can it be, that "Str" for pointer parameters causing the trouble?

(Just thinking... The source code is for GCC, a number of changes are necessary for MS Visual Studio. Maybe, those replacements of "unsigned long int" with "__UINT64" broke somewhere. Can we tell from the DLL what the actual type of a function parameter is?)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2005, 8:50 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
I don't know how to tell the type of a parameter just by looking at a compiled DLL file. In fact, I don't know how to find out anything at all about a function's parameters unless you have the source (except through trial and error).

I would guess that the function expects 32-bit integers rather than 64-bit. If that's not it, perhaps the functions are not CDecl after all, though I'm not sure if that would explain why ErrorLevel indicates access violation.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2005, 11:02 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
I am confused. If there is no way to tell the type (or at least the size) of the parameters, how could AHK set ErrorLevel An? I changed all the UInt64 parameter types to UInt, which makes now the total length of the parameters 4 or 8 bytes shorter. Everything seems to work fine, but I was expecting either before or now these An ErrorLevels.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 13th, 2005, 2:32 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Laszlo wrote:
If there is no way to tell the type (or at least the size) of the parameters, how could AHK set ErrorLevel An?
In the fineprint of the documentation, it states, "a Cdecl function never produces the An error". The reason is that the program cannot detect how many parameters a Cdecl function requires. By contrast, a non-Cdecl function reveals the total size of its parameters when it returns to its caller, which the program detects and reports via An.

So I guess for a Cdecl function, if you don't know the number or type of parameters to pass, the only clue you get from the program is whether an exception such as access violation was generated. Of course this is a poor clue compared to the An ErrorLevel.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 4 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group