6 Try to set up function pointers.
7 In this first implementation it is not an error if we can't load them
8 because we aren't currently trying to load any functions which we
9 absolutely need. If we later add some indispensible imports we can
10 return non-zero here to force an application exit.
12 HMODULE get_dll(const TCHAR *dll, unsigned long *error) {
15 HMODULE ret = LoadLibrary(dll);
17 *error = GetLastError();
18 log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_LOADLIBRARY_FAILED, dll, error_string(*error), 0);
24 FARPROC get_import(HMODULE library, const char *function, unsigned long *error) {
27 FARPROC ret = GetProcAddress(library, function);
29 *error = GetLastError();
33 mbstowcs_s(&buflen, NULL, 0, function, _TRUNCATE);
34 function_name = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, buflen * sizeof(TCHAR));
35 if (function_name) mbstowcs_s(&buflen, function_name, buflen * sizeof(TCHAR), function, _TRUNCATE);
37 function_name = (TCHAR *) function;
39 log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_GETPROCADDRESS_FAILED, function_name, error_string(*error), 0);
41 if (function_name) HeapFree(GetProcessHeap(), 0, function_name);
51 ZeroMemory(&imports, sizeof(imports));
53 imports.kernel32 = get_dll(_T("kernel32.dll"), &error);
54 if (imports.kernel32) {
55 imports.AttachConsole = (AttachConsole_ptr) get_import(imports.kernel32, "AttachConsole", &error);
56 if (! imports.AttachConsole) {
57 if (error != ERROR_PROC_NOT_FOUND) return 2;
60 imports.SleepConditionVariableCS = (SleepConditionVariableCS_ptr) get_import(imports.kernel32, "SleepConditionVariableCS", &error);
61 if (! imports.SleepConditionVariableCS) {
62 if (error != ERROR_PROC_NOT_FOUND) return 3;
65 imports.WakeConditionVariable = (WakeConditionVariable_ptr) get_import(imports.kernel32, "WakeConditionVariable", &error);
66 if (! imports.WakeConditionVariable) {
67 if (error != ERROR_PROC_NOT_FOUND) return 4;
70 else if (error != ERROR_MOD_NOT_FOUND) return 1;
76 if (imports.kernel32) FreeLibrary(imports.kernel32);
77 ZeroMemory(&imports, sizeof(imports));