wined3d: Use the np2_fixup to find out if a RECT texture is used.
[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 WCHAR szTitle[] = {'r','u','n','d','l','l','3','2',0};
57 static const WCHAR szWindowClass[] = {'c','l','a','s','s','_','r','u','n','d','l','l','3','2',0};
58 static const WCHAR kernel32[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
59 static const WCHAR shell32[] = {'s','h','e','l','l','3','2','.','d','l','l',0};
60
61 static HINSTANCE16 (WINAPI *pLoadLibrary16)(LPCSTR libname);
62 static FARPROC16 (WINAPI *pGetProcAddress16)(HMODULE16 hModule, LPCSTR name);
63 static void (WINAPI *pRunDLL_CallEntry16)( FARPROC proc, HWND hwnd, HINSTANCE inst,
64                                            LPCSTR cmdline, INT cmdshow );
65
66 static ATOM register_class(void)
67 {
68     WNDCLASSEXW wcex;
69
70     wcex.cbSize = sizeof(WNDCLASSEXW);
71
72     wcex.style          = CS_HREDRAW | CS_VREDRAW;
73     wcex.lpfnWndProc    = DefWindowProcW;
74     wcex.cbClsExtra     = 0;
75     wcex.cbWndExtra     = 0;
76     wcex.hInstance      = NULL;
77     wcex.hIcon          = NULL;
78     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
79     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
80     wcex.lpszMenuName   = NULL;
81     wcex.lpszClassName  = szWindowClass;
82     wcex.hIconSm        = NULL;
83
84     return RegisterClassExW(&wcex);
85 }
86
87 static HINSTANCE16 load_dll16( LPCWSTR dll )
88 {
89     HINSTANCE16 ret = 0;
90     DWORD len = WideCharToMultiByte( CP_ACP, 0, dll, -1, NULL, 0, NULL, NULL );
91     char *dllA = HeapAlloc( GetProcessHeap(), 0, len );
92
93     if (dllA)
94     {
95         WideCharToMultiByte( CP_ACP, 0, dll, -1, dllA, len, NULL, NULL );
96         pLoadLibrary16 = (void *)GetProcAddress( GetModuleHandleW(kernel32), (LPCSTR)35 );
97         if (pLoadLibrary16) ret = pLoadLibrary16( dllA );
98         HeapFree( GetProcessHeap(), 0, dllA );
99     }
100     return ret;
101 }
102
103 static FARPROC16 get_entry_point16( HINSTANCE16 inst, LPCWSTR entry )
104 {
105     FARPROC16 ret = 0;
106     DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
107     char *entryA = HeapAlloc( GetProcessHeap(), 0, len );
108
109     if (entryA)
110     {
111         WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );
112         pGetProcAddress16 = (void *)GetProcAddress( GetModuleHandleW(kernel32), (LPCSTR)37 );
113         if (pGetProcAddress16) ret = pGetProcAddress16( inst, entryA );
114         HeapFree( GetProcessHeap(), 0, entryA );
115     }
116     return ret;
117 }
118
119 static void *get_entry_point32( HMODULE module, LPCWSTR entry, BOOL *unicode )
120 {
121     void *ret;
122
123     /* determine if the entry point is an ordinal */
124     if (entry[0] == '#')
125     {
126         INT_PTR ordinal = atoiW( entry + 1 );
127         if (ordinal <= 0)
128             return NULL;
129
130         *unicode = TRUE;
131         ret = GetProcAddress( module, (LPCSTR)ordinal );
132     }
133     else
134     {
135         DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
136         char *entryA = HeapAlloc( GetProcessHeap(), 0, len + 1 );
137
138         if (!entryA)
139             return NULL;
140
141         WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );
142
143         /* first try the W version */
144         *unicode = TRUE;
145         strcat( entryA, "W" );
146         if (!(ret = GetProcAddress( module, entryA )))
147         {
148             /* now the A version */
149             *unicode = FALSE;
150             entryA[strlen(entryA)-1] = 'A';
151             if (!(ret = GetProcAddress( module, entryA )))
152             {
153                 /* now the version without suffix */
154                 entryA[strlen(entryA)-1] = 0;
155                 ret = GetProcAddress( module, entryA );
156             }
157         }
158         HeapFree( GetProcessHeap(), 0, entryA );
159     }
160     return ret;
161 }
162
163 static LPWSTR get_next_arg(LPWSTR *cmdline)
164 {
165     LPWSTR s;
166     LPWSTR arg,d;
167     int in_quotes,bcount,len=0;
168
169     /* count the chars */
170     bcount=0;
171     in_quotes=0;
172     s=*cmdline;
173     while (1) {
174         if (*s==0 || ((*s=='\t' || *s==' ') && !in_quotes)) {
175             /* end of this command line argument */
176             break;
177         } else if (*s=='\\') {
178             /* '\', count them */
179             bcount++;
180         } else if ((*s=='"') && ((bcount & 1)==0)) {
181             /* unescaped '"' */
182             in_quotes=!in_quotes;
183             bcount=0;
184         } else {
185             /* a regular character */
186             bcount=0;
187         }
188         s++;
189         len++;
190     }
191     arg=HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
192     if (!arg)
193         return NULL;
194
195     bcount=0;
196     in_quotes=0;
197     d=arg;
198     s=*cmdline;
199     while (*s) {
200         if ((*s=='\t' || *s==' ') && !in_quotes) {
201             /* end of this command line argument */
202             break;
203         } else if (*s=='\\') {
204             /* '\\' */
205             *d++=*s++;
206             bcount++;
207         } else if (*s=='"') {
208             /* '"' */
209             if ((bcount & 1)==0) {
210                 /* Preceded by an even number of '\', this is half that
211                  * number of '\', plus a quote which we erase.
212                  */
213                 d-=bcount/2;
214                 in_quotes=!in_quotes;
215                 s++;
216             } else {
217                 /* Preceded by an odd number of '\', this is half that
218                  * number of '\' followed by a '"'
219                  */
220                 d=d-bcount/2-1;
221                 *d++='"';
222                 s++;
223             }
224             bcount=0;
225         } else {
226             /* a regular character */
227             *d++=*s++;
228             bcount=0;
229         }
230     }
231     *d=0;
232     *cmdline=s;
233
234     /* skip the remaining spaces */
235     while (**cmdline=='\t' || **cmdline==' ') {
236         (*cmdline)++;
237     }
238
239     return arg;
240 }
241
242 int WINAPI wWinMain(HINSTANCE instance, HINSTANCE hOldInstance, LPWSTR szCmdLine, int nCmdShow)
243 {
244     HWND hWnd;
245     LPWSTR szDllName,szEntryPoint;
246     void *entry_point;
247     BOOL unicode = FALSE, win16;
248     STARTUPINFOW info;
249     HMODULE hDll;
250
251     hWnd=NULL;
252     hDll=NULL;
253     szDllName=NULL;
254
255     /* Initialize the rundll32 class */
256     register_class();
257     hWnd = CreateWindowW(szWindowClass, szTitle,
258           WS_OVERLAPPEDWINDOW|WS_VISIBLE,
259           CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, NULL, NULL);
260
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 = strchrW(szDllName, ',' )))
268         *szEntryPoint++=0;
269     else
270         szEntryPoint = get_next_arg(&szCmdLine);
271     WINE_TRACE("EntryPoint=%s\n",wine_dbgstr_w(szEntryPoint));
272
273     /* Load the library */
274     hDll=LoadLibraryW(szDllName);
275     if (hDll)
276     {
277         win16 = FALSE;
278         entry_point = get_entry_point32( hDll, szEntryPoint, &unicode );
279     }
280     else
281     {
282         HINSTANCE16 dll = load_dll16( szDllName );
283         if (dll <= 32)
284         {
285             /* Windows has a MessageBox here... */
286             WINE_ERR("Unable to load %s\n",wine_dbgstr_w(szDllName));
287             goto CLEANUP;
288         }
289         win16 = TRUE;
290         unicode = FALSE;
291         entry_point = get_entry_point16( dll, szEntryPoint );
292     }
293
294     if (!entry_point)
295     {
296         /* Windows has a MessageBox here... */
297         WINE_ERR( "Unable to find the entry point %s in %s\n",
298                   wine_dbgstr_w(szEntryPoint), wine_dbgstr_w(szDllName) );
299         goto CLEANUP;
300     }
301
302     GetStartupInfoW( &info );
303     if (!(info.dwFlags & STARTF_USESHOWWINDOW)) info.wShowWindow = SW_SHOWDEFAULT;
304
305     if (unicode)
306     {
307         EntryPointW pEntryPointW = entry_point;
308
309         WINE_TRACE( "Calling %s (%p,%p,%s,%d)\n", wine_dbgstr_w(szEntryPoint),
310                     hWnd, instance, wine_dbgstr_w(szCmdLine), info.wShowWindow );
311
312         pEntryPointW( hWnd, instance, szCmdLine, info.wShowWindow );
313     }
314     else
315     {
316         DWORD len = WideCharToMultiByte( CP_ACP, 0, szCmdLine, -1, NULL, 0, NULL, NULL );
317         char *cmdline = HeapAlloc( GetProcessHeap(), 0, len );
318
319         if (!cmdline)
320             goto CLEANUP;
321
322         WideCharToMultiByte( CP_ACP, 0, szCmdLine, -1, cmdline, len, NULL, NULL );
323
324         WINE_TRACE( "Calling %s (%p,%p,%s,%d)\n", wine_dbgstr_w(szEntryPoint),
325                     hWnd, instance, wine_dbgstr_a(cmdline), info.wShowWindow );
326
327         if (win16)
328         {
329             HMODULE shell = LoadLibraryW( shell32 );
330             if (shell) pRunDLL_CallEntry16 = (void *)GetProcAddress( shell, (LPCSTR)122 );
331             if (pRunDLL_CallEntry16)
332                 pRunDLL_CallEntry16( entry_point, hWnd, instance, cmdline, info.wShowWindow );
333         }
334         else
335         {
336             EntryPointA pEntryPointA = entry_point;
337             pEntryPointA( hWnd, instance, cmdline, info.wShowWindow );
338         }
339         HeapFree( GetProcessHeap(), 0, cmdline );
340     }
341
342 CLEANUP:
343     if (hWnd)
344         DestroyWindow(hWnd);
345     if (hDll)
346         FreeLibrary(hDll);
347     HeapFree(GetProcessHeap(),0,szDllName);
348     return 0; /* rundll32 always returns 0! */
349 }