sourcemm/oslink.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Aug 10 23:43:16 2007 +0000 (2007-08-10)
changeset 346 d0868271663a
parent 287 0492209c065f
permissions -rw-r--r--
Reformatted output of meta list command
     1 /* ======== SourceMM ========
     2  * Copyright (C) 2004-2007 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 
    40 extern "C" void __cxa_guard_acquire(void)
    41 {
    42 }
    43 
    44 extern "C" void __cxa_guard_release(void)
    45 {
    46 }
    47 #endif
    48 
    49 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
    50 {
    51 #if defined WIN32 || defined _WIN32
    52 	MEMORY_BASIC_INFORMATION mem;
    53 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
    54 		return false;
    55 	if (mem.AllocationBase == NULL)
    56 		return false;
    57 	HMODULE dll = (HMODULE)mem.AllocationBase;
    58 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
    59 #elif defined __linux__
    60 	Dl_info info;
    61 	if (!dladdr(pAddr, &info))
    62 		return false;
    63 	if (!info.dli_fbase || !info.dli_fname)
    64 		return false;
    65 	const char *dllpath = info.dli_fname;
    66 	snprintf(buffer, maxlength, "%s", dllpath);
    67 #endif
    68 	return true;
    69 }