PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgevent.c File Reference
#include "postgres_fe.h"
#include <windows.h>
#include <olectl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for pgevent.c:

Go to the source code of this file.

Functions

HRESULT DllInstall (BOOL bInstall, LPCWSTR pszCmdLine)
 
STDAPI DllRegisterServer (void)
 
STDAPI DllUnregisterServer (void)
 
BOOL WINAPI DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
 

Variables

HANDLE g_module = NULL
 
char event_source [256] = DEFAULT_EVENT_SOURCE
 

Function Documentation

HRESULT DllInstall ( BOOL  bInstall,
LPCWSTR  pszCmdLine 
)

Definition at line 44 of file pgevent.c.

References DllRegisterServer(), and event_source.

46 {
47  if (pszCmdLine && *pszCmdLine != '\0')
48  wcstombs(event_source, pszCmdLine, sizeof(event_source));
49 
50  /*
51  * This is an ugly hack due to the strange behavior of "regsvr32 /i".
52  *
53  * When installing, regsvr32 calls DllRegisterServer before DllInstall.
54  * When uninstalling (i.e. "regsvr32 /u /i"), on the other hand, regsvr32
55  * calls DllInstall and then DllUnregisterServer as expected.
56  *
57  * This strange behavior forces us to specify -n (i.e. "regsvr32 /n /i").
58  * Without -n, DllRegisterServer called before DllInstall would mistakenly
59  * overwrite the default "PostgreSQL" event source registration.
60  */
61  if (bInstall)
63  return S_OK;
64 }
STDAPI DllRegisterServer(void)
Definition: pgevent.c:71
char event_source[256]
Definition: pgevent.c:31
BOOL WINAPI DllMain ( HANDLE  hModule,
DWORD  ul_reason_for_call,
LPVOID  lpReserved 
)

Definition at line 158 of file pgevent.c.

References g_module, and TRUE.

162 {
163  if (ul_reason_for_call == DLL_PROCESS_ATTACH)
164  g_module = hModule;
165  return TRUE;
166 }
HANDLE g_module
Definition: pgevent.c:24
#define TRUE
Definition: c.h:214
STDAPI DllRegisterServer ( void  )

Definition at line 71 of file pgevent.c.

References event_source, g_module, and NULL.

Referenced by DllInstall().

72 {
73  HKEY key;
74  DWORD data;
75  char buffer[_MAX_PATH];
76  char key_name[400];
77 
78  /* Set the name of DLL full path name. */
79  if (!GetModuleFileName((HMODULE) g_module, buffer, sizeof(buffer)))
80  {
81  MessageBox(NULL, "Could not retrieve DLL filename", "PostgreSQL error", MB_OK | MB_ICONSTOP);
82  return SELFREG_E_TYPELIB;
83  }
84 
85  /*
86  * Add PostgreSQL source name as a subkey under the Application key in the
87  * EventLog registry key.
88  */
89  _snprintf(key_name, sizeof(key_name),
90  "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
91  event_source);
92  if (RegCreateKey(HKEY_LOCAL_MACHINE, key_name, &key))
93  {
94  MessageBox(NULL, "Could not create the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
95  return SELFREG_E_TYPELIB;
96  }
97 
98  /* Add the name to the EventMessageFile subkey. */
99  if (RegSetValueEx(key,
100  "EventMessageFile",
101  0,
102  REG_EXPAND_SZ,
103  (LPBYTE) buffer,
104  strlen(buffer) + 1))
105  {
106  MessageBox(NULL, "Could not set the event message file.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
107  return SELFREG_E_TYPELIB;
108  }
109 
110  /* Set the supported event types in the TypesSupported subkey. */
111  data = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
112 
113  if (RegSetValueEx(key,
114  "TypesSupported",
115  0,
116  REG_DWORD,
117  (LPBYTE) &data,
118  sizeof(DWORD)))
119  {
120  MessageBox(NULL, "Could not set the supported types.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
121  return SELFREG_E_TYPELIB;
122  }
123 
124  RegCloseKey(key);
125  return S_OK;
126 }
HANDLE g_module
Definition: pgevent.c:24
#define NULL
Definition: c.h:226
char event_source[256]
Definition: pgevent.c:31
STDAPI DllUnregisterServer ( void  )

Definition at line 133 of file pgevent.c.

References event_source, and NULL.

134 {
135  char key_name[400];
136 
137  /*
138  * Remove PostgreSQL source name as a subkey under the Application key in
139  * the EventLog registry key.
140  */
141 
142  _snprintf(key_name, sizeof(key_name),
143  "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
144  event_source);
145  if (RegDeleteKey(HKEY_LOCAL_MACHINE, key_name))
146  {
147  MessageBox(NULL, "Could not delete the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
148  return SELFREG_E_TYPELIB;
149  }
150  return S_OK;
151 }
#define NULL
Definition: c.h:226
char event_source[256]
Definition: pgevent.c:31

Variable Documentation

char event_source[256] = DEFAULT_EVENT_SOURCE

Definition at line 31 of file pgevent.c.

Referenced by DllInstall(), DllRegisterServer(), and DllUnregisterServer().

HANDLE g_module = NULL

Definition at line 24 of file pgevent.c.

Referenced by DllMain(), and DllRegisterServer().