sourcemm/oslink.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Sep 15 06:50:44 2006 +0000 (2006-09-15)
changeset 260 d72a4204d786
parent 238 c27105949293
child 282 7a237e9ea5a1
permissions -rw-r--r--
Tentative makefile changes to link to new tier1_i486.a static library
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2006 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 #include <stdio.h>
    20 #endif
    21 
    22 #if defined __WIN32__ || defined _WIN32 || defined WIN32
    23 const char *dlerror()
    24 {
    25 	static char buf[1024];
    26 	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
    27 		NULL, GetLastError(),
    28 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
    29 		(LPTSTR) &buf, 0, NULL);
    30 	return buf;
    31 }
    32 #endif
    33 
    34 #if defined __linux__
    35 int GetLastError()
    36 {
    37 	return errno;
    38 }
    39 #endif
    40 
    41 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
    42 {
    43 #if defined WIN32 || defined _WIN32
    44 	MEMORY_BASIC_INFORMATION mem;
    45 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
    46 		return false;
    47 	if (mem.AllocationBase == NULL)
    48 		return false;
    49 	HMODULE dll = (HMODULE)mem.AllocationBase;
    50 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
    51 #elif defined __linux__
    52 	Dl_info info;
    53 	if (!dladdr(pAddr, &info))
    54 		return false;
    55 	if (!info.dli_fbase || !info.dli_fname)
    56 		return false;
    57 	const char *dllpath = info.dli_fname;
    58 	snprintf(buffer, maxlength, "%s", dllpath);
    59 #endif
    60 	return true;
    61 }