Listing 3: main.c Demonstration program
// main.c #define UNICODE #define _UNICODE #include <windows.h> #include <tchar.h> #include <stdio.h> #include "symlink.h" #define A_PATH TEXT("Software\\PaulaT\\A") #define B_PATH TEXT("Software\\PaulaT\\B") #define CURRENT_PATH TEXT("Software\\PaulaT\\Current") #define CURRENT_KEY TEXT("Current") #define PAULAT TEXT("Software\\PaulaT") //----------------------------------------------------------------- int main(void) { HKEY hKey = NULL; DWORD status, t, s, dw = 0, dw2 = 0; TCHAR sz[MAX_PATH]; // Create some keys to test symbolic links with status = RegCreateKeyEx(HKEY_LOCAL_MACHINE, A_PATH, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &hKey, 0); if (status == ERROR_SUCCESS) { dw = 1; RegSetValueEx(hKey,TEXT("Val1"),0,REG_SZ, (LPBYTE)TEXT("AAA"), 8); RegSetValueEx(hKey,TEXT("Val2"),0,REG_DWORD,(LPBYTE)&dw,4); RegCloseKey(hKey); } status = RegCreateKeyEx(HKEY_LOCAL_MACHINE, B_PATH, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &hKey, 0); if (status == ERROR_SUCCESS) { dw = 2; RegSetValueEx(hKey,TEXT("Val1"),0,REG_SZ, (LPBYTE)TEXT("BBB"), 8); RegSetValueEx(hKey,TEXT("Val2"),0,REG_DWORD,(LPBYTE)&dw,4); RegCloseKey(hKey); } // Create a symbolic link key named "Current" status = CreateSymLinkKey(HKEY_LOCAL_MACHINE, PAULAT, CURRENT_KEY, &hKey); // Set the symbolic link key to key "A" SetSymLink(hKey, HKEY_LOCAL_MACHINE, A_PATH); RegCloseKey(hKey); // Test symbolic link by reading values status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, CURRENT_PATH, 0, KEY_READ, &hKey); if (status == ERROR_SUCCESS) { s = MAX_PATH * sizeof(TCHAR); RegQueryValueEx(hKey,TEXT("Val1"),0,&t,(LPBYTE)sz,&s); s = sizeof(DWORD); RegQueryValueEx(hKey,TEXT("Val2"),0,&t,(LPBYTE)&dw2,&s); _tprintf(TEXT("Val1 = %s, Val2 = %d\n"), sz, dw2); RegCloseKey(hKey); } // Reopen the symbolic link and reset it to different key status = OpenSymLink(HKEY_LOCAL_MACHINE, CURRENT_PATH, &hKey); if (status == ERROR_SUCCESS) { SetSymLink(hKey, HKEY_LOCAL_MACHINE, B_PATH); } // Test symbolic link by reading values status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, CURRENT_PATH, 0, KEY_READ, &hKey); if (status == ERROR_SUCCESS) { s = MAX_PATH * sizeof(TCHAR); RegQueryValueEx(hKey,TEXT("Val1"),0,&t,(LPBYTE)sz,&s); s = sizeof(DWORD); RegQueryValueEx(hKey,TEXT("Val2"),0,&t,(LPBYTE)&dw2,&s); _tprintf(TEXT("Val1 = %s, Val2 = %d\n"), sz, dw2); RegCloseKey(hKey); } // Now try deleting the Symbolic link key (not the key it points to) DeleteSymLink(HKEY_LOCAL_MACHINE, CURRENT_PATH); return TRUE; } // main //End of File