- change main.cc to use LoadLibrary/GetProcAddress instead of ltdl library
- change MAINAPI macro to make code more readable
This commit is contained in:
parent
fec82f3ee8
commit
afe8489945
@ -1,34 +1,52 @@
|
||||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
#include <ltdl.h>
|
||||
#define MAIN_DLL_EXPORT
|
||||
#include "main.h"
|
||||
|
||||
const char *version_string = "uselib-test6-1.0";
|
||||
MAINAPI const char *version_string = "uselib-test6-1.0";
|
||||
|
||||
int register_module (const char *name)
|
||||
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);
|
||||
lt_dlhandle handle = lt_dlopen (buf);
|
||||
printf ("handle is %p\n", handle);
|
||||
HMODULE handle = LoadLibrary(buf);
|
||||
printf ("handle is 0x%p\n", handle);
|
||||
if (!handle) {
|
||||
printf ("lt_dlopen error: %s\n", lt_dlerror ());
|
||||
print_last_error ("LoadLibrary failed: %s\n");
|
||||
return -1;
|
||||
}
|
||||
modload_func func = (modload_func) lt_dlsym (handle, "module_init");
|
||||
printf ("module_init function is at %p\n", func);
|
||||
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 {
|
||||
printf ("lt_dlsym error: %s\n", lt_dlerror ());
|
||||
print_last_error ("GetProcAddress failed: %s\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
@ -37,21 +55,16 @@ int load_module (const char *fmt, const char *name)
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
printf ("start\n");
|
||||
if (lt_dlinit () != 0) {
|
||||
printf ("lt_dlinit error: %s\n", lt_dlerror ());
|
||||
return -1;
|
||||
}
|
||||
printf ("loading module1\n");
|
||||
// try to load module1
|
||||
if (load_module ("lib%s.la", "module1") < 0) {
|
||||
if (load_module ("%s.dll", "module1") < 0) {
|
||||
printf ("load module1 failed\n");
|
||||
}
|
||||
if (load_module ("lib%s.la", "module2") < 0) {
|
||||
if (load_module ("%s.dll", "module2") < 0) {
|
||||
printf ("load module2 failed\n");
|
||||
}
|
||||
int arg;
|
||||
for (int arg=1; arg < argc; arg++) {
|
||||
if (load_module ("%s", argv[arg]) < 0) {
|
||||
if (load_module ("%s.dll", argv[arg]) < 0) {
|
||||
printf ("load %s failed\n", argv[arg]);
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +1,22 @@
|
||||
#if defined(WIN32) || defined(__CYGWIN__)
|
||||
# ifdef MAIN_DLL_EXPORT
|
||||
# ifdef DLL_EXPORT
|
||||
# warning I will export DLL symbols for MODULE1
|
||||
# define MAINAPI(type) __declspec(dllexport) type
|
||||
# warning I will export DLL symbols for MAIN
|
||||
# define MAINAPI __declspec(dllexport)
|
||||
# endif
|
||||
# else
|
||||
# warning I will import DLL symbols for MODULE1
|
||||
# define MAINAPI(type) __declspec(dllimport) type
|
||||
# warning I will import DLL symbols from MAIN
|
||||
# define MAINAPI __declspec(dllimport)
|
||||
# endif
|
||||
#endif
|
||||
#ifndef MAINAPI
|
||||
# warning No DLL import/export is needed
|
||||
# define MAINAPI(type) type
|
||||
# define MAINAPI
|
||||
#endif
|
||||
|
||||
typedef void (*modload_func)(void);
|
||||
|
||||
MAINAPI(extern const char *) version_string;
|
||||
MAINAPI(extern int) register_module (const char *name);
|
||||
MAINAPI extern const char * version_string;
|
||||
MAINAPI extern int register_module (const char *name);
|
||||
|
||||
extern "C" {
|
||||
// this prevents C++ name mangling
|
||||
|
Loading…
x
Reference in New Issue
Block a user