6 Try to set up function pointers.
\r
7 In this first implementation it is not an error if we can't load them
\r
8 because we aren't currently trying to load any functions which we
\r
9 absolutely need. If we later add some indispensible imports we can
\r
10 return non-zero here to force an application exit.
\r
12 HMODULE get_dll(const TCHAR *dll, unsigned long *error) {
\r
15 HMODULE ret = LoadLibrary(dll);
\r
17 *error = GetLastError();
\r
18 if (*error != ERROR_PROC_NOT_FOUND) log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_LOADLIBRARY_FAILED, dll, error_string(*error), 0);
\r
24 FARPROC get_import(HMODULE library, const char *function, unsigned long *error) {
\r
27 FARPROC ret = GetProcAddress(library, function);
\r
29 *error = GetLastError();
\r
30 if (*error != ERROR_PROC_NOT_FOUND) {
\r
31 TCHAR *function_name;
\r
34 mbstowcs_s(&buflen, NULL, 0, function, _TRUNCATE);
\r
35 function_name = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, buflen * sizeof(TCHAR));
\r
36 if (function_name) mbstowcs_s(&buflen, function_name, buflen * sizeof(TCHAR), function, _TRUNCATE);
\r
38 function_name = (TCHAR *) function;
\r
40 log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_GETPROCADDRESS_FAILED, function_name, error_string(*error), 0);
\r
42 if (function_name) HeapFree(GetProcessHeap(), 0, function_name);
\r
51 unsigned long error;
\r
53 ZeroMemory(&imports, sizeof(imports));
\r
55 imports.kernel32 = get_dll(_T("kernel32.dll"), &error);
\r
56 if (imports.kernel32) {
\r
57 imports.AttachConsole = (AttachConsole_ptr) get_import(imports.kernel32, "AttachConsole", &error);
\r
58 if (! imports.AttachConsole) {
\r
59 if (error != ERROR_PROC_NOT_FOUND) return 2;
\r
62 imports.SleepConditionVariableCS = (SleepConditionVariableCS_ptr) get_import(imports.kernel32, "SleepConditionVariableCS", &error);
\r
63 if (! imports.SleepConditionVariableCS) {
\r
64 if (error != ERROR_PROC_NOT_FOUND) return 3;
\r
67 imports.WakeConditionVariable = (WakeConditionVariable_ptr) get_import(imports.kernel32, "WakeConditionVariable", &error);
\r
68 if (! imports.WakeConditionVariable) {
\r
69 if (error != ERROR_PROC_NOT_FOUND) return 4;
\r
72 else if (error != ERROR_MOD_NOT_FOUND) return 1;
\r
74 imports.advapi32 = get_dll(_T("advapi32.dll"), &error);
\r
75 if (imports.advapi32) {
\r
76 imports.CreateWellKnownSid = (CreateWellKnownSid_ptr) get_import(imports.advapi32, "CreateWellKnownSid", &error);
\r
77 if (! imports.CreateWellKnownSid) {
\r
78 if (error != ERROR_PROC_NOT_FOUND) return 6;
\r
80 imports.IsWellKnownSid = (IsWellKnownSid_ptr) get_import(imports.advapi32, "IsWellKnownSid", &error);
\r
81 if (! imports.IsWellKnownSid) {
\r
82 if (error != ERROR_PROC_NOT_FOUND) return 7;
\r
85 else if (error != ERROR_MOD_NOT_FOUND) return 5;
\r
90 void free_imports() {
\r
91 if (imports.kernel32) FreeLibrary(imports.kernel32);
\r
92 if (imports.advapi32) FreeLibrary(imports.advapi32);
\r
93 ZeroMemory(&imports, sizeof(imports));
\r