Watt-32 tcp/ip  2.2 dev-rel.10
win_dll.c
Go to the documentation of this file.
1 
32 #include "wattcp.h"
33 #include "strings.h"
34 #include "misc.h"
35 #include "pcdbug.h"
36 
37 #if defined(WIN32) /* Rest of file */
38 
39 #include "packet32.h"
40 #include "win_dll.h"
41 
42 /* Function-ptr for functions in these DLLs:
43  */
44 func_GetComputerNameExA p_GetComputerNameExA = NULL; /* Kernel32.dll, >= Win-2000 Pro */
45 func_GetFileVersionInfoSizeA p_GetFileVersionInfoSizeA = NULL; /* version.dll */
46 func_GetFileVersionInfoA p_GetFileVersionInfoA = NULL; /* version.dll */
47 /* func_GetFileVersionInfoExA p_GetFileVersionInfoExA = NULL; */ /* version.dll, >= Win-Vista */
48 func_VerQueryValueA p_VerQueryValueA = NULL; /* version.dll, >= Win-2000 Pro */
49 func_QueryThreadCycleTime p_QueryThreadCycleTime = NULL; /* kernel32.dll, >= Win-Vista */
50 func_NtQueryInformationThread p_NtQueryInformationThread = NULL; /* ntdll.dll, >= Win-2003 */
51 /* func_GetThreadInformation p_GetThreadInformation = NULL; */ /* Kernel32.dll, >= Win-8 */
52 func_IsWow64Process p_IsWow64Process = NULL; /* Kernel32.dll, >= Win-XP SP2 */
53 
54 /* Function-ptr for functions in WinPacket.dll:
55  * (the WinPcap low-level NetMon wrapper library)
56  */
57 func_WanPacketSetBpfFilter p_WanPacketSetBpfFilter = NULL;
58 func_WanPacketOpenAdapter p_WanPacketOpenAdapter = NULL;
59 func_WanPacketCloseAdapter p_WanPacketCloseAdapter = NULL;
60 func_WanPacketSetBufferSize p_WanPacketSetBufferSize = NULL;
61 func_WanPacketReceivePacket p_WanPacketReceivePacket = NULL;
62 func_WanPacketSetMinToCopy p_WanPacketSetMinToCopy = NULL;
63 func_WanPacketGetStats p_WanPacketGetStats = NULL;
64 func_WanPacketSetReadTimeout p_WanPacketSetReadTimeout = NULL;
65 func_WanPacketSetMode p_WanPacketSetMode = NULL;
66 func_WanPacketGetReadEvent p_WanPacketGetReadEvent = NULL;
67 func_WanPacketTestAdapter p_WanPacketTestAdapter = NULL;
68 
69 #if defined(HAVE_WINDNS_H)
70  func_DnsFree p_DnsFree = NULL;
71  func_DnsQuery_A p_DnsQuery_A = NULL;
72  func_DnsModifyRecordsInSet_A p_DnsModifyRecordsInSet_A = NULL;
73 #endif
74 
75 #if defined(USE_BUGTRAP)
76  func_BT_InstallSehFilter p_BT_InstallSehFilter = NULL;
77  func_BT_SetPreErrHandler p_BT_SetPreErrHandler = NULL;
78  func_BT_SetAppVersion p_BT_SetAppVersion = NULL;
79  func_BT_SetAppName p_BT_SetAppName = NULL;
80  func_BT_SetReportFormat p_BT_SetReportFormat = NULL;
81  func_BT_SetSupportEMail p_BT_SetSupportEMail = NULL;
82  func_BT_SetFlags p_BT_SetFlags = NULL;
83  func_BT_SetLogSizeInEntries p_BT_SetLogSizeInEntries = NULL;
84  func_BT_SetLogFlags p_BT_SetLogFlags = NULL;
85  func_BT_SetLogEchoMode p_BT_SetLogEchoMode = NULL;
86  func_BT_InsLogEntryF p_BT_InsLogEntryF = NULL;
87  func_BT_OpenLogFile p_BT_OpenLogFile = NULL;
88  func_BT_CloseLogFile p_BT_CloseLogFile = NULL;
89  func_BT_AddLogFile p_BT_AddLogFile = NULL;
90 #endif
91 
92 #if defined(UNICODE) || defined(_UNICODE)
93  #define BUGTRAP_DLL L"BugTrapU.dll"
94 #else
95  #define BUGTRAP_DLL "BugTrap.dll"
96 #endif
97 
98 #define ADD_VALUE(dll, func) { NULL, _T(dll), #func, (void**)&p_##func }
99 
100 struct LoadTable dyn_funcs [] = {
101  { NULL, _T("kernel32.dll"), "QueryThreadCycleTime", (void**)&p_QueryThreadCycleTime },
102  { NULL, _T("kernel32.dll"), "GetComputerNameExA", (void**)&p_GetComputerNameExA },
103  { NULL, _T("kernel32.dll"), "IsWow64Process", (void**)&p_IsWow64Process },
104  { NULL, _T("version.dll"), "GetFileVersionInfoSizeA", (void**)&p_GetFileVersionInfoSizeA },
105  { NULL, _T("version.dll"), "GetFileVersionInfoA", (void**)&p_GetFileVersionInfoA },
106  { NULL, _T("version.dll"), "VerQueryValueA", (void**)&p_VerQueryValueA },
107  { NULL, _T("ntdll.dll"), "NtQueryInformationThread",(void**)&p_NtQueryInformationThread },
108  { NULL, _T("WanPacket.dll"), "WanPacketSetBpfFilter", (void**)&p_WanPacketSetBpfFilter },
109  { NULL, _T("WanPacket.dll"), "WanPacketOpenAdapter", (void**)&p_WanPacketOpenAdapter },
110  { NULL, _T("WanPacket.dll"), "WanPacketCloseAdapter", (void**)&p_WanPacketCloseAdapter },
111  { NULL, _T("WanPacket.dll"), "WanPacketSetBufferSize", (void**)&p_WanPacketSetBufferSize },
112  { NULL, _T("WanPacket.dll"), "WanPacketReceivePacket", (void**)&p_WanPacketReceivePacket },
113  { NULL, _T("WanPacket.dll"), "WanPacketSetMinToCopy", (void**)&p_WanPacketSetMinToCopy },
114  { NULL, _T("WanPacket.dll"), "WanPacketGetStats", (void**)&p_WanPacketGetStats },
115  { NULL, _T("WanPacket.dll"), "WanPacketSetReadTimeout", (void**)&p_WanPacketSetReadTimeout },
116  { NULL, _T("WanPacket.dll"), "WanPacketSetMode", (void**)&p_WanPacketSetMode },
117  { NULL, _T("WanPacket.dll"), "WanPacketGetReadEvent", (void**)&p_WanPacketGetReadEvent },
118  { NULL, _T("WanPacket.dll"), "WanPacketTestAdapter", (void**)&p_WanPacketTestAdapter },
119 #if defined(HAVE_WINDNS_H)
120  { NULL, _T("dnsapi.dll"), "DnsQuery_A", (void**)&p_DnsQuery_A },
121  { NULL, _T("dnsapi.dll"), "DnsFree", (void**)&p_DnsFree },
122  { NULL, _T("dnsapi.dll"), "DnsModifyRecordsInSet_A", (void**)&p_DnsModifyRecordsInSet_A },
123 #endif
124 #if defined(USE_BUGTRAP)
125  { NULL, BUGTRAP_DLL, "BT_InstallSehFilter", (void**)&p_BT_InstallSehFilter },
126  { NULL, BUGTRAP_DLL, "BT_SetPreErrHandler", (void**)&p_BT_SetPreErrHandler },
127  { NULL, BUGTRAP_DLL, "BT_SetAppVersion", (void**)&p_BT_SetAppVersion },
128  { NULL, BUGTRAP_DLL, "BT_SetAppName", (void**)&p_BT_SetAppName },
129  { NULL, BUGTRAP_DLL, "BT_SetReportFormat", (void**)&p_BT_SetReportFormat },
130  { NULL, BUGTRAP_DLL, "BT_SetSupportEMail", (void**)&p_BT_SetSupportEMail },
131  { NULL, BUGTRAP_DLL, "BT_SetFlags", (void**)&p_BT_SetFlags },
132  { NULL, BUGTRAP_DLL, "BT_SetLogSizeInEntries", (void**)&p_BT_SetLogSizeInEntries },
133  { NULL, BUGTRAP_DLL, "BT_SetLogFlags", (void**)&p_BT_SetLogFlags },
134  { NULL, BUGTRAP_DLL, "BT_SetLogEchoMode", (void**)&p_BT_SetLogEchoMode },
135  { NULL, BUGTRAP_DLL, "BT_InsLogEntryF", (void**)&p_BT_InsLogEntryF },
136  { NULL, BUGTRAP_DLL, "BT_OpenLogFile", (void**)&p_BT_OpenLogFile },
137  { NULL, BUGTRAP_DLL, "BT_CloseLogFile", (void**)&p_BT_CloseLogFile },
138  { NULL, BUGTRAP_DLL, "BT_AddLogFile", (void**)&p_BT_AddLogFile },
139 #endif
140  };
141 
142 size_t dyn_funcs_num = DIM (dyn_funcs);
143 
144 /*
145  * Handling of dynamic loading and unloading of DLLs and their functions.
146  */
147 int load_dynamic_table (struct LoadTable *tab, int tab_size)
148 {
149  int i;
150 
151  for (i = 0; i < tab_size; tab++, i++)
152  {
153  HINSTANCE mod_handle;
154 
155  if (!_watt_use_bugtrap && !_tcsicmp(tab->mod_name,BUGTRAP_DLL))
156  continue;
157 
158  if (i > 0 && !_tcsicmp(tab->mod_name, (tab-1)->mod_name))
159  mod_handle = (tab-1)->mod_handle;
160  else mod_handle = LoadLibrary (tab->mod_name);
161 
162  if (mod_handle && mod_handle != INVALID_HANDLE_VALUE)
163  {
164  FARPROC addr = GetProcAddress (mod_handle, tab->func_name);
165 
166  if (!addr)
167  CONSOLE_MSG (4, ("Function \"%s\" not found in %" TSTR2ASCII_FMT ".\n",
168  tab->func_name, tab->mod_name));
169  *tab->func_addr = addr;
170  }
171  tab->mod_handle = mod_handle;
172 
173  CONSOLE_MSG (4, ("%2d: Module 0x%08lX/%" TSTR2ASCII_FMT ", func \"%s\" -> 0x%" ADDR_FMT ".\n",
174  i, HandleToUlong(tab->mod_handle), tab->mod_name, tab->func_name,
175  ADDR_CAST(*tab->func_addr)));
176  }
177  return (i);
178 }
179 
180 int unload_dynamic_table (struct LoadTable *tab, int tab_size)
181 {
182  int i;
183 
184  for (i = 0; i < tab_size; tab++, i++)
185  {
186  int m_unload = 0;
187  int f_unload = 0;
188 
189  if (tab->mod_handle && tab->mod_handle != INVALID_HANDLE_VALUE)
190  {
191  FreeLibrary (tab->mod_handle);
192  m_unload = 1;
193  }
194  tab->mod_handle = INVALID_HANDLE_VALUE;
195 
196  if (*tab->func_addr)
197  f_unload = 1;
198  *tab->func_addr = NULL;
199 
200  CONSOLE_MSG (4, ("%2d: function \"%s\" %s. Module \"%" TSTR2ASCII_FMT "\" %s.\n",
201  i, tab->func_name, f_unload ? "freed" : "not used",
202  tab->mod_name, m_unload ? "unloaded" : "not used"));
203  }
204  return (i);
205 }
206 #endif /* WIN32 */
207 
Core definitions.