PDA

Click to See Complete Forum and Search --> : DLLs using LoadLibrary()


Wynd
Jan 31st, 2002, 01:46 PM
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:
//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;
}//DLL code
void __stdcall __declspec(dllexport) funcAdd(int a, int b, int &c)
{
c = a + b;
}Any ideas? :confused:

made_of_asp
Jan 31st, 2002, 03:06 PM
try using


extern C _declspec(dllimport) funcAdd(int a, int b, int &c);


& import a lib file into your project ( of your dll)

Wynd
Jan 31st, 2002, 07:03 PM
I don't understand. Why do I need a lib file? Where do I get this lib file?

ExciteMouse
Jan 31st, 2002, 07:44 PM
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:


void _stdcall funcAdd(int a, int b, int &c);
// yes there is supposed to be only one underscore infront of stdcall.

Wynd
Feb 1st, 2002, 06:19 PM
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).

Zaei
Feb 1st, 2002, 11:43 PM
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.

Wynd
Feb 2nd, 2002, 12:19 AM
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)

made_of_asp
Feb 2nd, 2002, 05:05 AM
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)

Zaei
Feb 2nd, 2002, 10:54 AM
Like I said, you need a .def file.

Z.

made_of_asp
Feb 2nd, 2002, 06:43 PM
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?

Zaei
Feb 2nd, 2002, 10:07 PM
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.