Listing 3: register.c Code to register/unregister DLL
// Server and Proxy/Stub registration // Author : Fran Heeran #include <windows.h> #include "marsamp.h" char s_szCLSID[] = "{BAEA9B70-28E9-11d1-ADD0-006097731D51}"; char s_szIFaceID[] = "{005763A0-2850-11d1-ADCD-006097731D51}"; // Creates a new registry key and optionally sets a default value BOOL SetRegKeyValue(LPTSTR pszKey,LPTSTR pszSubkey, LPTSTR pszValue) { LONG lRes; HKEY hKey; TCHAR szKey[256]; lstrcpy(szKey, pszKey); if (NULL != pszSubkey) { lstrcat(szKey, TEXT("\\")); lstrcat(szKey, pszSubkey); } if (ERROR_SUCCESS != RegCreateKeyEx(HKEY_CLASSES_ROOT,szKey,0, NULL,REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,NULL,&hKey,NULL)) return FALSE; if (pszValue) lRes = RegSetValueEx(hKey,NULL,0,REG_SZ,(BYTE *)pszValue, (lstrlen(pszValue)+1)*sizeof(TCHAR)); RegCloseKey(hKey); return (lRes == ERROR_SUCCESS); } // Deletes a registry key void DeleteRegKey(LPTSTR pszKey,LPTSTR pszSubkey) { TCHAR szKey[256]; lstrcpy(szKey, pszKey); if (pszSubkey) { lstrcat(szKey, TEXT("\\")); lstrcat(szKey, pszSubkey); } RegDeleteKey(HKEY_CLASSES_ROOT, szKey); } // Called to register the Server and Proxy/Stub DLL.The servers file // name is passed in and the Proxy/Stub DLL name derived from that BOOL RegisterServer(LPSTR pszExePath) { TCHAR szCLSID[129]; TCHAR szIFace[129]; char szFile[MAX_PATH]; lstrcpy(szCLSID, TEXT("CLSID\\")); lstrcat(szCLSID, s_szCLSID); strcpy(szFile, pszExePath); strcat(szFile, "\\server.exe"); // Create entries under CLSID. SetRegKeyValue(szCLSID, NULL, TEXT("ISimpleObject - WDJ wire_marshall example")); SetRegKeyValue(szCLSID, TEXT("LaunchPermission"), TEXT("Y")); SetRegKeyValue(szCLSID, TEXT("LocalServer32"), szFile); // Register the Proxy/Stub DLL strcpy(szFile, pszExePath); strcat(szFile, "\\sobj.dll"); lstrcpy(szIFace, TEXT("Interface\\")); lstrcat(szIFace, s_szIFaceID); // Create the HKEY_CLASSES_ROOT\Interface entries. SetRegKeyValue(szIFace, NULL, TEXT("ISimpleObject")); SetRegKeyValue(szIFace, TEXT("ProxyStubClsid32"), s_szIFaceID); SetRegKeyValue(szIFace, TEXT("NumMethods"), TEXT("4")); // Create the HKEY_CLASSES_ROOT\CLSID entries. lstrcpy(szCLSID, TEXT("CLSID\\")); lstrcat(szCLSID, s_szIFaceID); SetRegKeyValue(szCLSID, NULL, TEXT("ISimpleObject Proxy/Stub Factory")); SetRegKeyValue(szCLSID, TEXT("InprocServer32"), szFile); return TRUE; } // Called to unregister the Server and Proxy/Stub DLL void UnregisterServer() { TCHAR szCLSID[129]; TCHAR szIFace[129]; // Delete server registry entries lstrcpy(szCLSID, TEXT("CLSID\\")); lstrcat(szCLSID, s_szCLSID); DeleteRegKey(szCLSID, TEXT("LaunchPermission")); DeleteRegKey(szCLSID, TEXT("LocalServer32")); DeleteRegKey(szCLSID, NULL); // Delete Proxy/Stub Entries lstrcpy(szIFace, TEXT("Interface\\")); lstrcat(szIFace, s_szIFaceID); DeleteRegKey(szIFace, TEXT("ProxyStubClsid32")); DeleteRegKey(szIFace, TEXT("NumMethods")); DeleteRegKey(szIFace, NULL); lstrcpy(szCLSID, TEXT("CLSID\\")); lstrcat(szCLSID, s_szIFaceID); DeleteRegKey(szCLSID, TEXT("InprocServer32")); DeleteRegKey(szCLSID, NULL); } //End of File