-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinhost.c
82 lines (67 loc) · 1.89 KB
/
winhost.c
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
/**
Window Host
This will host the DLL
The intention is pure dynamic loading not linked against at
run time. Known as Run-Time Dynamic Linking
Sources:
http://msdn.microsoft.com/en-us/library/ms686944(v=VS.85).aspx
Compiling
VC:
cl winhost.c
GCC:
gcc winhost.c
*/
#include <windows.h>
#include <stdio.h>
void errorReport(const char *extra) {
LPVOID errMsg;
DWORD errorID = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errorID,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &errMsg,
0, NULL );
printf("Error with %s: %s", extra, errMsg);
}
int main(int argc, char *argv[]) {
// Hold onto the dll instance
HINSTANCE dllInstance;
// Define functions
void (__cdecl *func1)(void);
int (__cdecl *func2)(void);
int (__cdecl *func3)(int,int);
// This loads in the libary
dllInstance = LoadLibrary(TEXT("windll1.dll"));
if (dllInstance == NULL) {
errorReport("loading library");
return -1;
}
// Get the address to the function in the libary
func1 = (void (__cdecl *)(void))GetProcAddress(dllInstance, "function1");
if (func1 == NULL) {
errorReport("finding function1");
}
func2 = (int (__cdecl *)(void))GetProcAddress(dllInstance, "function2");
if (func2 == NULL) {
errorReport("finding function2");
}
func3 = (int (__cdecl *)(int,int))GetProcAddress(dllInstance, "function3");
if (func3 == NULL) {
errorReport("finding function3");
}
// Call the first function
if (func1 != NULL) {
func1();
}
if (func2 != NULL) {
printf("Function 2: %d\n", func2());
}
if (func3 != NULL) {
printf("Function 3: %d\n", func3(2, 3));
}
return 0;
}