Created
August 12, 2016 09:52
-
-
Save ssfang/904211c08798bb649fb73ed4ddbea884 to your computer and use it in GitHub Desktop.
GetApp32SystemDirectory
This file contains hidden or 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
// Win32.cpp : 定义控制台应用程序的入口点。 | |
// | |
#include <stdio.h> | |
#include <tchar.h> | |
// TODO: 在此处引用程序需要的其他头文件 | |
#include <windows.h> | |
//PathFileExists | |
#include <shlwapi.h> | |
#pragma comment(lib, "shlwapi.lib") | |
//SHGetSpecialFolderPath, SHGetFolderPath | |
#include <Shlobj.h> | |
#pragma comment(lib, "Shell32.lib") | |
// For a 32-bit application, GetSystemDirectory() always gives the same result(e.g. C:\Windows\system32) on both 32bit and 64bit Windows. | |
// In order to get a real path: Use SHGetFolderPath or SHGetSpecialFolderPath to retrieve the "System32" directory on 32bit and the "SysWOW64" directory on 64bit. | |
// @see http://stackoverflow.com/questions/3540930/getting-syswow64-directory-using-32-bit-application | |
#ifdef _WIN32 | |
#define CSIDL_SYSTEM_DIR CSIDL_SYSTEMX86 // 0x0029, x86 system directory on RISC | |
#else | |
#define CSIDL_SYSTEM_DIR CSIDL_SYSTEM // 0x0025, GetSystemDirectory() | |
#endif | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
TCHAR szDir[MAX_PATH], szRealDir[MAX_PATH]; | |
UINT uLength; | |
PVOID OldValue = NULL; | |
//https://msdn.microsoft.com/en-us/library/windows/desktop/bb762181.aspx | |
//Windows 2000 Professional, Windows XP Shell32.dll (version 5.0 or later) | |
if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_SYSTEM_DIR, NULL, 0, szDir))) | |
{ | |
_tprintf(TEXT("SHGetFolderPath: %s\n"), szDir); | |
} | |
//https://msdn.microsoft.com/en-us/library/windows/desktop/bb762204(v=vs.85).aspx | |
//From Windows 2000 Professional Shell32.dll (version 4.71 or later) | |
SHGetSpecialFolderPath(NULL, szDir, CSIDL_SYSTEM_DIR, FALSE); | |
_tprintf(TEXT("SHGetSpecialFolderPath: %s\n"), szDir); | |
// Disable redirection immediately prior to the native API function call. | |
if( Wow64DisableWow64FsRedirection (&OldValue) ) | |
{ | |
// Copy to the buffer, terminating null character. | |
// return length in TCHARs, not including the terminating null character. | |
uLength = GetSystemDirectory(szDir, _countof(szDir)); | |
_tprintf(TEXT("GetSystemDirectory: %s\n"), szDir); | |
// Immediately re-enable redirection. Note that any resources | |
// associated with OldValue are cleaned up by this call. | |
if ( FALSE == Wow64RevertWow64FsRedirection(OldValue) ) | |
{ | |
// Failure to re-enable redirection should be considered | |
// a criticial failure and execution aborted. | |
} | |
} | |
GetSystemWow64Directory(szDir, _countof(szDir)); | |
_tprintf(TEXT("GetSystemWow64Directory: %s\n"), szDir); | |
uLength = GetFullPathName(szDir, _countof(szRealDir), szRealDir, NULL); | |
if (uLength == 0) | |
{ | |
// Handle an error condition. | |
printf ("GetFullPathName failed (%d)\n", GetLastError()); | |
} | |
else | |
{ | |
_tprintf(TEXT("The full path name is: %s\n"), szRealDir); | |
} | |
// C:\Windows\SysWOW64 is not safe but it's currently ok. | |
LPCTSTR pszSysWow64OnlyOn64 = TEXT("C:\\Windows\\SysWOW64"); | |
PathFileExists(pszSysWow64OnlyOn64); | |
_tprintf(TEXT("%d = PathFileExists(%s)\n"), PathFileExists(pszSysWow64OnlyOn64), pszSysWow64OnlyOn64); | |
getchar(); | |
return uLength; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment