Created
August 18, 2015 05:20
-
-
Save uekkie/49e4f657623c459a9212 to your computer and use it in GitHub Desktop.
WOW64 のファイル システム リダイレクタを制御するクラス ref: http://qiita.com/hirocueki/items/54e35f9aa1c7137b53cf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Wow64FsRedirectionService | |
{ | |
typedef BOOL ( WINAPI *fpWow64DisableWow64FsRedirection )( LPVOID* ); | |
typedef BOOL ( WINAPI *fpWow64RevertWow64FsRedirection )( LPVOID ); | |
public: | |
Wow64FsRedirectionService() | |
: m_lpOldVar( nullptr ) | |
, m_fpWow64DisableWow64FsRedirection(nullptr) | |
, m_fpWow64RevertWow64FsRedirection(nullptr) | |
{ | |
m_hModule = ::LoadLibrary( _T( "Kernel32.dll" ) ); | |
if( m_hModule ) | |
{ | |
m_fpWow64DisableWow64FsRedirection = (fpWow64DisableWow64FsRedirection)::GetProcAddress( m_hModule, "Wow64DisableWow64FsRedirection" ); | |
m_fpWow64RevertWow64FsRedirection = (fpWow64RevertWow64FsRedirection)::GetProcAddress( m_hModule, "Wow64RevertWow64FsRedirection" ); | |
} | |
} | |
~Wow64FsRedirectionService() | |
{ | |
if ( m_hModule ) { | |
// m_lpOldVar がnullptrではないとき => Wow64RevertWow64FsRedirection(リダイレクト有効化)が呼ばれていないので、もどしておく | |
RevertRedirection(); | |
::FreeLibrary(m_hModule); | |
m_hModule = nullptr; | |
} | |
} | |
void DisableRedirection() { | |
if (m_fpWow64DisableWow64FsRedirection ) | |
m_fpWow64DisableWow64FsRedirection( &m_lpOldVar ); | |
} | |
void RevertRedirection() { | |
if (m_lpOldVar && m_fpWow64RevertWow64FsRedirection ) { | |
m_fpWow64RevertWow64FsRedirection( m_lpOldVar ); | |
m_lpOldVar = nullptr; | |
} | |
} | |
private: | |
LPVOID m_lpOldVar; | |
HINSTANCE m_hModule; | |
fpWow64DisableWow64FsRedirection m_fpWow64DisableWow64FsRedirection; | |
fpWow64RevertWow64FsRedirection m_fpWow64RevertWow64FsRedirection; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void do() | |
{ | |
Wow64FsRedirectionService serv; | |
serv.DisableRedirection(); | |
// リダイレクトされてしまうファイルパスへのアクセスはここに。 | |
serv.RevertRedirection(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <functional> | |
class Wow64FsRedirectionService | |
{ | |
typedef std::function<void()> DisableScopeFunction; | |
typedef BOOL ( WINAPI *fpWow64DisableWow64FsRedirection )( LPVOID* ); | |
typedef BOOL ( WINAPI *fpWow64RevertWow64FsRedirection )( LPVOID ); | |
public: | |
Wow64FsRedirectionService() | |
: m_lpOldVar( nullptr ) | |
, m_fpWow64DisableWow64FsRedirection(nullptr) | |
, m_fpWow64RevertWow64FsRedirection(nullptr) | |
{ | |
m_hModule = ::LoadLibrary( _T( "Kernel32.dll" ) ); | |
if( m_hModule ) | |
{ | |
m_fpWow64DisableWow64FsRedirection = (fpWow64DisableWow64FsRedirection)::GetProcAddress( m_hModule, "Wow64DisableWow64FsRedirection" ); | |
m_fpWow64RevertWow64FsRedirection = (fpWow64RevertWow64FsRedirection)::GetProcAddress( m_hModule, "Wow64RevertWow64FsRedirection" ); | |
} | |
} | |
~Wow64FsRedirectionService() | |
{ | |
if ( m_hModule ) { | |
::FreeLibrary(m_hModule); | |
m_hModule = nullptr; | |
} | |
} | |
void UsingDisable(DisableScopeFunction dosomething) { | |
if( DisableRedirection() ) | |
{ | |
dosomething(); | |
RevertRedirection(); | |
} | |
} | |
private: | |
bool DisableRedirection() { | |
if ( !m_fpWow64DisableWow64FsRedirection ) | |
return false; | |
return TRUE == m_fpWow64DisableWow64FsRedirection( &m_lpOldVar ); | |
} | |
bool RevertRedirection() { | |
if (!m_lpOldVar || !m_fpWow64RevertWow64FsRedirection ) | |
return false; | |
const BOOL result = m_fpWow64RevertWow64FsRedirection( m_lpOldVar ); | |
m_lpOldVar = nullptr; | |
return TRUE == result; | |
} | |
private: | |
LPVOID m_lpOldVar; | |
HINSTANCE m_hModule; | |
fpWow64DisableWow64FsRedirection m_fpWow64DisableWow64FsRedirection; | |
fpWow64RevertWow64FsRedirection m_fpWow64RevertWow64FsRedirection; | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Wow64FsRedirectionService FsRedirecta; | |
FsRedirecta.UsingDisable( [](){ | |
// リダイレクトされてしまうファイルパスへのアクセスはここに。 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment