core-legacy/oslink.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed May 21 17:26:09 2014 -0400 (2014-05-21)
changeset 917 95639ee45d3d
parent 545 07e5df11f9fc
permissions -rw-r--r--
Trigger build for hl2sdk-l4d changes.
     1 /* ======== SourceMM ========
     2  * Copyright (C) 2004-2008 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 <stdio.h>
    17 #include "oslink.h"
    18 #ifdef __linux
    19 #include <errno.h>
    20 #endif
    21 
    22 #if defined __WIN32__ || defined _WIN32 || defined WIN32
    23 const char *dlerror()
    24 {
    25 	static char buf[1024];
    26 	DWORD num;
    27 
    28 	num = GetLastError();
    29 
    30 	if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
    31 						NULL,
    32 						num,
    33 						MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
    34 						buf,
    35 						sizeof(buf),
    36 						NULL)
    37 		== 0)
    38 	{
    39 		_snprintf(buf, sizeof(buf), "unknown error %x", num);
    40 	}
    41 
    42 	return buf;
    43 }
    44 #endif
    45 
    46 #if defined __linux__
    47 int GetLastError()
    48 {
    49 	return errno;
    50 }
    51 
    52 extern "C" void __cxa_guard_acquire(void)
    53 {
    54 }
    55 
    56 extern "C" void __cxa_guard_release(void)
    57 {
    58 }
    59 #endif
    60 
    61 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
    62 {
    63 #if defined WIN32 || defined _WIN32
    64 	MEMORY_BASIC_INFORMATION mem;
    65 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
    66 		return false;
    67 	if (mem.AllocationBase == NULL)
    68 		return false;
    69 	HMODULE dll = (HMODULE)mem.AllocationBase;
    70 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
    71 #elif defined __linux__
    72 	Dl_info info;
    73 	if (!dladdr(pAddr, &info))
    74 		return false;
    75 	if (!info.dli_fbase || !info.dli_fname)
    76 		return false;
    77 	const char *dllpath = info.dli_fname;
    78 	snprintf(buffer, maxlength, "%s", dllpath);
    79 #endif
    80 	return true;
    81 }