Saturday 14 October 2017

How To Resolve Ms Access - “Bad Dll Calling Convention” (Error 49)



Summary:
Through this post you will acquire the knowledge of the best fixes to resolve this Access Bad DLL Calling Convention Error. So, if you too fetching the same issue then just go through the post and get the best fixes to resolve it perfectly. 


Scenario:

Please help, I have tried all possible ways to load the attached dll library and use it to perform two simple functions, first "PortOpen" and second to "DisplayText", I have attached the user guide which came with the dll file but the code written in C++, i tried the following which got me error "runtime error 49 bad dll calling convention"
Declare Function PortOpen Lib "VFD_DLL.dll" (ByVal PortName As String) As Long
Declare Function DisplayText Lib "VFD_DLL.dll" (ByVal str As String) As Long
Call PortOpen("COM1")

source:

Bad DLL Calling Convention Error 49

Bad DLL calling convention occurs usually when one of the procedures finishes and returns to its calling procedure. However the error message doesn’t make any sense because you haven’t called any DLL. Debugging and steeping to your code will take you get crazy because everything is perfectly fine in this, yet the error keeping popping-up here. 
Here are the issue that encountered after having this issue.

Issue encountered due to Bad DLL Calling Convention Error

Queries with VBA Functions Suddenly Fail

You will see that suddenly your VBA function fails to work. Your queries having the VBA code starts failing.


Compact and Repair doesn't help

This is quiet confusing because it doesn’t make any sense because everything looks fine and compact and repair doesn’t help.

Initial assumption is that the database is somehow get corrupted, which is true up to some extent. Many of you might have also tried creating a new database and then importing their objects into it. it solves the issue upto much extent.

People then resort to creating a new database and importing their objects into it which is time consuming and a pain. It solves the problem, but there's a much easier way.

Possible Solutions for Bad DLL Calling Convention Error

Here are the few option that you can try to fix out this Bad DLL Calling Convention Error. So let’s get started…!

Are any Library References Broken?

The very first thing that you need to check is that whether the library references for your module code are valid or not. 

Go to the VBA IDE> Tools,> References to see the list of referenced libraries.  So watch carefully that none of the checked item written “missing” in front of it.

If so, then fix it and your issue will get resolved.

The Problem Lies with the Compile State of VBA

With the time, the section of the Access database that stores VBA modules code can become bloated and sometime throes strange errors such as Bad DLL calling convention.

So when you compile and run code, VBA leaves behind the old version of the code which is no longer valid, but it is not removed from the database.

Database compact and repair addresses the Jet tables in your database not the code. So, it has no impact when the error arises.


Bad DLL Calling Convention (Error 49) Error Reasons With Their Fixes:

Arguments passed to dynamic-link library (DLL) should match well to those expected by the routine. As, the calling convention works with type, number and ordering of the arguments. This error has following reasons and solutions, so have a look over it.

Your program is calling a routine in a DLL that's being passed the wrong type of arguments.

>>Check out whether the argument type agrees with those specified in the declaration of the routine you are calling.

Your program is calling a routine in DLL which is passes the wrong number of arguments.

>> So, check out that are you passing the same number of arguments indicated in the declaration of the routine your are calling.

Your program is calling a routine in DLL, but not using the StdCall calling convention.

>>If DLL routine expects arguments by value, then make sure ByVal is specified for those arguments in the declaration for the routine.

Conclusion:

Now you can yourself resolve this access  - “Bad Dll Calling Convention” (Error 49). So, try it well if you are well efficient in working with manual fixes.

2 comments:

  1. For anyone who finds this post and is experiencing the same error, I resolved it by removing the reference to the external library(MS Excel, in my case) and then adding it back. It resolved the error message completely. Hope this helps someone!

    ReplyDelete
  2. as an addition to my post, above, after adding the reference back, don't forget to recompile!

    ReplyDelete