/// @file srcProcessCommunicationDependRegistry.cpp /// @brief KEY_WOW64_64KEY or KEY_WOW64_32KEY use on RegXX functions /// @title experiment: process communication depend by registry /// @reference http://www.codeproject.com/Articles/14200/Registry-Redirector-in-x64-IA64/ #include "stdafx.h" #include <windows.h> #include <tchar.h> #include <string> #ifndef G_REG_KEY_NAME #define G_REG_KEY_NAME _T("SOFTWARE\\LsApp\\Parameter") #define G_REG_VALUE_NAME _T("ProcessId") #endif DWORD WriteProcessHandleOnX86(); DWORD ReadProcessHandelOnX64(); int _tmain(int argc, _TCHAR* argv[]) { LONG lRc = 0; lRc = WriteProcessHandleOnX86(); _tprintf(_T("%s = %d\n"), _T("WriteProcessHandleOnX86"), lRc); lRc = ReadProcessHandelOnX64(); _tprintf(_T("%s = %d\n"), _T("ReadProcessHandelOnX64"), lRc); getchar(); /// run result /// run x86 version first /** On X86 write: szValue = 4660 WriteProcessHandleOnX86 = 0 ReadProcessHandelOnX64 = 1 */ /// run x64 version /** WriteProcessHandleOnX86 = 1 On X64 Read: szValue = 4660 ReadProcessHandelOnX64 = 0 */ return 0; } DWORD WriteProcessHandleOnX86() { LONG lRc = S_FALSE; #if _M_IX86 HKEY hKey = NULL; TCHAR szValue[_MAX_PATH]; ::ZeroMemory(szValue, sizeof(szValue)); lRc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, G_REG_KEY_NAME, NULL, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &hKey); if(ERROR_SUCCESS != lRc) { /// if key not exist, try to create it lRc = RegCreateKeyEx(HKEY_LOCAL_MACHINE,G_REG_KEY_NAME, 0, NULL, 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY, NULL, &hKey, NULL); } if(ERROR_SUCCESS != lRc) { goto _WriteProcessHandleOnX86_END; } _stprintf(szValue, _T("%d"), GetCurrentProcessId()); _tprintf(_T("%s: %s = %s\n"), _T("On X86 write"), _T("szValue"), szValue); RegSetValueEx(hKey, G_REG_VALUE_NAME, 0, REG_SZ, (const BYTE*) szValue, (_tcslen(szValue) + 1) * sizeof(TCHAR)); _WriteProcessHandleOnX86_END: if(hKey) { RegCloseKey(hKey); } #endif return lRc; } DWORD ReadProcessHandelOnX64() { LONG lRc = S_FALSE; #if _M_X64 HKEY hKey = NULL; TCHAR szValue[_MAX_PATH]; DWORD nLenValue = sizeof(szValue); ::ZeroMemory(szValue, sizeof(szValue)); lRc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, G_REG_KEY_NAME, NULL, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &hKey); if(ERROR_SUCCESS != lRc) { /// x86 application write to registry, x64 application read back only goto _ReadProcessHandelOnX64_END; } lRc = RegQueryValueEx(hKey, G_REG_VALUE_NAME, NULL, NULL, (BYTE*)szValue, &nLenValue); if(ERROR_SUCCESS == lRc) { _tprintf(_T("%s: %s = %s\n"), _T("On X64 Read"), _T("szValue"), szValue); } _ReadProcessHandelOnX64_END: if(hKey) { RegCloseKey(hKey); } #endif return lRc; }