Do not check for non NULL pointer before HeapFree'ing it. It's
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 TCHAR  *szTitle = "rundll32";
57 static 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 MyRegisterClass(HINSTANCE hInstance)
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      = hInstance;
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     WideCharToMultiByte( CP_ACP, 0, dll, -1, dllA, len, NULL, NULL );
91     pLoadLibrary16 = (void *)GetProcAddress( GetModuleHandleA("kernel32.dll"), "LoadLibrary16" );
92     if (pLoadLibrary16) ret = pLoadLibrary16( dllA );
93     HeapFree( GetProcessHeap(), 0, dllA );
94     return ret;
95 }
96
97 static FARPROC16 get_entry_point16( HINSTANCE16 inst, LPCWSTR entry )
98 {
99     FARPROC16 ret = 0;
100     DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
101     char *entryA = HeapAlloc( GetProcessHeap(), 0, len );
102     WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );
103     pGetProcAddress16 = (void *)GetProcAddress( GetModuleHandleA("kernel32.dll"), "GetProcAddress16" );
104     if (pGetProcAddress16) ret = pGetProcAddress16( inst, entryA );
105     HeapFree( GetProcessHeap(), 0, entryA );
106     return ret;
107 }
108
109 static void *get_entry_point32( HMODULE module, LPCWSTR entry, BOOL *unicode )
110 {
111     void *ret;
112     DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
113     char *entryA = HeapAlloc( GetProcessHeap(), 0, len + 1 );
114     WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );
115
116     /* first try the W version */
117     *unicode = TRUE;
118     strcat( entryA, "W" );
119     if (!(ret = GetProcAddress( module, entryA )))
120     {
121         /* now the A version */
122         *unicode = FALSE;
123         entryA[strlen(entryA)-1] = 'A';
124         if (!(ret = GetProcAddress( module, entryA )))
125         {
126             /* now the version without suffix */
127             entryA[strlen(entryA)-1] = 0;
128             ret = GetProcAddress( module, entryA );
129         }
130     }
131     HeapFree( GetProcessHeap(), 0, entryA );
132     return ret;
133 }
134
135 static LPWSTR GetNextArg(LPWSTR *cmdline)
136 {
137     LPWSTR s;
138     LPWSTR arg,d;
139     int in_quotes,bcount,len=0;
140
141     /* count the chars */
142     bcount=0;
143     in_quotes=0;
144     s=*cmdline;
145     while (1) {
146         if (*s==0 || ((*s=='\t' || *s==' ') && !in_quotes)) {
147             /* end of this command line argument */
148             break;
149         } else if (*s=='\\') {
150             /* '\', count them */
151             bcount++;
152         } else if ((*s=='"') && ((bcount & 1)==0)) {
153             /* unescaped '"' */
154             in_quotes=!in_quotes;
155             bcount=0;
156         } else {
157             /* a regular character */
158             bcount=0;
159         }
160         s++;
161         len++;
162     }
163     arg=HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
164     if (!arg)
165         return NULL;
166
167     bcount=0;
168     in_quotes=0;
169     d=arg;
170     s=*cmdline;
171     while (*s) {
172         if ((*s=='\t' || *s==' ') && !in_quotes) {
173             /* end of this command line argument */
174             break;
175         } else if (*s=='\\') {
176             /* '\\' */
177             *d++=*s++;
178             bcount++;
179         } else if (*s=='"') {
180             /* '"' */
181             if ((bcount & 1)==0) {
182                 /* Preceeded by an even number of '\', this is half that
183                  * number of '\', plus a quote which we erase.
184                  */
185                 d-=bcount/2;
186                 in_quotes=!in_quotes;
187                 s++;
188             } else {
189                 /* Preceeded by an odd number of '\', this is half that
190                  * number of '\' followed by a '"'
191                  */
192                 d=d-bcount/2-1;
193                 *d++='"';
194                 s++;
195             }
196             bcount=0;
197         } else {
198             /* a regular character */
199             *d++=*s++;
200             bcount=0;
201         }
202     }
203     *d=0;
204     *cmdline=s;
205
206     /* skip the remaining spaces */
207     while (**cmdline=='\t' || **cmdline==' ') {
208         (*cmdline)++;
209     }
210
211     return arg;
212 }
213
214 int main(int argc, char* argv[])
215 {
216     HWND hWnd;
217     LPWSTR szCmdLine;
218     LPWSTR szDllName,szEntryPoint;
219     void *entry_point;
220     BOOL unicode, win16;
221     STARTUPINFOW info;
222     HMODULE hDll, instance;
223
224     hWnd=NULL;
225     hDll=NULL;
226     szDllName=NULL;
227
228     /* Initialize the rundll32 class */
229     MyRegisterClass( NULL );
230     hWnd = CreateWindow(szWindowClass, szTitle,
231           WS_OVERLAPPEDWINDOW|WS_VISIBLE,
232           CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, NULL, NULL);
233
234     /* Skip the rundll32.exe path */
235     szCmdLine=GetCommandLineW();
236     WINE_TRACE("CmdLine=%s\n",wine_dbgstr_w(szCmdLine));
237     szDllName=GetNextArg(&szCmdLine);
238     if (!szDllName || *szDllName==0)
239         goto CLEANUP;
240     HeapFree(GetProcessHeap(),0,szDllName);
241
242     /* Get the dll name and API EntryPoint */
243     szDllName=GetNextArg(&szCmdLine);
244     if (!szDllName || *szDllName==0)
245         goto CLEANUP;
246     WINE_TRACE("DllName=%s\n",wine_dbgstr_w(szDllName));
247     if (!(szEntryPoint = strchrW( szDllName, ',' ))) goto CLEANUP;
248     *szEntryPoint++=0;
249     WINE_TRACE("EntryPoint=%s\n",wine_dbgstr_w(szEntryPoint));
250
251     /* Load the library */
252     hDll=LoadLibraryW(szDllName);
253     if (hDll)
254     {
255         win16 = FALSE;
256         entry_point = get_entry_point32( hDll, szEntryPoint, &unicode );
257     }
258     else
259     {
260         HINSTANCE16 dll = load_dll16( szDllName );
261         if (dll <= 32)
262         {
263             /* Windows has a MessageBox here... */
264             WINE_ERR("Unable to load %s\n",wine_dbgstr_w(szDllName));
265             goto CLEANUP;
266         }
267         win16 = TRUE;
268         unicode = FALSE;
269         entry_point = get_entry_point16( dll, szEntryPoint );
270     }
271
272     if (!entry_point)
273     {
274         /* Windows has a MessageBox here... */
275         WINE_ERR( "Unable to find the entry point %s in %s\n",
276                   wine_dbgstr_w(szEntryPoint), wine_dbgstr_w(szDllName) );
277         goto CLEANUP;
278     }
279
280     GetStartupInfoW( &info );
281     if (!(info.dwFlags & STARTF_USESHOWWINDOW)) info.wShowWindow = SW_SHOWDEFAULT;
282     instance = GetModuleHandleW(NULL);  /* Windows always uses that, not hDll */
283
284     if (unicode)
285     {
286         EntryPointW pEntryPointW = entry_point;
287
288         WINE_TRACE( "Calling %s (%p,%p,%s,%d)\n", wine_dbgstr_w(szEntryPoint),
289                     hWnd, instance, wine_dbgstr_w(szCmdLine), info.wShowWindow );
290
291         pEntryPointW( hWnd, instance, szCmdLine, info.wShowWindow );
292     }
293     else
294     {
295         DWORD len = WideCharToMultiByte( CP_ACP, 0, szCmdLine, -1, NULL, 0, NULL, NULL );
296         char *cmdline = HeapAlloc( GetProcessHeap(), 0, len );
297         WideCharToMultiByte( CP_ACP, 0, szCmdLine, -1, cmdline, len, NULL, NULL );
298
299         WINE_TRACE( "Calling %s (%p,%p,%s,%d)\n", wine_dbgstr_w(szEntryPoint),
300                     hWnd, instance, wine_dbgstr_a(cmdline), info.wShowWindow );
301
302         if (win16)
303         {
304             HMODULE shell = LoadLibraryA( "shell32.dll" );
305             if (shell) pRunDLL_CallEntry16 = (void *)GetProcAddress( shell, (LPCSTR)122 );
306             if (pRunDLL_CallEntry16)
307                 pRunDLL_CallEntry16( entry_point, hWnd, instance, cmdline, info.wShowWindow );
308         }
309         else
310         {
311             EntryPointA pEntryPointA = entry_point;
312             pEntryPointA( hWnd, instance, cmdline, info.wShowWindow );
313         }
314         HeapFree( GetProcessHeap(), 0, cmdline );
315     }
316
317 CLEANUP:
318     if (hWnd)
319         DestroyWindow(hWnd);
320     if (hDll)
321         FreeLibrary(hDll);
322     HeapFree(GetProcessHeap(),0,szDllName);
323     return 0; /* rundll32 always returns 0! */
324 }