sourcemm/oslink.cpp
author David Anderson <dvander@alliedmods.net>
Thu Oct 06 00:59:01 2005 +0000 (2005-10-06)
changeset 122 7c36121e2dfe
parent 9 01b8b7435e67
child 127 4f3bdf8aa859
permissions -rw-r--r--
Initial import of new GameDLL loading code
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2005 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): David "BAILOPAN" Anderson
     8 * ============================
     9 */
    10 
    11 /**
    12  * @brief Implements OS-dependant functions from oslink.h
    13  * @file oslink.cpp
    14  */
    15 
    16 #include "oslink.h"
    17 #ifdef __linux
    18 #include <errno.h>
    19 #endif
    20 
    21 #if defined __WIN32__ || defined _WIN32 || defined WIN32
    22 const char *dlerror()
    23 {
    24 	static char buf[1024];
    25 	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
    26 		NULL, GetLastError(),
    27 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
    28 		(LPTSTR) &buf, 0, NULL);
    29 	return buf;
    30 }
    31 #endif
    32 
    33 #if defined __linux__
    34 int GetLastError()
    35 {
    36 	return errno;
    37 }
    38 #endif
    39 
    40 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
    41 {
    42 #if defined WIN32 || defined _WIN32
    43 	MEMORY_BASIC_INFORMATION mem;
    44 	if (!VirtualQuery((void *)pAddr, &mem, sizeof(mem)))
    45 		return false;
    46 	if (mem.AllocationBase == NULL)
    47 		return false;
    48 	HMODULE dll = (HMODULE)mem.AllocationBase;
    49 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
    50 #elif defined __linux__
    51 	Dl_info info;
    52 	if (!dladdr((void *)pAddr, &info))
    53 		return false;
    54 	if (!info.dli_fbase || !info.dli_fname)
    55 		return false;
    56 	const char *dllpath = info.dli_fname;
    57 	snprintf(buffer, maxlength, "%s", dllpath);
    58 #endif
    59 	return true;
    60 }