Can't use dll functions

Hi,

I load a dll and then I want to use the dll's functions, but if I call a function from the dll, it gives a weird error. The error is on line 53.

This is the code I use:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#undef UNICODE
#include <windows.h>
#include <cstdio>
//#include <stdio.h>
#include <iostream>
#include <cstring>
bool initdll();

//DLL vars and functions
HINSTANCE hInst;
typedef double (CALLBACK *FUNCTION1)(double, double);
typedef double (CALLBACK *FUNCTION2)(char *, double);
typedef double (CALLBACK *FUNCTION3)(double, double);
typedef double (CALLBACK *FUNCTION4)(double);
typedef char * (CALLBACK *FUNCTION5)(double);
typedef double (CALLBACK *FUNCTION6)(double);
typedef double (CALLBACK *FUNCTION7)(char *, double, double);
typedef double (CALLBACK *FUNCTION8)(double, double, double);
typedef double (CALLBACK *FUNCTION9)(double, double);
typedef double (CALLBACK *FUNCTION10)(double, char *, double, double);
typedef double (CALLBACK *FUNCTION11)(double, double, double);
typedef double (CALLBACK *FUNCTION12)(double);
FUNCTION1 writebyte;
FUNCTION2 writestring;
FUNCTION3 writeshort;
FUNCTION4 readbyte;
FUNCTION5 readstring;
FUNCTION6 readshort;
FUNCTION7 tcpconnect;
FUNCTION8 tcplisten;
FUNCTION9 tcpaccept;
FUNCTION10 sendmessage;
FUNCTION11 receivemessage;
FUNCTION12 tcpip;
//End

//Variables
int players[32];
double servertcp;
//End

int main ()
{
	if ( initdll() == true )
	{
		std::cout << "Server started!" << std::endl;
	}
	else
	{
		std::cout << "DLL not found" << std::endl;
		return false;
	}
	servertcp = tcplisten(12564, 10, 1);
	//while ( 1 == 1 ) // This is needed for the calling of the DLL functions
	//{
		std::cin.get();
		return 0;
	//}
}

bool initdll()
{
	if(hInst = LoadLibrary("C:\\Users\\Joshua\\Documents\\Visual Studio 2008\\Projects\\server\\Debug\\39dll.dll"))
       {
		//Define DLL functions
		writebyte =      (FUNCTION1)  GetProcAddress(hInst, "writebyte");
		writestring =    (FUNCTION2)  GetProcAddress(hInst, "writestring");
		writeshort =     (FUNCTION3)  GetProcAddress(hInst, "writeshort");
		readbyte =       (FUNCTION4)  GetProcAddress(hInst, "readbyte");
		readstring =     (FUNCTION5)  GetProcAddress(hInst, "readstring");
		readshort =      (FUNCTION6)  GetProcAddress(hInst, "readshort");
		tcpconnect =     (FUNCTION7)  GetProcAddress(hInst, "tcpconnect");
		tcplisten =      (FUNCTION8)  GetProcAddress(hInst, "tcplisten");
		tcpaccept =      (FUNCTION9)  GetProcAddress(hInst, "tcpaccept");
		sendmessage =    (FUNCTION10) GetProcAddress(hInst, "sendmessage");
		receivemessage = (FUNCTION11) GetProcAddress(hInst, "receivemessage");
		tcpip =          (FUNCTION12) GetProcAddress(hInst, "tcpip");
		//FreeLibrary( hInst ); // Ofcourse not
		return true;
	}
	else
	{
		return false;
	}
}


It gives this error while running:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.


Can anyone help?
Last edited on
try typedef double (__cdecl *FUNCTION1)(double, double); or typedef double ( *FUNCTION1)(double, double);

How did you declare functions in DLL? If they are declared as
__declspec(dllexport) void Somefunction() then you don't need CALLBACK in function pointer definition.

You can read more about calling conventions here: http://unixwiz.net/techtips/win32-callconv.html
Topic archived. No new replies allowed.