Skip to content

Trouble help

Cristian Gallardo edited this page Mar 25, 2020 · 5 revisions

For the use of the dynamic link library, we take into account that the .dll file can be next to our executable or in another location, in our case the library is located in the System32 folder.

Errors in the use of DLL in C#

NOTE: The calling convention __Fastcall is not supported in C # of 32bits, for more information check the reference of the official Microsoft page here.

1. Error: In C # when loading the 32-bit library of type calling convention __stdcall gives an error "Cannot find entry point 'function_Name' in DLL"

Solution: It is solved with the declaration of the calling convention and the name of the corresponding function, check the names of the functions according to the calling convention here.

Example for the function "openMemory"

[DllImport (dllPath, EntryPoint = "openMemory@8", CallingConvention = CallingConvention.StdCall)]`
static extern int openMemory (String memoryName, int typeCode);

2. Error: System.BadImageFormatException:

Most likely, the architecture for which visual studio is compiling, does not correspond to the library architecture, Example, we want to use the 64Bit library but we are compiling for "Any CPU" or "x86". Solution: Change to the correct compiler type, if we do not have it added we must follow the following steps: Click on the combo box of architecture types next to "Debug" and choose "Configuration Manager", in the new window that appears click on the combo box under "Platform" and choose "New", in the new window we choose what new support we want to create, for example "x64" and specify from where we want to copy the settings, there we choose "Any CPU". We click OK and then Close. With this we have already added the support and we can choose it in the combo box.

Errors in the use of DLL in Excel

1. Error: Integer-type values are not sent or returned incorrectly.

This is due to an erroneous equivalence, in the library we use the Int type but in Excel we must specify in the call to the function of the .dll that the value is of type Long.

Example for the function "getInt"

In this function both the memory position and the return value are of type Int, but in Excel we must declare as Long (Especially when we work with values of a cell, if we directly send the integer value it could work by declaring Integer in the function , but to avoid problems it is recommended to declare as Long)

X86

Public Declare Function getInt Lib "C:\Windows\SysNative\smClient.dll" Alias "getInt@8" _
(ByVal namMemory As String, ByVal position As Long) As Long

X64

Public Declare PtrSafe Function getInt Lib "C:\Windows\SysNative\smClient.dll" _
(ByVal namMemory As String, ByVal position As Long) As Long

2. Error: In 32bits Excel “Can’t find DLL entry point 'functionName' in xxxxxxx”.

Solution: You must specify the name of the function with the decorator after the keyword Alias.

Example with the function "setFloat"

Public Declare Sub setFloat Lib "C:\Windows\SysNative\smClient.dll" Alias "setFloat@12" _
(ByVal namMemory As String, ByVal position As Long, ByVal value As Single)

3. Error: "Compile error: Syntax error."

Solution: Although it is a very generic error that can have many reasons, one reason may be that functions that have no return should be declared as sub, not as Function, and when we want to call them, we use the reserved word 'call'.

Example of a function that has a return value, is declared as "function"

Public Declare Function getFloat Lib "C:\Windows\SysNative\smClient.dll" Alias "getFloat@8" _
(ByVal namMemory As String, ByVal position As Long) As Single

Example of a function that has no return value, is declared as "sub"

Public Declare Sub setFloat Lib "C:\Windows\SysNative\smClient.dll" Alias "setFloat@12" _
(ByVal namMemory As String, ByVal position As Long, ByVal value As Single)

Example of a function call without return "sub"

Call setFloat(memFloatName, position, valueFloat)

4. Error: "Run-time error 49 - Bad DLL calling convention"

Excel 32bits gives an error when using libraries generated with calling convention of type __cdecl or __fastcall.

There is no solution - 32-bit Excel only supports 32-bit libraries created with the calling conventions __stdcall.

For more information you can consult the official documentation here and here

5. Error: Excel freezes and after a few seconds it closes

Although there are many reasons for this type of behavior, we can list a couple of them.

  • This can happen because we declare that the function inside the library will receive an Integer type parameter and we are taking an integer value from a cell and sending it.

Solution - Although in the library the parameter is declared as Int, in Excel we declare that the parameter is of type Long. (Similar to error number 1).

  • In the library we have a parameter of type pointer char (char * variable), within Excel we define a variable type 'Dim retString As String' but when it is sent to the library it freezes and Excel closes.

Solution - the variable declared as String within Excel must be initialized or declare the size of the String variable.

Dim retString As String * 13

Errors in the use of DLL in C and C++

1.- Error: “violation of access rights during execution at 0x00000000”

This occurs because it cannot find the name of the function, using the 32-bit library with the calling convention __stdcall.

Solution: define the name of the functions depending on the calling convention, check the name of the function in the following table here.

Example of the "openMemory" function with its decorator generated by the calling convention

For __stdcall calling convention

fnOpenMemory = (FN1PTR)GetProcAddress(hInst, "openMemory@8"); 

For __fastcall calling convention

fnOpenMemory = (FN1PTR)GetProcAddress(hInst, "@openMemory@8");

2. Error: "read access violation at …."

This occurs because it cannot automatically detect the calling convention.

Solution: Add __fastcall or WINAPI (fot __stdcall) in the typedef of the function call structure.

For __cdecl calling convention

typedef void(*FNWINTPTR)(char *, int, int); 

For __fastcall calling convention

typedef int(__fastcall *FN1PTR)(char *, int);

For __stdcall calling convention

typedef int(WINAPI *FN1PTR)(char *, int);

3. Error: "The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention"

Like the previous problem, it is a problem of ignorance of the calling convention.

Solution: Specify the corresponding calling convention name, you can check the names of the functions depending of their calling convention here.

Errors in the use of DLL in VisualBasic .NET

1. Error: "Cannot find entry point"

In 32-bit VisualBasic, it is because you cannot find the name of the function.

Solution: We must specify the name of the function with the decorator. You can find the corresponding names for each calling convention here.

OpenMemory function definition example with calling convention __stdcall

Public Declare Function openMemory Lib "C:\Windows\SysNative\smClientStd.dll" Alias "openMemory@8" _
    (ByVal memName As String, ByVal cantidad As Integer) As Integer

2. Error: "unbalanced the stack. This is probably because the PInvoke managed signature does not match the unmanaged target signature. Verify that the call agreement and PInvoke signature settings match the unmanaged target signature."

In 32bit Visual Basic, it is because you do not recognize the calling convention.

Solution: for __cdecl calling convention you must import System.Runtime.InteropServices and when you call the functions do it with the following header. Example function openMemory.

<DllImport(pathDll, CallingConvention:=CallingConvention.Cdecl)>
 Public Function openMemory(ByVal memName As String, ByVal typeCode As Integer) As Integer
 End Function

3. Error: “Invalid calling convention for managed code: must be stdcall, cdecl or thiscall.”

This message appears when we want to use a library that has been compiled under a calling convention not supported by Visual Basic, for example __fastcall is not supported.

Errors in the use of DLL in Python

1. Error: "AttributeError: Function 'xxxxxxx' not found"

In 32-bit python it is because it cannot find the name of the function.

Soluction: You must specify the name of the function with its respective decorator corresponding to each calling convention, check the table of names here.

For 32bits __stdcall convention

setInt = getattr(mydll, 'setInt@12')

1. Error: "Value error: Procedure called with not enough arguments ( xx bytes missing) or wrong calling convention"

In 32-bit python this happens because we load the library with a wrong calling conventions.

Solution: Call according to the calling convention. For __cdecl

cdll.LoadLibrary

For __stdcall

windll.LoadLibrary

NOTE: __thiscall and __fastcall are not added to ctypes because they are not compatible with other compilers in any standard way, for more information here

Errors in the use of DLL in Matlab

1. Error: "Type 'int__fastcall' was not found. Defaulting to type error"

In matlab when calling a type dll we also indicate the load of the extension header file .h, in this header file the type of calling convention is specified, either __fastcall, __cdecl etc.

Solution: Delete the calling convention from the .h header file that is loaded together with the library.

2. Error: "Error using loadlibrary. No supported compiler was found. You can install the freely available MinGW-w64..."

It is likely that in the equipment that we install our Matlab does not have a C/C++ compiler installed, or the compiler that we have in our system is not supported by our Matlab.

Solution: To visualize the status of the compilers recognized in matlab we can use the setup command, in its variants mex -v -setup and mex -setup, to see if it recognizes any compiler in the system. If we want to know the list of compilers supported in our Matlab we can visualize in the folder, for example C:\Program Files\MATLAB\R2018b\bin\win64\mexopts.

We must install any C / C ++ compiler from that supported list.

3. Error: I have Visual Studio 2017 installed but Matlab doesn't recognize it

Visual Studio 2017 sometimes give more problems, this occurs because Visual Studio 2017 often installs the entire IDE but DOES NOT INSTALL the C/C++ compiler, so Matab will recognize Visual Studio 2017 but cannot register it.

Running que command mex -v -setup we can see this output.

Looking for registry setting 'HKLM \ SOFTWARE \ Wow6432Node \ Microsoft \ VisualStudio \ SxS \ VS7' 15.0 ... Yes ('C: \ Program Files (x86) \ Microsoft Visual Studio \ 2017 \ Enterprise \').

And Matlab will try to register it but can't do it.!!

Executing command 'set" vcroot = C: \ Program Files (x86) \ Microsoft Visual Studio \ 2017 \ Enterprise "& for / f" delims = "% a in (' type" C: \ Program Files ( x86) \ Microsoft Visual Studio \ 2017 \ Enterprise \ VC \ Auxiliary \ Build \ Microsoft.VCToolsVersion.default.txt "') do @if exist" C: \ Program Files (x86) \ Microsoft Visual Studio \ 2017 \ Enterprise \ VC \ Tools \ MSVC \% a \ bin \ HostX64 \ x64 \ cl.exe "call echo% vcroot% 
'... No.

Most likely, we have the Visual Studio 2017 IDE installed, but not the C/C++ support, to solve this, we again run the Visual Studio installer and add the "Desktop Development with C++" component. We can verify that C++ support was installed when opening Visual Studio 2017, creating a new project and the option of "Visual C++" should be listed.

Now we will open our Matlab again and you must recognize the Visual Studio 2017 compiler.

Clone this wiki locally