2004-06-20 03:32:49 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pgevent.c
|
|
|
|
* Defines the entry point for pgevent dll.
|
2004-08-29 07:07:03 +02:00
|
|
|
* The DLL defines event source for backend
|
2004-06-20 03:32:49 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/bin/pgevent/pgevent.c
|
2004-06-20 03:32:49 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-09-27 21:16:03 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#include <olectl.h>
|
|
|
|
#include <string.h>
|
2011-10-25 20:02:55 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2004-06-20 03:32:49 +02:00
|
|
|
|
|
|
|
/* Global variables */
|
2004-08-29 07:07:03 +02:00
|
|
|
HANDLE g_module = NULL; /* hModule of DLL */
|
2004-06-20 03:32:49 +02:00
|
|
|
|
2011-10-25 20:02:55 +02:00
|
|
|
/*
|
|
|
|
* The event source is stored as a registry key.
|
|
|
|
* The maximum length of a registry key is 255 characters.
|
|
|
|
* http://msdn.microsoft.com/en-us/library/ms724872(v=vs.85).aspx
|
|
|
|
*/
|
|
|
|
char event_source[256] = "PostgreSQL";
|
|
|
|
|
2004-06-20 03:32:49 +02:00
|
|
|
/* Prototypes */
|
2011-10-25 20:02:55 +02:00
|
|
|
HRESULT DllInstall(BOOL bInstall, __in_opt LPCWSTR pszCmdLine);
|
|
|
|
STDAPI DllRegisterServer(void);
|
2004-08-29 07:07:03 +02:00
|
|
|
STDAPI DllUnregisterServer(void);
|
|
|
|
BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved);
|
2004-06-20 03:32:49 +02:00
|
|
|
|
2011-10-25 20:02:55 +02:00
|
|
|
/*
|
|
|
|
* DllInstall --- Passes the command line argument to DLL
|
|
|
|
*/
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
DllInstall(BOOL bInstall,
|
|
|
|
__in_opt LPCWSTR pszCmdLine)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
if (pszCmdLine && *pszCmdLine != '\0')
|
|
|
|
wcstombs_s(&ret, event_source, sizeof(event_source),
|
|
|
|
pszCmdLine, sizeof(event_source));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is an ugly hack due to the strange behavior of "regsvr32 /i".
|
|
|
|
*
|
|
|
|
* When installing, regsvr32 calls DllRegisterServer before DllInstall.
|
|
|
|
* When uninstalling (i.e. "regsvr32 /u /i"), on the other hand, regsvr32
|
|
|
|
* calls DllInstall and then DllUnregisterServer as expected.
|
|
|
|
*
|
|
|
|
* This strange behavior forces us to specify -n (i.e. "regsvr32 /n /i").
|
|
|
|
* Without -n, DllRegisterServer called before DllInstall would mistakenly
|
|
|
|
* overwrite the default "PostgreSQL" event source registration.
|
|
|
|
*/
|
|
|
|
if (bInstall)
|
|
|
|
DllRegisterServer();
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2004-06-20 03:32:49 +02:00
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* DllRegisterServer --- Instructs DLL to create its registry entries
|
2004-06-20 03:32:49 +02:00
|
|
|
*/
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
STDAPI
|
|
|
|
DllRegisterServer(void)
|
2004-06-20 03:32:49 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
HKEY key;
|
|
|
|
DWORD data;
|
|
|
|
char buffer[_MAX_PATH];
|
2011-10-25 20:02:55 +02:00
|
|
|
char key_name[400];
|
2004-06-20 03:32:49 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
/* Set the name of DLL full path name. */
|
|
|
|
if (!GetModuleFileName((HMODULE) g_module, buffer, sizeof(buffer)))
|
2004-06-20 03:32:49 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
MessageBox(NULL, "Could not retrieve DLL filename", "PostgreSQL error", MB_OK | MB_ICONSTOP);
|
|
|
|
return SELFREG_E_TYPELIB;
|
2004-06-20 03:32:49 +02:00
|
|
|
}
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Add PostgreSQL source name as a subkey under the Application key in the
|
|
|
|
* EventLog registry key.
|
2004-08-29 07:07:03 +02:00
|
|
|
*/
|
2011-10-25 20:02:55 +02:00
|
|
|
_snprintf(key_name, sizeof(key_name),
|
|
|
|
"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
|
|
|
|
event_source);
|
|
|
|
if (RegCreateKey(HKEY_LOCAL_MACHINE, key_name, &key))
|
2004-06-20 03:32:49 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
MessageBox(NULL, "Could not create the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
|
|
|
|
return SELFREG_E_TYPELIB;
|
2004-06-20 03:32:49 +02:00
|
|
|
}
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
/* Add the name to the EventMessageFile subkey. */
|
|
|
|
if (RegSetValueEx(key,
|
|
|
|
"EventMessageFile",
|
|
|
|
0,
|
|
|
|
REG_EXPAND_SZ,
|
|
|
|
(LPBYTE) buffer,
|
|
|
|
strlen(buffer) + 1))
|
2004-06-20 03:32:49 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
MessageBox(NULL, "Could not set the event message file.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
|
|
|
|
return SELFREG_E_TYPELIB;
|
2004-06-20 03:32:49 +02:00
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
|
|
|
|
/* Set the supported event types in the TypesSupported subkey. */
|
|
|
|
data = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
|
|
|
|
|
|
|
|
if (RegSetValueEx(key,
|
|
|
|
"TypesSupported",
|
|
|
|
0,
|
|
|
|
REG_DWORD,
|
2011-10-25 20:02:55 +02:00
|
|
|
(LPBYTE) & data,
|
2004-08-29 07:07:03 +02:00
|
|
|
sizeof(DWORD)))
|
2004-06-20 03:32:49 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
MessageBox(NULL, "Could not set the supported types.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
|
|
|
|
return SELFREG_E_TYPELIB;
|
2004-06-20 03:32:49 +02:00
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
|
|
|
|
RegCloseKey(key);
|
2004-06-20 03:32:49 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DllUnregisterServer --- Instructs DLL to remove only those entries created through DllRegisterServer
|
|
|
|
*/
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
STDAPI
|
|
|
|
DllUnregisterServer(void)
|
2004-06-20 03:32:49 +02:00
|
|
|
{
|
2011-10-25 20:02:55 +02:00
|
|
|
char key_name[400];
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Remove PostgreSQL source name as a subkey under the Application key in
|
|
|
|
* the EventLog registry key.
|
2004-08-29 07:07:03 +02:00
|
|
|
*/
|
|
|
|
|
2011-10-25 20:02:55 +02:00
|
|
|
_snprintf(key_name, sizeof(key_name),
|
|
|
|
"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
|
|
|
|
event_source);
|
|
|
|
if (RegDeleteKey(HKEY_LOCAL_MACHINE, key_name))
|
2004-06-20 03:32:49 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
MessageBox(NULL, "Could not delete the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
|
|
|
|
return SELFREG_E_TYPELIB;
|
2004-06-20 03:32:49 +02:00
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DllMain --- is an optional entry point into a DLL.
|
|
|
|
*/
|
|
|
|
|
2004-08-30 04:54:42 +02:00
|
|
|
BOOL WINAPI
|
2004-08-29 07:07:03 +02:00
|
|
|
DllMain(HANDLE hModule,
|
|
|
|
DWORD ul_reason_for_call,
|
|
|
|
LPVOID lpReserved
|
|
|
|
)
|
2004-06-20 03:32:49 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
|
2004-06-20 03:32:49 +02:00
|
|
|
g_module = hModule;
|
2004-08-29 07:07:03 +02:00
|
|
|
return TRUE;
|
2004-06-20 03:32:49 +02:00
|
|
|
}
|