Quote:
Originally Posted by BeetleFart
I dont know how to make the dll dynamically load.
|
Here is a sample code.
Code:
#ifdef _WIN32
#include <windows.h>
#include <winsock.h>
#else
#include <unistd.h>
#endif
#include <stdio.h>
#ifdef _WIN32
typedef int (__stdcall * WSAStartupProc)(WORD wVersionRequested, LPWSADATA lpWSAData);
typedef int (__stdcall * gethostnameProc)(char FAR* name, int namelen);
typedef int (__stdcall * WSACleanupProc)(void);
#endif
int main(int argc, char* argv[])
{
#ifdef _WIN32
HINSTANCE hLib = LoadLibrary("wsock32.dll");
if (!hLib) return 1;
WSAStartupProc WSAStartup = (WSAStartupProc)GetProcAddress(hLib, "WSAStartup");
if (!WSAStartup) return 1;
gethostnameProc gethostname = (gethostnameProc)GetProcAddress(hLib, "gethostname");
if (!gethostname) return 1;
WSACleanupProc WSACleanup = (WSACleanupProc)GetProcAddress(hLib, "WSACleanup");
if (!WSACleanup) return 1;
WSADATA wsa;
if (WSAStartup(MAKEWORD(1, 1), &wsa)) return 1;
#endif
char hostname[256];
int result = gethostname(hostname, sizeof(hostname));
fprintf(stdout, "result == %d, hostname == %s\n", result, result == 0 ? hostname : "ERROR");
#ifdef _WIN32
if (WSACleanup()) return 1;
#endif
return 0;
}