-
DLLs using LoadLibrary()
I am trying to write a DLL and call a function in it using LoadLibrary and GetProcAddress(). The code compiles fine, and I can enter two numbers, but then it crashes. This is my code:
Code:
//Main program
#include <iostream>
using namespace std;
#include <windows.h>
typedef void (__stdcall* MYFUNCTION)(int, int, int &);
int main()
{
HINSTANCE hDLL;
MYFUNCTION funcAdd;
int x, y, sum;
hDLL = LoadLibrary("dll.dll");
if (hDLL)
{
funcAdd = (MYFUNCTION)GetProcAddress(hDLL, "funcAdd");
cout << "Enter 2 ints:" << endl;
cin >> x >> y;
(funcAdd)(x, y, sum);// call the function
cout << "The sum is " << sum << endl;
FreeLibrary(hDLL);
}
cin.get();
return 0;
}
Code:
//DLL code
void __stdcall __declspec(dllexport) funcAdd(int a, int b, int &c)
{
c = a + b;
}
Any ideas? :confused:
-
try using
Code:
extern C _declspec(dllimport) funcAdd(int a, int b, int &c);
& import a lib file into your project ( of your dll)
-
I don't understand. Why do I need a lib file? Where do I get this lib file?
-
when you compile a DLL in C++ there will be a .LIB file created along with it.
then all you have to do is include the LIB in your project settings, make a header to call to the LIB which calls your DLL.
so in the header you would put:
Code:
void _stdcall funcAdd(int a, int b, int &c);
// yes there is supposed to be only one underscore infront of stdcall.
-
I'm using Borland command line compiler 5.5, there is no .lib file. I know I can include the header file and link with the .lib file, but I am doing this for practice so I know how to use DLLs for which I don't have a header and .lib file (cards.dll for example).
-
If you are using LoadLibrary, there is no reason for a .lib file. That is only required when implicitly linking with the library.
Do you have a .def file? If I understand correctly, if you dont, the names get mangled, and "funcAdd" wont work. It appears that GetProcAddress() isnt finding the function specified, but you try to use the function anyway.
if __stdcall compiles, there is no reason to change it, unless you plan on compiling on several platforms... in which case you might not be developing DLLs.
Z.
-
I figured it out :o The dependency walker lists the function as @funcAdd$qqsiiri, so I changed funcAdd in GetProcAddress() to that and now it works. One question though, how would I change it from @funcAdd$qqsiiri to funcAdd in the DLL? (Keep in mind I am using borland command line tools 5.5)
-
if u don't use extern C { } block in DLL, the compiler names the functions with extra stuff in them. To rename the function in a DLL you will need to access the DLL inside (source & recompile)
-
Like I said, you need a .def file.
Z.
-
Zaei - don't u need a def file when the function is
void _stdcall BlaBla
{
}
because C++ renames it anyway, with extern C {} no _stdcall, no def?
-
I dont use def files much, so i dont know exactly what they do, but for all intents, they remove the name mangling from the function. So, funcAdd is really funcAdd, instead of the mangled version. extern "C" tells the compiler that the function IS a C function. __stdcall simply tells the compiler which calling convention to use.
Z.