rundll32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 void (WINAPI *pRunDLL_CallEntry16)( FARPROC proc, HWND hwnd, HINSTANCE inst,
  80. LPCSTR cmdline, INT cmdshow );
  81. /*
  82. * Control_RunDLL needs to have a window. So lets make us a very simple window class.
  83. */
  84. static ATOM register_class(void)
  85. {
  86. WNDCLASSEXW wcex;
  87. wcex.cbSize = sizeof(WNDCLASSEXW);
  88. wcex.style = CS_HREDRAW | CS_VREDRAW;
  89. wcex.lpfnWndProc = DefWindowProcW;
  90. wcex.cbClsExtra = 0;
  91. wcex.cbWndExtra = 0;
  92. wcex.hInstance = NULL;
  93. wcex.hIcon = NULL;
  94. wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
  95. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  96. wcex.lpszMenuName = NULL;
  97. wcex.lpszClassName = L"class_rundll32";
  98. wcex.hIconSm = NULL;
  99. return RegisterClassExW(&wcex);
  100. }
  101. #ifdef __i386__
  102. static HINSTANCE16 load_dll16( LPCWSTR dll )
  103. {
  104. HINSTANCE16 (WINAPI *pLoadLibrary16)(LPCSTR libname);
  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 (WINAPI *pGetProcAddress16)(HMODULE16 hModule, LPCSTR name);
  120. FARPROC16 ret = 0;
  121. DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
  122. char *entryA = HeapAlloc( GetProcessHeap(), 0, len );
  123. if (entryA)
  124. {
  125. WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );
  126. pGetProcAddress16 = (void *)GetProcAddress( GetModuleHandleW(L"kernel32.dll"), (LPCSTR)37 );
  127. if (pGetProcAddress16) ret = pGetProcAddress16( inst, entryA );
  128. HeapFree( GetProcessHeap(), 0, entryA );
  129. }
  130. return ret;
  131. }
  132. #endif
  133. static void *get_entry_point32( HMODULE module, LPCWSTR entry, BOOL *unicode )
  134. {
  135. void *ret;
  136. /* determine if the entry point is an ordinal */
  137. if (entry[0] == '#')
  138. {
  139. INT_PTR ordinal = wcstol( entry + 1, NULL, 10 );
  140. if (ordinal <= 0)
  141. return NULL;
  142. *unicode = TRUE;
  143. ret = GetProcAddress( module, (LPCSTR)ordinal );
  144. }
  145. else
  146. {
  147. DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
  148. char *entryA = HeapAlloc( GetProcessHeap(), 0, len + 1 );
  149. if (!entryA)
  150. return NULL;
  151. WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );
  152. /* first try the W version */
  153. *unicode = TRUE;
  154. strcat( entryA, "W" );
  155. if (!(ret = GetProcAddress( module, entryA )))
  156. {
  157. /* now the A version */
  158. *unicode = FALSE;
  159. entryA[strlen(entryA)-1] = 'A';
  160. if (!(ret = GetProcAddress( module, entryA )))
  161. {
  162. /* now the version without suffix */
  163. entryA[strlen(entryA)-1] = 0;
  164. ret = GetProcAddress( module, entryA );
  165. }
  166. }
  167. HeapFree( GetProcessHeap(), 0, entryA );
  168. }
  169. return ret;
  170. }
  171. static LPWSTR get_next_arg(LPWSTR *cmdline)
  172. {
  173. LPWSTR s;
  174. LPWSTR arg,d;
  175. BOOL in_quotes;
  176. int bcount,len=0;
  177. /* count the chars */
  178. bcount=0;
  179. in_quotes=FALSE;
  180. s=*cmdline;
  181. while (1) {
  182. if (*s==0 || ((*s=='\t' || *s==' ') && !in_quotes)) {
  183. /* end of this command line argument */
  184. break;
  185. } else if (*s=='\\') {
  186. /* '\', count them */
  187. bcount++;
  188. } else if ((*s=='"') && ((bcount & 1)==0)) {
  189. /* unescaped '"' */
  190. in_quotes=!in_quotes;
  191. bcount=0;
  192. } else {
  193. /* a regular character */
  194. bcount=0;
  195. }
  196. s++;
  197. len++;
  198. }
  199. arg=HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
  200. if (!arg)
  201. return NULL;
  202. bcount=0;
  203. in_quotes=FALSE;
  204. d=arg;
  205. s=*cmdline;
  206. while (*s) {
  207. if ((*s=='\t' || *s==' ') && !in_quotes) {
  208. /* end of this command line argument */
  209. break;
  210. } else if (*s=='\\') {
  211. /* '\\' */
  212. *d++=*s++;
  213. bcount++;
  214. } else if (*s=='"') {
  215. /* '"' */
  216. if ((bcount & 1)==0) {
  217. /* Preceded by an even number of '\', this is half that
  218. * number of '\', plus a quote which we erase.
  219. */
  220. d-=bcount/2;
  221. in_quotes=!in_quotes;
  222. s++;
  223. } else {
  224. /* Preceded by an odd number of '\', this is half that
  225. * number of '\' followed by a '"'
  226. */
  227. d=d-bcount/2-1;
  228. *d++='"';
  229. s++;
  230. }
  231. bcount=0;
  232. } else {
  233. /* a regular character */
  234. *d++=*s++;
  235. bcount=0;
  236. }
  237. }
  238. *d=0;
  239. *cmdline=s;
  240. /* skip the remaining spaces */
  241. while (**cmdline=='\t' || **cmdline==' ') {
  242. (*cmdline)++;
  243. }
  244. return arg;
  245. }
  246. int WINAPI wWinMain(HINSTANCE instance, HINSTANCE hOldInstance, LPWSTR szCmdLine, int nCmdShow)
  247. {
  248. HWND hWnd;
  249. LPWSTR szDllName,szEntryPoint;
  250. void *entry_point = NULL;
  251. BOOL unicode = FALSE, win16 = FALSE;
  252. STARTUPINFOW info;
  253. HMODULE hDll;
  254. hWnd=NULL;
  255. hDll=NULL;
  256. szDllName=NULL;
  257. /* Initialize the rundll32 class */
  258. register_class();
  259. hWnd = CreateWindowW(L"class_rundll32", L"rundll32", WS_OVERLAPPEDWINDOW|WS_VISIBLE,
  260. CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, NULL, NULL);
  261. /* Get the dll name and API EntryPoint */
  262. WINE_TRACE("CmdLine=%s\n",wine_dbgstr_w(szCmdLine));
  263. szDllName = get_next_arg(&szCmdLine);
  264. if (!szDllName || *szDllName==0)
  265. goto CLEANUP;
  266. WINE_TRACE("DllName=%s\n",wine_dbgstr_w(szDllName));
  267. if ((szEntryPoint = wcschr(szDllName, ',' )))
  268. *szEntryPoint++=0;
  269. else
  270. szEntryPoint = get_next_arg(&szCmdLine);
  271. WINE_TRACE("EntryPoint=%s\n",wine_dbgstr_w(szEntryPoint));
  272. /* Load the library */
  273. hDll=LoadLibraryW(szDllName);
  274. if (hDll) entry_point = get_entry_point32( hDll, szEntryPoint, &unicode );
  275. #ifdef __i386__
  276. else
  277. {
  278. HINSTANCE16 dll = load_dll16( szDllName );
  279. if (dll <= 32)
  280. {
  281. /* Windows has a MessageBox here... */
  282. WINE_ERR("Unable to load %s\n",wine_dbgstr_w(szDllName));
  283. goto CLEANUP;
  284. }
  285. win16 = TRUE;
  286. entry_point = get_entry_point16( dll, szEntryPoint );
  287. }
  288. #endif
  289. if (!entry_point)
  290. {
  291. /* Windows has a MessageBox here... */
  292. WINE_ERR( "Unable to find the entry point %s in %s\n",
  293. wine_dbgstr_w(szEntryPoint), wine_dbgstr_w(szDllName) );
  294. goto CLEANUP;
  295. }
  296. GetStartupInfoW( &info );
  297. if (!(info.dwFlags & STARTF_USESHOWWINDOW)) info.wShowWindow = SW_SHOWDEFAULT;
  298. if (unicode)
  299. {
  300. WINE_TRACE( "Calling %s (%p,%p,%s,%d)\n", wine_dbgstr_w(szEntryPoint),
  301. hWnd, instance, wine_dbgstr_w(szCmdLine), info.wShowWindow );
  302. call_entry_point( entry_point, hWnd, instance, szCmdLine, info.wShowWindow );
  303. }
  304. else
  305. {
  306. DWORD len = WideCharToMultiByte( CP_ACP, 0, szCmdLine, -1, NULL, 0, NULL, NULL );
  307. char *cmdline = HeapAlloc( GetProcessHeap(), 0, len );
  308. if (!cmdline)
  309. goto CLEANUP;
  310. WideCharToMultiByte( CP_ACP, 0, szCmdLine, -1, cmdline, len, NULL, NULL );
  311. WINE_TRACE( "Calling %s (%p,%p,%s,%d)\n", wine_dbgstr_w(szEntryPoint),
  312. hWnd, instance, wine_dbgstr_a(cmdline), info.wShowWindow );
  313. if (win16)
  314. {
  315. HMODULE shell = LoadLibraryW( L"shell32.dll" );
  316. if (shell) pRunDLL_CallEntry16 = (void *)GetProcAddress( shell, (LPCSTR)122 );
  317. if (pRunDLL_CallEntry16)
  318. pRunDLL_CallEntry16( entry_point, hWnd, instance, cmdline, info.wShowWindow );
  319. }
  320. else call_entry_point( entry_point, hWnd, instance, cmdline, info.wShowWindow );
  321. HeapFree( GetProcessHeap(), 0, cmdline );
  322. }
  323. CLEANUP:
  324. if (hWnd)
  325. DestroyWindow(hWnd);
  326. if (hDll)
  327. FreeLibrary(hDll);
  328. HeapFree(GetProcessHeap(),0,szDllName);
  329. return 0; /* rundll32 always returns 0! */
  330. }