Skip to content

Instantly share code, notes, and snippets.

@Paxxi
Created February 19, 2015 12:37
Show Gist options
  • Save Paxxi/960c1836b637d5c757ea to your computer and use it in GitHub Desktop.
Save Paxxi/960c1836b637d5c757ea to your computer and use it in GitHub Desktop.
simpler version without boost
#pragma once
/*
* Copyright (C) 2015 Team Kodi
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
/**
* \file win32\crts_caller.h
* \brief Declares crts_caller class for calling same function for all loaded CRTs.
* \author Karlson2k
*/
#include <string>
#include <vector>
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif // WIN32_LEAN_AND_MEAN
#include <Windows.h>
namespace win32_utils
{
template <typename ret_type, typename... param_types>
typename ret_type call_in_all_crts(const char* func_name, ret_type(*cur_fnc_ptr) (param_types...), param_types... params)
{
std::vector<std::wstring> s_crtNames
{
L"msvcrt.dll", // Visual Studio 6.0 / MinGW[-w64]
L"msvcr70.dll", // Visual Studio 2002
L"msvcr71.dll", // Visual Studio 2003
L"msvcr80.dll", // Visual Studio 2005
L"msvcr90.dll", // Visual Studio 2008
#ifdef _DEBUG
L"msvcr90d.dll", // Visual Studio 2008 (debug)
#endif
L"msvcr100.dll", // Visual Studio 2010
#ifdef _DEBUG
L"msvcr100d.dll",// Visual Studio 2010 (debug)
#endif
L"msvcr110.dll", // Visual Studio 2012
#ifdef _DEBUG
L"msvcr110d.dll",// Visual Studio 2012 (debug)
#endif
L"msvcr120.dll", // Visual Studio 2013
#ifdef _DEBUG
L"msvcr120d.dll",// Visual Studio 2013 (debug)
#endif
};
for (auto& crtName : s_crtNames)
{
HMODULE hCrt = NULL;
if (!GetModuleHandleExW(0, crtName.c_str(), &hCrt) || hCrt == NULL) // Flag 0 ensures that CRL will not be unloaded while we are using it here
continue; // Module not loaded
auto func_ptr = reinterpret_cast<decltype(cur_fnc_ptr)>(GetProcAddress(hCrt, func_name));
if (func_ptr != NULL)
{
(void)func_ptr(params...); // ignoring result of function call
}
FreeLibrary(hCrt); // this CRT will not be used
}
return cur_fnc_ptr(params...); // return result of calling process's CRT function
}
}
//call as
win32_utils::call_in_all_crts("_configthreadlocale", _configthreadlocale, param)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment