1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcemm/oslink.cpp Tue Sep 18 20:03:47 2007 +0000
1.3 @@ -0,0 +1,69 @@
1.4 +/* ======== SourceMM ========
1.5 + * Copyright (C) 2004-2007 Metamod:Source Development Team
1.6 + * No warranties of any kind
1.7 + *
1.8 + * License: zlib/libpng
1.9 + *
1.10 + * Author(s): David "BAILOPAN" Anderson
1.11 + * ============================
1.12 + */
1.13 +
1.14 +/**
1.15 + * @brief Implements OS-dependant functions from oslink.h
1.16 + * @file oslink.cpp
1.17 + */
1.18 +
1.19 +#include "oslink.h"
1.20 +#ifdef __linux
1.21 +#include <errno.h>
1.22 +#include <stdio.h>
1.23 +#endif
1.24 +
1.25 +#if defined __WIN32__ || defined _WIN32 || defined WIN32
1.26 +const char *dlerror()
1.27 +{
1.28 + static char buf[1024];
1.29 + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1.30 + NULL, GetLastError(),
1.31 + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1.32 + (LPTSTR) &buf, 0, NULL);
1.33 + return buf;
1.34 +}
1.35 +#endif
1.36 +
1.37 +#if defined __linux__
1.38 +int GetLastError()
1.39 +{
1.40 + return errno;
1.41 +}
1.42 +
1.43 +extern "C" void __cxa_guard_acquire(void)
1.44 +{
1.45 +}
1.46 +
1.47 +extern "C" void __cxa_guard_release(void)
1.48 +{
1.49 +}
1.50 +#endif
1.51 +
1.52 +bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
1.53 +{
1.54 +#if defined WIN32 || defined _WIN32
1.55 + MEMORY_BASIC_INFORMATION mem;
1.56 + if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
1.57 + return false;
1.58 + if (mem.AllocationBase == NULL)
1.59 + return false;
1.60 + HMODULE dll = (HMODULE)mem.AllocationBase;
1.61 + GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
1.62 +#elif defined __linux__
1.63 + Dl_info info;
1.64 + if (!dladdr(pAddr, &info))
1.65 + return false;
1.66 + if (!info.dli_fbase || !info.dli_fname)
1.67 + return false;
1.68 + const char *dllpath = info.dli_fname;
1.69 + snprintf(buffer, maxlength, "%s", dllpath);
1.70 +#endif
1.71 + return true;
1.72 +}