Bochs/bochs-testing/plugin-test/test7-win32dll/main.cc
Bryce Denney afe8489945 - change main.cc to use LoadLibrary/GetProcAddress instead of ltdl library
- change MAINAPI macro to make code more readable
2002-10-14 20:43:33 +00:00

75 lines
1.7 KiB
C++

#include <windows.h>
#include <stdio.h>
#define MAIN_DLL_EXPORT
#include "main.h"
MAINAPI const char *version_string = "uselib-test6-1.0";
MAINAPI int register_module (const char *name)
{
printf ("register_module was called by module '%s'\n", name);
return 0;
}
void print_last_error (char *fmtstring)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
printf (fmtstring, (char*)lpMsgBuf);
LocalFree (lpMsgBuf);
}
int load_module (const char *fmt, const char *name)
{
char buf[512];
sprintf (buf, fmt, name);
printf ("loading module from %s\n", buf);
HMODULE handle = LoadLibrary(buf);
printf ("handle is 0x%p\n", handle);
if (!handle) {
print_last_error ("LoadLibrary failed: %s\n");
return -1;
}
modload_func func = (modload_func) GetProcAddress (handle, "module_init");
printf ("module_init function is at 0x%p\n", func);
if (func != NULL) {
printf ("Calling module_init\n");
(*func)();
} else {
print_last_error ("GetProcAddress failed: %s\n");
return -1;
}
return 0;
}
int main (int argc, char **argv)
{
printf ("start\n");
printf ("loading module1\n");
// try to load module1
if (load_module ("%s.dll", "module1") < 0) {
printf ("load module1 failed\n");
}
if (load_module ("%s.dll", "module2") < 0) {
printf ("load module2 failed\n");
}
for (int arg=1; arg < argc; arg++) {
if (load_module ("%s.dll", argv[arg]) < 0) {
printf ("load %s failed\n", argv[arg]);
}
}
printf ("stop\n");
return 0;
}