rundll32: Check more heap allocation failure paths for consistency.
[wine] / programs / rundll32 / rundll32.c
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  *
26  *  rundll32 dllname,entrypoint [arguments]
27  *
28  *  Documentation for this utility found on KB Q164787
29  *
30  */
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35
36 /* Exclude rarely-used stuff from Windows headers */
37 #define WIN32_LEAN_AND_MEAN
38 #include "windows.h"
39 #include "wine/winbase16.h"
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(rundll32);
44
45
46 /*
47  * Control_RunDLL has these parameters
48  */
49 typedef void (WINAPI *EntryPointW)(HWND hWnd, HINSTANCE hInst, LPWSTR lpszCmdLine, int nCmdShow);
50 typedef void (WINAPI *EntryPointA)(HWND hWnd, HINSTANCE hInst, LPSTR lpszCmdLine, int nCmdShow);
51
52 /*
53  * Control_RunDLL needs to have a window. So lets make us a very
54  * simple window class.
55  */
56 static const TCHAR  *szTitle = "rundll32";
57 static const TCHAR  *szWindowClass = "class_rundll32";
58
59 static HINSTANCE16 (WINAPI *pLoadLibrary16)(LPCSTR libname);
60 static FARPROC16 (WINAPI *pGetProcAddress16)(HMODULE16 hModule, LPCSTR name);
61 static void (WINAPI *pRunDLL_CallEntry16)( FARPROC proc, HWND hwnd, HINSTANCE inst,
62                                            LPCSTR cmdline, INT cmdshow );
63
64 static ATOM register_class(void)
65 {
66     WNDCLASSEX wcex;
67
68     wcex.cbSize = sizeof(WNDCLASSEX);
69
70     wcex.style          = CS_HREDRAW | CS_VREDRAW;
71     wcex.lpfnWndProc    = DefWindowProc;
72     wcex.cbClsExtra     = 0;
73     wcex.cbWndExtra     = 0;
74     wcex.hInstance      = NULL;
75     wcex.hIcon          = NULL;
76     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
77     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
78     wcex.lpszMenuName   = NULL;
79     wcex.lpszClassName  = szWindowClass;
80     wcex.hIconSm        = NULL;
81
82     return RegisterClassEx(&wcex);
83 }
84
85 static HINSTANCE16 load_dll16( LPCWSTR dll )
86 {
87     HINSTANCE16 ret = 0;
88     DWORD len = WideCharToMultiByte( CP_ACP, 0, dll, -1, NULL, 0, NULL, NULL );
89     char *dllA = HeapAlloc( GetProcessHeap(), 0, len );
90
91     if (dllA)
92     {
93         WideCharToMultiByte( CP_ACP, 0, dll, -1, dllA, len, NULL, NULL );
94         pLoadLibrary16 = (void *)GetProcAddress( GetModuleHandleA("kernel32.dll"), (LPCSTR)35 );
95         if (pLoadLibrary16) ret = pLoadLibrary16( dllA );
96         HeapFree( GetProcessHeap(), 0, dllA );
97     }
98     return ret;
99 }
100
101 static FARPROC16 get_entry_point16( HINSTANCE16 inst, LPCWSTR entry )
102 {
103     FARPROC16 ret = 0;
104     DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
105     char *entryA = HeapAlloc( GetProcessHeap(), 0, len );
106
107     if (entryA)
108     {
109         WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );
110         pGetProcAddress16 = (void *)GetProcAddress( GetModuleHandleA("kernel32.dll"), (LPCSTR)37 );
111         if (pGetProcAddress16) ret = pGetProcAddress16( inst, entryA );
112         HeapFree( GetProcessHeap(), 0, entryA );
113     }
114     return ret;
115 }
116
117 static void *get_entry_point32( HMODULE module, LPCWSTR entry, BOOL *unicode )
118 {
119     void *ret;
120     DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
121     char *entryA = HeapAlloc( GetProcessHeap(), 0, len + 1 );
122
123     if (!entryA)
124         return NULL;
125
126     WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );
127
128     /* first try the W version */
129     *unicode = TRUE;
130     strcat( entryA, "W" );
131     if (!(ret = GetProcAddress( module, entryA )))
132     {
133         /* now the A version */
134         *unicode = FALSE;
135         entryA[strlen(entryA)-1] = 'A';
136         if (!(ret = GetProcAddress( module, entryA )))
137         {
138             /* now the version without suffix */
139             entryA[strlen(entryA)-1] = 0;
140             ret = GetProcAddress( module, entryA );
141         }
142     }
143     HeapFree( GetProcessHeap(), 0, entryA );
144     return ret;
145 }
146
147 static LPWSTR get_next_arg(LPWSTR *cmdline)
148 {
149     LPWSTR s;
150     LPWSTR arg,d;
151     int in_quotes,bcount,len=0;
152
153     /* count the chars */
154     bcount=0;
155     in_quotes=0;
156     s=*cmdline;
157     while (1) {
158         if (*s==0 || ((*s=='\t' || *s==' ') && !in_quotes)) {
159             /* end of this command line argument */
160             break;
161         } else if (*s=='\\') {
162             /* '\', count them */
163             bcount++;
164         } else if ((*s=='"') && ((bcount & 1)==0)) {
165             /* unescaped '"' */
166             in_quotes=!in_quotes;
167             bcount=0;
168         } else {
169             /* a regular character */
170             bcount=0;
171         }
172         s++;
173         len++;
174     }
175     arg=HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
176     if (!arg)
177         return NULL;
178
179     bcount=0;
180     in_quotes=0;
181     d=arg;
182     s=*cmdline;
183     while (*s) {
184         if ((*s=='\t' || *s==' ') && !in_quotes) {
185             /* end of this command line argument */
186             break;
187         } else if (*s=='\\') {
188             /* '\\' */
189             *d++=*s++;
190             bcount++;
191         } else if (*s=='"') {
192             /* '"' */
193             if ((bcount & 1)==0) {
194                 /* Preceded by an even number of '\', this is half that
195                  * number of '\', plus a quote which we erase.
196                  */
197                 d-=bcount/2;
198                 in_quotes=!in_quotes;
199                 s++;
200             } else {
201                 /* Preceded by an odd number of '\', this is half that
202                  * number of '\' followed by a '"'
203                  */
204                 d=d-bcount/2-1;
205                 *d++='"';
206                 s++;
207             }
208             bcount=0;
209         } else {
210             /* a regular character */
211             *d++=*s++;
212             bcount=0;
213         }
214     }
215     *d=0;
216     *cmdline=s;
217
218     /* skip the remaining spaces */
219     while (**cmdline=='\t' || **cmdline==' ') {
220         (*cmdline)++;
221     }
222
223     return arg;
224 }
225
226 int WINAPI WinMain(HINSTANCE instance, HINSTANCE hOldInstance, LPSTR szCmdArgs, int nCmdShow)
227 {
228     HWND hWnd;
229     LPWSTR szCmdLine;
230     LPWSTR szDllName,szEntryPoint;
231     void *entry_point;
232     BOOL unicode = FALSE, win16;
233     STARTUPINFOW info;
234     HMODULE hDll;
235
236     hWnd=NULL;
237     hDll=NULL;
238     szDllName=NULL;
239
240     /* Initialize the rundll32 class */
241     register_class();
242     hWnd = CreateWindow(szWindowClass, szTitle,
243           WS_OVERLAPPEDWINDOW|WS_VISIBLE,
244           CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, NULL, NULL);
245
246     /* Skip the rundll32.exe path */
247     szCmdLine=GetCommandLineW();
248     WINE_TRACE("CmdLine=%s\n",wine_dbgstr_w(szCmdLine));
249     szDllName = get_next_arg(&szCmdLine);
250     if (!szDllName || *szDllName==0)
251         goto CLEANUP;
252     HeapFree(GetProcessHeap(),0,szDllName);
253
254     /* Get the dll name and API EntryPoint */
255     szDllName = get_next_arg(&szCmdLine);
256     if (!szDllName || *szDllName==0)
257         goto CLEANUP;
258     WINE_TRACE("DllName=%s\n",wine_dbgstr_w(szDllName));
259     if ((szEntryPoint = strchrW(szDllName, ',' )))
260         *szEntryPoint++=0;
261     else
262         szEntryPoint = get_next_arg(&szCmdLine);
263     WINE_TRACE("EntryPoint=%s\n",wine_dbgstr_w(szEntryPoint));
264
265     /* Load the library */
266     hDll=LoadLibraryW(szDllName);
267     if (hDll)
268     {
269         win16 = FALSE;
270         entry_point = get_entry_point32( hDll, szEntryPoint, &unicode );
271     }
272     else
273     {
274         HINSTANCE16 dll = load_dll16( szDllName );
275         if (dll <= 32)
276         {
277             /* Windows has a MessageBox here... */
278             WINE_ERR("Unable to load %s\n",wine_dbgstr_w(szDllName));
279             goto CLEANUP;
280         }
281         win16 = TRUE;
282         unicode = FALSE;
283         entry_point = get_entry_point16( dll, szEntryPoint );
284     }
285
286     if (!entry_point)
287     {
288         /* Windows has a MessageBox here... */
289         WINE_ERR( "Unable to find the entry point %s in %s\n",
290                   wine_dbgstr_w(szEntryPoint), wine_dbgstr_w(szDllName) );
291         goto CLEANUP;
292     }
293
294     GetStartupInfoW( &info );
295     if (!(info.dwFlags & STARTF_USESHOWWINDOW)) info.wShowWindow = SW_SHOWDEFAULT;
296
297     if (unicode)
298     {
299         EntryPointW pEntryPointW = entry_point;
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
304         pEntryPointW( hWnd, instance, szCmdLine, info.wShowWindow );
305     }
306     else
307     {
308         DWORD len = WideCharToMultiByte( CP_ACP, 0, szCmdLine, -1, NULL, 0, NULL, NULL );
309         char *cmdline = HeapAlloc( GetProcessHeap(), 0, len );
310
311         if (!cmdline)
312             goto CLEANUP;
313
314         WideCharToMultiByte( CP_ACP, 0, szCmdLine, -1, cmdline, len, NULL, NULL );
315
316         WINE_TRACE( "Calling %s (%p,%p,%s,%d)\n", wine_dbgstr_w(szEntryPoint),
317                     hWnd, instance, wine_dbgstr_a(cmdline), info.wShowWindow );
318
319         if (win16)
320         {
321             HMODULE shell = LoadLibraryA( "shell32.dll" );
322             if (shell) pRunDLL_CallEntry16 = (void *)GetProcAddress( shell, (LPCSTR)122 );
323             if (pRunDLL_CallEntry16)
324                 pRunDLL_CallEntry16( entry_point, hWnd, instance, cmdline, info.wShowWindow );
325         }
326         else
327         {
328             EntryPointA pEntryPointA = entry_point;
329             pEntryPointA( hWnd, instance, cmdline, info.wShowWindow );
330         }
331         HeapFree( GetProcessHeap(), 0, cmdline );
332     }
333
334 CLEANUP:
335     if (hWnd)
336         DestroyWindow(hWnd);
337     if (hDll)
338         FreeLibrary(hDll);
339     HeapFree(GetProcessHeap(),0,szDllName);
340     return 0; /* rundll32 always returns 0! */
341 }