rundll32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * PURPOSE: Load a DLL and run an entry point with the specified parameters
  3. *
  4. * Copyright 2002 Alberto Massari
  5. * Copyright 2001-2003 Aric Stewart for CodeWeavers
  6. * Copyright 2003 Mike McCormack for CodeWeavers
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. *
  22. */
  23. /*
  24. *
  25. * rundll32 dllname,entrypoint [arguments]
  26. *
  27. * Documentation for this utility found on KB Q164787
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. /* Exclude rarely-used stuff from Windows headers */
  34. #define WIN32_LEAN_AND_MEAN
  35. #include "windows.h"
  36. #include "wine/winbase16.h"
  37. #include "wine/asm.h"
  38. #include "wine/debug.h"
  39. WINE_DEFAULT_DEBUG_CHANNEL(rundll32);
  40. #ifdef __i386__
  41. /* wrapper for dlls that declare the entry point incorrectly */
  42. extern void call_entry_point( void *func, HWND hwnd, HINSTANCE inst, void *cmdline, int show );
  43. __ASM_GLOBAL_FUNC( call_entry_point,
  44. "pushl %ebp\n\t"
  45. __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
  46. __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
  47. "movl %esp,%ebp\n\t"
  48. __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
  49. "pushl %edi\n\t"
  50. __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
  51. "pushl %esi\n\t"
  52. __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
  53. "pushl %ebx\n\t"
  54. __ASM_CFI(".cfi_rel_offset %ebx,-12\n\t")
  55. "subl $12,%esp\n\t"
  56. "pushl 24(%ebp)\n\t"
  57. "pushl 20(%ebp)\n\t"
  58. "pushl 16(%ebp)\n\t"
  59. "pushl 12(%ebp)\n\t"
  60. "call *8(%ebp)\n\t"
  61. "leal -12(%ebp),%esp\n\t"
  62. "popl %ebx\n\t"
  63. __ASM_CFI(".cfi_same_value %ebx\n\t")
  64. "popl %esi\n\t"
  65. __ASM_CFI(".cfi_same_value %esi\n\t")
  66. "popl %edi\n\t"
  67. __ASM_CFI(".cfi_same_value %edi\n\t")
  68. "leave\n\t"
  69. __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
  70. __ASM_CFI(".cfi_same_value %ebp\n\t")
  71. "ret" )
  72. #else
  73. static void call_entry_point( void *func, HWND hwnd, HINSTANCE inst, void *cmdline, int show )
  74. {
  75. void (WINAPI *entry_point)( HWND hwnd, HINSTANCE inst, void *cmdline, int show ) = func;
  76. entry_point( hwnd, inst, cmdline, show );
  77. }
  78. #endif
  79. static HINSTANCE16 (WINAPI *pLoadLibrary16)(LPCSTR libname);
  80. static FARPROC16 (WINAPI *pGetProcAddress16)(HMODULE16 hModule, LPCSTR name);
  81. static void (WINAPI *pRunDLL_CallEntry16)( FARPROC proc, HWND hwnd, HINSTANCE inst,
  82. LPCSTR cmdline, INT cmdshow );
  83. /*
  84. * Control_RunDLL needs to have a window. So lets make us a very simple window class.
  85. */
  86. static ATOM register_class(void)
  87. {
  88. WNDCLASSEXW wcex;
  89. wcex.cbSize = sizeof(WNDCLASSEXW);
  90. wcex.style = CS_HREDRAW | CS_VREDRAW;
  91. wcex.lpfnWndProc = DefWindowProcW;
  92. wcex.cbClsExtra = 0;
  93. wcex.cbWndExtra = 0;
  94. wcex.hInstance = NULL;
  95. wcex.hIcon = NULL;
  96. wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
  97. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  98. wcex.lpszMenuName = NULL;
  99. wcex.lpszClassName = L"class_rundll32";
  100. wcex.hIconSm = NULL;
  101. return RegisterClassExW(&wcex);
  102. }
  103. static HINSTANCE16 load_dll16( LPCWSTR dll )
  104. {
  105. HINSTANCE16 ret = 0;
  106. DWORD len = WideCharToMultiByte( CP_ACP, 0, dll, -1, NULL, 0, NULL, NULL );
  107. char *dllA = HeapAlloc( GetProcessHeap(), 0, len );
  108. if (dllA)
  109. {
  110. WideCharToMultiByte( CP_ACP, 0, dll, -1, dllA, len, NULL, NULL );
  111. pLoadLibrary16 = (void *)GetProcAddress( GetModuleHandleW(L"kernel32.dll"), (LPCSTR)35 );
  112. if (pLoadLibrary16) ret = pLoadLibrary16( dllA );
  113. HeapFree( GetProcessHeap(), 0, dllA );
  114. }
  115. return ret;
  116. }
  117. static FARPROC16 get_entry_point16( HINSTANCE16 inst, LPCWSTR entry )
  118. {
  119. FARPROC16 ret = 0;
  120. DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
  121. char *entryA = HeapAlloc( GetProcessHeap(), 0, len );
  122. if (entryA)
  123. {
  124. WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );
  125. pGetProcAddress16 = (void *)GetProcAddress( GetModuleHandleW(L"kernel32.dll"), (LPCSTR)37 );
  126. if (pGetProcAddress16) ret = pGetProcAddress16( inst, entryA );
  127. HeapFree( GetProcessHeap(), 0, entryA );
  128. }
  129. return ret;
  130. }
  131. static void *get_entry_point32( HMODULE module, LPCWSTR entry, BOOL *unicode )
  132. {
  133. void *ret;
  134. /* determine if the entry point is an ordinal */
  135. if (entry[0] == '#')
  136. {
  137. INT_PTR ordinal = wcstol( entry + 1, NULL, 10 );
  138. if (ordinal <= 0)
  139. return NULL;
  140. *unicode = TRUE;
  141. ret = GetProcAddress( module, (LPCSTR)ordinal );
  142. }
  143. else
  144. {
  145. DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
  146. char *entryA = HeapAlloc( GetProcessHeap(), 0, len + 1 );
  147. if (!entryA)
  148. return NULL;
  149. WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );
  150. /* first try the W version */
  151. *unicode = TRUE;
  152. strcat( entryA, "W" );
  153. if (!(ret = GetProcAddress( module, entryA )))
  154. {
  155. /* now the A version */
  156. *unicode = FALSE;
  157. entryA[strlen(entryA)-1] = 'A';
  158. if (!(ret = GetProcAddress( module, entryA )))
  159. {
  160. /* now the version without suffix */
  161. entryA[strlen(entryA)-1] = 0;
  162. ret = GetProcAddress( module, entryA );
  163. }
  164. }
  165. HeapFree( GetProcessHeap(), 0, entryA );
  166. }
  167. return ret;
  168. }
  169. static LPWSTR get_next_arg(LPWSTR *cmdline)
  170. {
  171. LPWSTR s;
  172. LPWSTR arg,d;
  173. BOOL in_quotes;
  174. int bcount,len=0;
  175. /* count the chars */
  176. bcount=0;
  177. in_quotes=FALSE;
  178. s=*cmdline;
  179. while (1) {
  180. if (*s==0 || ((*s=='\t' || *s==' ') && !in_quotes)) {
  181. /* end of this command line argument */
  182. break;
  183. } else if (*s=='\\') {
  184. /* '\', count them */
  185. bcount++;
  186. } else if ((*s=='"') && ((bcount & 1)==0)) {
  187. /* unescaped '"' */
  188. in_quotes=!in_quotes;
  189. bcount=0;
  190. } else {
  191. /* a regular character */
  192. bcount=0;
  193. }
  194. s++;
  195. len++;
  196. }
  197. arg=HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
  198. if (!arg)
  199. return NULL;
  200. bcount=0;
  201. in_quotes=FALSE;
  202. d=arg;
  203. s=*cmdline;
  204. while (*s) {
  205. if ((*s=='\t' || *s==' ') && !in_quotes) {
  206. /* end of this command line argument */
  207. break;
  208. } else if (*s=='\\') {
  209. /* '\\' */
  210. *d++=*s++;
  211. bcount++;
  212. } else if (*s=='"') {
  213. /* '"' */
  214. if ((bcount & 1)==0) {
  215. /* Preceded by an even number of '\', this is half that
  216. * number of '\', plus a quote which we erase.
  217. */
  218. d-=bcount/2;
  219. in_quotes=!in_quotes;
  220. s++;
  221. } else {
  222. /* Preceded by an odd number of '\', this is half that
  223. * number of '\' followed by a '"'
  224. */
  225. d=d-bcount/2-1;
  226. *d++='"';
  227. s++;
  228. }
  229. bcount=0;
  230. } else {
  231. /* a regular character */
  232. *d++=*s++;
  233. bcount=0;
  234. }
  235. }
  236. *d=0;
  237. *cmdline=s;
  238. /* skip the remaining spaces */
  239. while (**cmdline=='\t' || **cmdline==' ') {
  240. (*cmdline)++;
  241. }
  242. return arg;
  243. }
  244. int WINAPI wWinMain(HINSTANCE instance, HINSTANCE hOldInstance, LPWSTR szCmdLine, int nCmdShow)
  245. {
  246. HWND hWnd;
  247. LPWSTR szDllName,szEntryPoint;
  248. void *entry_point;
  249. BOOL unicode = FALSE, win16;
  250. STARTUPINFOW info;
  251. HMODULE hDll;
  252. hWnd=NULL;
  253. hDll=NULL;
  254. szDllName=NULL;
  255. /* Initialize the rundll32 class */
  256. register_class();
  257. hWnd = CreateWindowW(L"class_rundll32", L"rundll32", WS_OVERLAPPEDWINDOW|WS_VISIBLE,
  258. CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, NULL, NULL);
  259. /* Get the dll name and API EntryPoint */
  260. WINE_TRACE("CmdLine=%s\n",wine_dbgstr_w(szCmdLine));
  261. szDllName = get_next_arg(&szCmdLine);
  262. if (!szDllName || *szDllName==0)
  263. goto CLEANUP;
  264. WINE_TRACE("DllName=%s\n",wine_dbgstr_w(szDllName));
  265. if ((szEntryPoint = wcschr(szDllName, ',' )))
  266. *szEntryPoint++=0;
  267. else
  268. szEntryPoint = get_next_arg(&szCmdLine);
  269. WINE_TRACE("EntryPoint=%s\n",wine_dbgstr_w(szEntryPoint));
  270. /* Load the library */
  271. hDll=LoadLibraryW(szDllName);
  272. if (hDll)
  273. {
  274. win16 = FALSE;
  275. entry_point = get_entry_point32( hDll, szEntryPoint, &unicode );
  276. }
  277. else
  278. {
  279. HINSTANCE16 dll = load_dll16( szDllName );
  280. if (dll <= 32)
  281. {
  282. /* Windows has a MessageBox here... */
  283. WINE_ERR("Unable to load %s\n",wine_dbgstr_w(szDllName));
  284. goto CLEANUP;
  285. }
  286. win16 = TRUE;
  287. unicode = FALSE;
  288. entry_point = get_entry_point16( dll, szEntryPoint );
  289. }
  290. if (!entry_point)
  291. {
  292. /* Windows has a MessageBox here... */
  293. WINE_ERR( "Unable to find the entry point %s in %s\n",
  294. wine_dbgstr_w(szEntryPoint), wine_dbgstr_w(szDllName) );
  295. goto CLEANUP;
  296. }
  297. GetStartupInfoW( &info );
  298. if (!(info.dwFlags & STARTF_USESHOWWINDOW)) info.wShowWindow = SW_SHOWDEFAULT;
  299. if (unicode)
  300. {
  301. WINE_TRACE( "Calling %s (%p,%p,%s,%d)\n", wine_dbgstr_w(szEntryPoint),
  302. hWnd, instance, wine_dbgstr_w(szCmdLine), info.wShowWindow );
  303. call_entry_point( entry_point, hWnd, instance, szCmdLine, info.wShowWindow );
  304. }
  305. else
  306. {
  307. DWORD len = WideCharToMultiByte( CP_ACP, 0, szCmdLine, -1, NULL, 0, NULL, NULL );
  308. char *cmdline = HeapAlloc( GetProcessHeap(), 0, len );
  309. if (!cmdline)
  310. goto CLEANUP;
  311. WideCharToMultiByte( CP_ACP, 0, szCmdLine, -1, cmdline, len, NULL, NULL );
  312. WINE_TRACE( "Calling %s (%p,%p,%s,%d)\n", wine_dbgstr_w(szEntryPoint),
  313. hWnd, instance, wine_dbgstr_a(cmdline), info.wShowWindow );
  314. if (win16)
  315. {
  316. HMODULE shell = LoadLibraryW( L"shell32.dll" );
  317. if (shell) pRunDLL_CallEntry16 = (void *)GetProcAddress( shell, (LPCSTR)122 );
  318. if (pRunDLL_CallEntry16)
  319. pRunDLL_CallEntry16( entry_point, hWnd, instance, cmdline, info.wShowWindow );
  320. }
  321. else call_entry_point( entry_point, hWnd, instance, cmdline, info.wShowWindow );
  322. HeapFree( GetProcessHeap(), 0, cmdline );
  323. }
  324. CLEANUP:
  325. if (hWnd)
  326. DestroyWindow(hWnd);
  327. if (hDll)
  328. FreeLibrary(hDll);
  329. HeapFree(GetProcessHeap(),0,szDllName);
  330. return 0; /* rundll32 always returns 0! */
  331. }