Header And Logo

PostgreSQL
| The world's most advanced open source database.

pgevent.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * pgevent.c
00004  *      Defines the entry point for pgevent dll.
00005  *      The DLL defines event source for backend
00006  *
00007  *
00008  * IDENTIFICATION
00009  *    src/bin/pgevent/pgevent.c
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 
00014 
00015 #include <windows.h>
00016 #include <olectl.h>
00017 #include <string.h>
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 
00021 /* Global variables */
00022 HANDLE      g_module = NULL;    /* hModule of DLL */
00023 
00024 /*
00025  * The event source is stored as a registry key.
00026  * The maximum length of a registry key is 255 characters.
00027  * http://msdn.microsoft.com/en-us/library/ms724872(v=vs.85).aspx
00028  */
00029 char        event_source[256] = "PostgreSQL";
00030 
00031 /* Prototypes */
00032 HRESULT     DllInstall(BOOL bInstall, LPCWSTR pszCmdLine);
00033 STDAPI      DllRegisterServer(void);
00034 STDAPI      DllUnregisterServer(void);
00035 BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved);
00036 
00037 /*
00038  * DllInstall --- Passes the command line argument to DLL
00039  */
00040 
00041 HRESULT
00042 DllInstall(BOOL bInstall,
00043            LPCWSTR pszCmdLine)
00044 {
00045 
00046     if (pszCmdLine && *pszCmdLine != '\0')
00047         wcstombs(event_source, pszCmdLine, sizeof(event_source));
00048 
00049     /*
00050      * This is an ugly hack due to the strange behavior of "regsvr32 /i".
00051      *
00052      * When installing, regsvr32 calls DllRegisterServer before DllInstall.
00053      * When uninstalling (i.e. "regsvr32 /u /i"), on the other hand, regsvr32
00054      * calls DllInstall and then DllUnregisterServer as expected.
00055      *
00056      * This strange behavior forces us to specify -n (i.e. "regsvr32 /n /i").
00057      * Without -n, DllRegisterServer called before DllInstall would mistakenly
00058      * overwrite the default "PostgreSQL" event source registration.
00059      */
00060     if (bInstall)
00061         DllRegisterServer();
00062     return S_OK;
00063 }
00064 
00065 /*
00066  * DllRegisterServer --- Instructs DLL to create its registry entries
00067  */
00068 
00069 STDAPI
00070 DllRegisterServer(void)
00071 {
00072     HKEY        key;
00073     DWORD       data;
00074     char        buffer[_MAX_PATH];
00075     char        key_name[400];
00076 
00077     /* Set the name of DLL full path name. */
00078     if (!GetModuleFileName((HMODULE) g_module, buffer, sizeof(buffer)))
00079     {
00080         MessageBox(NULL, "Could not retrieve DLL filename", "PostgreSQL error", MB_OK | MB_ICONSTOP);
00081         return SELFREG_E_TYPELIB;
00082     }
00083 
00084     /*
00085      * Add PostgreSQL source name as a subkey under the Application key in the
00086      * EventLog registry key.
00087      */
00088     _snprintf(key_name, sizeof(key_name),
00089             "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
00090               event_source);
00091     if (RegCreateKey(HKEY_LOCAL_MACHINE, key_name, &key))
00092     {
00093         MessageBox(NULL, "Could not create the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
00094         return SELFREG_E_TYPELIB;
00095     }
00096 
00097     /* Add the name to the EventMessageFile subkey. */
00098     if (RegSetValueEx(key,
00099                       "EventMessageFile",
00100                       0,
00101                       REG_EXPAND_SZ,
00102                       (LPBYTE) buffer,
00103                       strlen(buffer) + 1))
00104     {
00105         MessageBox(NULL, "Could not set the event message file.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
00106         return SELFREG_E_TYPELIB;
00107     }
00108 
00109     /* Set the supported event types in the TypesSupported subkey. */
00110     data = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
00111 
00112     if (RegSetValueEx(key,
00113                       "TypesSupported",
00114                       0,
00115                       REG_DWORD,
00116                       (LPBYTE) &data,
00117                       sizeof(DWORD)))
00118     {
00119         MessageBox(NULL, "Could not set the supported types.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
00120         return SELFREG_E_TYPELIB;
00121     }
00122 
00123     RegCloseKey(key);
00124     return S_OK;
00125 }
00126 
00127 /*
00128  * DllUnregisterServer --- Instructs DLL to remove only those entries created through DllRegisterServer
00129  */
00130 
00131 STDAPI
00132 DllUnregisterServer(void)
00133 {
00134     char        key_name[400];
00135 
00136     /*
00137      * Remove PostgreSQL source name as a subkey under the Application key in
00138      * the EventLog registry key.
00139      */
00140 
00141     _snprintf(key_name, sizeof(key_name),
00142             "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
00143               event_source);
00144     if (RegDeleteKey(HKEY_LOCAL_MACHINE, key_name))
00145     {
00146         MessageBox(NULL, "Could not delete the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
00147         return SELFREG_E_TYPELIB;
00148     }
00149     return S_OK;
00150 }
00151 
00152 /*
00153  * DllMain --- is an optional entry point into a DLL.
00154  */
00155 
00156 BOOL        WINAPI
00157 DllMain(HANDLE hModule,
00158         DWORD ul_reason_for_call,
00159         LPVOID lpReserved
00160 )
00161 {
00162     if (ul_reason_for_call == DLL_PROCESS_ATTACH)
00163         g_module = hModule;
00164     return TRUE;
00165 }