How To Add a Dll TO MFC Application in visual Studio
-
What is it that you don't understand? How to code the function calls, or how to include the library in your project? What does the documentation say that came with the library?
i had tried this: #import "mcl_gen.dll" in my .h file and i have written this piece of code in my .cpp file: " typedef int (*ConnectByAddressfuncPtr)(short Addr); HINSTANCE LoadME; LoadME = LoadLibrary("mcl_gen.dll"); // Check to see if the library was loaded successfully if (LoadME != 0) printf("LoadMe library loaded!\n"); else printf("LoadMe library failed to load!\n"); ConnectByAddressfuncPtr LibMainConnectByAddress; LibMainConnectByAddress = (ConnectByAddressfuncPtr)GetProcAddress(LoadME,"ConnectByAddress"); int x = LibMainConnectByAddress(0x01); " "ConnectByAddress" is a function in .dll file when i run the code although the dll file is being loaded but i get this error: Unhandled exception at 0x770115ee in fdll.exe: 0xC0000005: Access violation.
-
i had tried this: #import "mcl_gen.dll" in my .h file and i have written this piece of code in my .cpp file: " typedef int (*ConnectByAddressfuncPtr)(short Addr); HINSTANCE LoadME; LoadME = LoadLibrary("mcl_gen.dll"); // Check to see if the library was loaded successfully if (LoadME != 0) printf("LoadMe library loaded!\n"); else printf("LoadMe library failed to load!\n"); ConnectByAddressfuncPtr LibMainConnectByAddress; LibMainConnectByAddress = (ConnectByAddressfuncPtr)GetProcAddress(LoadME,"ConnectByAddress"); int x = LibMainConnectByAddress(0x01); " "ConnectByAddress" is a function in .dll file when i run the code although the dll file is being loaded but i get this error: Unhandled exception at 0x770115ee in fdll.exe: 0xC0000005: Access violation.
-
Well that just indicates that you have a bad address somewhere in the code. You need to use your debugger to find out where it occurs and why.
you mean that i have loaded .dll file correctly and the error is because of somewhat like syntax error? but i have exactly copied the name of function in dll. i dont know why this error occurs
-
Hello everybody i have a mfc application project in Visual studio 2010. it is a GUI infact. i want to get communication with a mini circuit signal generator device. the device has a dll file named "mcl_gen64.dll i want to use functions of that dll in my code but i dont have any idea how to do this any idea please?
To use functions from a DLL you have two choices: Early and late binding. Early binding: Link your application with the DLL by using the
#import
directive in one file (usually a source file using functions from the DLL) specifying the file name (usually without extension or with .lib), or add the .lib file to your project settings (Linker - Input). Include the header file and call the functions defined in that file. Late binding: This is only necessary if the DLL might not be present when your application is executed or you do not have a .lib file. Then useLoadLibrary
andGetProcAddress
like in your above example. But check both return values to be notNULL
. Have a look at the header file (if present), to know how to define the function prototypes. Example:typedef int (__stdcall *ConnectByAddressfuncPtr)(short Addr);
ConnectByAddressfuncPtr LibMainConnectByAddress = NULL;
HMODULE hLib = LoadLibrary(_T("mcl_gen.dll"));
if (hLib)
LibMainConnectByAddress = (ConnectByAddressfuncPtr)GetProcAddress(hLib,"ConnectByAddress");
if (LibMainConnectByAddress)
LibMainConnectByAddress(0x01);Note that I have initialised the function pointer with
NULL
and checked it before calling the function. Note also the__stdcall
in the prototype declaration. It defines the calling convention used by the DLL. You have to check which is used (by inspecting the header file or asking the provider).__stdcall
is common but it might be also__cdecl
. -
you mean that i have loaded .dll file correctly and the error is because of somewhat like syntax error? but i have exactly copied the name of function in dll. i dont know why this error occurs
I have no idea why the error occurred, beyond saying that is is caused by a bad address reference somewhere. And the only way to find out where it occurred is by using the debugger. Are you certain that the parameter
0x01
that you send to theConnectByAddress
function is valid? Check the documentation to see what that function is supposed to do, and what it is supposed to return. -
To use functions from a DLL you have two choices: Early and late binding. Early binding: Link your application with the DLL by using the
#import
directive in one file (usually a source file using functions from the DLL) specifying the file name (usually without extension or with .lib), or add the .lib file to your project settings (Linker - Input). Include the header file and call the functions defined in that file. Late binding: This is only necessary if the DLL might not be present when your application is executed or you do not have a .lib file. Then useLoadLibrary
andGetProcAddress
like in your above example. But check both return values to be notNULL
. Have a look at the header file (if present), to know how to define the function prototypes. Example:typedef int (__stdcall *ConnectByAddressfuncPtr)(short Addr);
ConnectByAddressfuncPtr LibMainConnectByAddress = NULL;
HMODULE hLib = LoadLibrary(_T("mcl_gen.dll"));
if (hLib)
LibMainConnectByAddress = (ConnectByAddressfuncPtr)GetProcAddress(hLib,"ConnectByAddress");
if (LibMainConnectByAddress)
LibMainConnectByAddress(0x01);Note that I have initialised the function pointer with
NULL
and checked it before calling the function. Note also the__stdcall
in the prototype declaration. It defines the calling convention used by the DLL. You have to check which is used (by inspecting the header file or asking the provider).__stdcall
is common but it might be also__cdecl
.Hi. thank you for your answer. i dont have any .lib file i tried your code but "LibMainConnectByAddress" still remains NULL. so the code skips LibMainConnectByAddress(0x01); do you know why? thank you again
-
Hi. thank you for your answer. i dont have any .lib file i tried your code but "LibMainConnectByAddress" still remains NULL. so the code skips LibMainConnectByAddress(0x01); do you know why? thank you again
All I can suggest is to check if the DLL exports the function "ConnectByAddress" (e.g. using the Dependency Walker (depends.exe) Home Page[^] or executing
dumpbin /exports path_to_dll
on the command line; dumpbin is in the VC/bin folder) and trying it with different calling conventions like__cdecl
. I would use the Dependency Walker because it shows also the calling convention for the exported functions. -
Hi. thank you for your answer. i dont have any .lib file i tried your code but "LibMainConnectByAddress" still remains NULL. so the code skips LibMainConnectByAddress(0x01); do you know why? thank you again
Member 13471493 wrote:
"LibMainConnectByAddress" still remains NULL.
You omitted to mention that in your reply to me. If that function pointer is null then it will cause the error you have seen. You need to check the function name that you are trying to get the address for, does it actually exist in the dll?
-
Member 13471493 wrote:
"LibMainConnectByAddress" still remains NULL.
You omitted to mention that in your reply to me. If that function pointer is null then it will cause the error you have seen. You need to check the function name that you are trying to get the address for, does it actually exist in the dll?
Yes it Does. i tried others functions that exist in dll too. but i got the same error. i am sure about syntax and existence of these functions in the dll.
-
Yes it Does. i tried others functions that exist in dll too. but i got the same error. i am sure about syntax and existence of these functions in the dll.
-
Yes it Does. i tried others functions that exist in dll too. but i got the same error. i am sure about syntax and existence of these functions in the dll.
Have you used the Dependency Walker or dumpbin meanwhile? The exported names may be mangled (see Name mangling - Wikipedia[^] ). Then you have to pass the mangled name to
GetProcAddress
. -
Hello everybody i have a mfc application project in Visual studio 2010. it is a GUI infact. i want to get communication with a mini circuit signal generator device. the device has a dll file named "mcl_gen64.dll i want to use functions of that dll in my code but i dont have any idea how to do this any idea please?
This article explains it. Basically you use LoadLibrary with the DLL's path given to it. The rest is mapping the functions you will need to call.
- Michael Haephrati מיכאל האפרתי