UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> 49 - Bad DLL Calling Convention    

Error Code: 49
Error Source:
Error Message: Bad DLL Calling Convention

This error is often found when attempting to call a Win32 API or other DLL function from VB/VBA. There are a few causes, all of which have the root cause concerning function argument passing.


Contents

Possible Causes and Resolutions

Datatypes defined in the function arguments in the VB end do not correctly map to the datatypes required by the API declarations.

Double check all API documentation, and choose the VB datatype that correctly maps to the required API datatype. Finding the correct mappings may be difficult, it will require looking at the low-level requirements for the API types as well as the VB types to find the correct matches. Failure to correctly map the datatypes may result in application/system crashes.

Arguments are passed ByRef when they should be ByVal, or vice-versa.

Again, this situation requires an understanding of By Value vs. By Reference argument handling, along with throrough documentation on the API in question so that these can be determinted. Failure to correctly handle the passing method may result in application/system crashes.

The API Library was compiled with non-standard Win32 external declarations in the DLL source.

In some cases, there is nothing we can do to coerce VB to work with a DLL. Depending on the design of the API, it just plain may not work through the VB interface. However, some APIs may have declared external functions using the __stdcall method, and depending on the compiler that handled the source code, we may be able to set the Alias for the function to include the number of bytes that the arguments take:

CODE

Declare Function MyDLLFunction _
 Lib "C:\MyDll.dll" _
 Alias "MyDllFunction@8" _
 (ByVal MyArg1 As Long, _
  ByVal MyArg2 As Long _
 ) As Long

Here we can see that the Alias must include the DLL's internal function name, followed by "@", followed by the total number of bytes required by all arguments of the DLL (in this example case, that value is 8, as each Long = 4 bytes).

In some situations, the Alias must be decorated with an underscore prefix, or with an underscore prefix and suffix:

CODE

Declare Function MyDLLFunction _
 Lib "C:\MyDll.dll" _
 Alias "_MyDllFunction@8_" _
 (ByVal MyArg1 As Long, _
  ByVal MyArg2 As Long _
 ) As Long

If the DEF file that the library was compiled with is available, it will have the exact function name and required decorations that must be called through the Alias declaration in VB. If the file is available, these functions and their decorations will be listed under the EXPORTS heading.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 3,264 times.  This page was last modified 02:35, 30 October 2010 by Jack Leach.   Disclaimers