|
-
Jan 31st, 2002, 02:46 PM
#1
Thread Starter
Fanatic Member
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?
Alcohol & calculus don't mix.
Never drink & derive.
-
Jan 31st, 2002, 04:06 PM
#2
Hyperactive Member
try using
Code:
extern C _declspec(dllimport) funcAdd(int a, int b, int &c);
& import a lib file into your project ( of your dll)
-
Jan 31st, 2002, 08:03 PM
#3
Thread Starter
Fanatic Member
I don't understand. Why do I need a lib file? Where do I get this lib file?
Alcohol & calculus don't mix.
Never drink & derive.
-
Jan 31st, 2002, 08:44 PM
#4
Lively Member
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.
-
Feb 1st, 2002, 07:19 PM
#5
Thread Starter
Fanatic Member
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).
Alcohol & calculus don't mix.
Never drink & derive.
-
Feb 2nd, 2002, 12:43 AM
#6
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.
-
Feb 2nd, 2002, 01:19 AM
#7
Thread Starter
Fanatic Member
I figured it out 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)
Alcohol & calculus don't mix.
Never drink & derive.
-
Feb 2nd, 2002, 06:05 AM
#8
Hyperactive Member
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)
-
Feb 2nd, 2002, 11:54 AM
#9
Like I said, you need a .def file.
Z.
-
Feb 2nd, 2002, 07:43 PM
#10
Hyperactive Member
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?
-
Feb 2nd, 2002, 11:07 PM
#11
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|