Hi all,
sry for writing first time in engish. Ok now to my problem. I have a Dll with a strange error. I think the code is right, but if i load the Dll, i get a exeption that my dll have a wrong format.
Now my Dll code:
1 2 3 4
//My header
#ifndef TestDll_h
#define TestDll_h extern "C" int __declspec( dllexport )sum( int a, int b );
#endif
1 2 3 4 5
//My funktion
extern"C"int __declspec(dllexport) sum(int a, int b)
{
return a + b;
}
Here i am going to load it:
The Dll and the application are in the same directory.
1 2 3 4 5 6 7 8 9 10 11 12
#include <windows.h>
#include <iostream>
usingnamespace std;
extern"C" _declspec (dllimport) int sum(int a, int b);
int main(int argc, char* argv[])
{
cout << sum(5, 6) << endl;
return 0;
}
I have included my header in stdafx.h.
I hope you can help me! :)
BTW: Sry for my bad english^^
#include <windows.h>
#include <iostream>
usingnamespace std;
//typedef for a function pointer
//to the function you want to load
typedefint (*DLL_FUNC)(int,int);
int main(int argc, char* argv[])
{
//a function pointer to hold
//the loaded function
DLL_FUNC my_func;
//the dll handle
HINSTANCE hDll;
//that's how you load a dll ;)
hDll = LoadLibrary(TEXT("your_dll_name.dll"));
//and that's how you get your function ;)
my_func = (DLL_FUNC) GetProcAddress(hDll, "sum");
cout << my_func(5, 6) << endl;
//unload the dll
FreeLibrary(hDll);
cout << "\nhit enter to quit...";
cin.get();
return 0;
}
LOL, what do you mean? You want this discussion to end with a post by me? Fine then.
mTy wrote:
Ok thank you! Now it works...:P
You are welcome! :D
mTy wrote:
Gives in this forum a thanks button?
Haha, you don't need to thank me, I'll get my reward in my next life review ;)
If you don't know what a life review is, take a look here -> http://cplusplus.com/forum/lounge/24744/