[C++] Dll Problem

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>

using namespace 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^^
Last edited on
Bitte auf Englisch.
Does this work?

1
2
3
4
5
6
7
//Header
#ifndef TestDll_h
#define TestDll_h

extern "C" int __declspec( dllexport )sum( int a, int b );

#endif  

EDIT: How do you test your dll?
Last edited on
I try to load it:

1
2
3
4
5
6
7
8
9
10
11
12
#include <windows.h>
#include <iostream>

using namespace 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 getting this error.

1
2
3
4
5
1>Main.obj : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__sum".
1>C:\Users\Jan\Desktop\C++\Loader\Release\Loader.exe : fatal error LNK1120: 1 nicht aufgelöste externe Verweise.
1>Das Buildprotokoll wurde unter "file://c:\Users\Jan\Desktop\C++\Loader\Loader\Release\BuildLog.htm" gespeichert.
1>Loader - 2 Fehler, 0 Warnung(en)
========== Erstellen: 0 erfolgreich, Fehler bei 1, 0 aktuell, 0 übersprungen ==========


@m4ster r0shi: sry it did not work.
Last edited on
Ok, that's not how you load a DLL :P Let's see how it's done:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <windows.h>
#include <iostream>

using namespace std;

//typedef for a function pointer
//to the function you want to load
typedef int (*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;
}

EDIT: See here for more info:

http://msdn.microsoft.com/en-us/library/ms684175%28VS.85%29.aspx (LoadLibrary)
http://msdn.microsoft.com/en-us/library/ms683212%28v=VS.85%29.aspx (GetProcAddress)
Last edited on
Ok thank you! Now it works...:P
Gives in this forum a thanks button?

close pls.
Last edited on
mTy wrote:
close pls.

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/
Topic archived. No new replies allowed.