rundll32.c 10.4 KB
Newer Older
1 2 3 4
/*
 * PURPOSE: Load a DLL and run an entry point with the specified parameters
 *
 * Copyright 2002 Alberto Massari
5 6
 * Copyright 2001-2003 Aric Stewart for CodeWeavers
 * Copyright 2003 Mike McCormack for CodeWeavers
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 27 28 29 30 31 32 33
 *
 */

/*
 *
 *  rundll32 dllname,entrypoint [arguments]
 *
 *  Documentation for this utility found on KB Q164787
 *
 */

#include <stdio.h>
#include <string.h>
34 35 36 37
#include <stdlib.h>

/* Exclude rarely-used stuff from Windows headers */
#define WIN32_LEAN_AND_MEAN
38 39 40 41
#include "windows.h"
#include "wine/winbase16.h"
#include "wine/unicode.h"
#include "wine/debug.h"
42

43
WINE_DEFAULT_DEBUG_CHANNEL(rundll32);
44

45 46 47

/*
 * Control_RunDLL has these parameters
48
 */
49 50 51 52 53 54 55
typedef void (WINAPI *EntryPointW)(HWND hWnd, HINSTANCE hInst, LPWSTR lpszCmdLine, int nCmdShow);
typedef void (WINAPI *EntryPointA)(HWND hWnd, HINSTANCE hInst, LPSTR lpszCmdLine, int nCmdShow);

/*
 * Control_RunDLL needs to have a window. So lets make us a very
 * simple window class.
 */
56 57 58 59
static const WCHAR szTitle[] = {'r','u','n','d','l','l','3','2',0};
static const WCHAR szWindowClass[] = {'c','l','a','s','s','_','r','u','n','d','l','l','3','2',0};
static const WCHAR kernel32[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
static const WCHAR shell32[] = {'s','h','e','l','l','3','2','.','d','l','l',0};
60

61 62 63 64 65
static HINSTANCE16 (WINAPI *pLoadLibrary16)(LPCSTR libname);
static FARPROC16 (WINAPI *pGetProcAddress16)(HMODULE16 hModule, LPCSTR name);
static void (WINAPI *pRunDLL_CallEntry16)( FARPROC proc, HWND hwnd, HINSTANCE inst,
                                           LPCSTR cmdline, INT cmdshow );

66
static ATOM register_class(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
67
{
68
    WNDCLASSEXW wcex;
69

70
    wcex.cbSize = sizeof(WNDCLASSEXW);
71 72

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
73
    wcex.lpfnWndProc    = DefWindowProcW;
74 75
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
76
    wcex.hInstance      = NULL;
77
    wcex.hIcon          = NULL;
78
    wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
79 80 81 82 83
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = NULL;

84
    return RegisterClassExW(&wcex);
Alexandre Julliard's avatar
Alexandre Julliard committed
85
}
86

87 88
static HINSTANCE16 load_dll16( LPCWSTR dll )
{
89
    HINSTANCE16 ret = 0;
90 91
    DWORD len = WideCharToMultiByte( CP_ACP, 0, dll, -1, NULL, 0, NULL, NULL );
    char *dllA = HeapAlloc( GetProcessHeap(), 0, len );
92 93 94 95

    if (dllA)
    {
        WideCharToMultiByte( CP_ACP, 0, dll, -1, dllA, len, NULL, NULL );
96
        pLoadLibrary16 = (void *)GetProcAddress( GetModuleHandleW(kernel32), (LPCSTR)35 );
97 98 99
        if (pLoadLibrary16) ret = pLoadLibrary16( dllA );
        HeapFree( GetProcessHeap(), 0, dllA );
    }
100 101 102 103 104
    return ret;
}

static FARPROC16 get_entry_point16( HINSTANCE16 inst, LPCWSTR entry )
{
105
    FARPROC16 ret = 0;
106 107
    DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
    char *entryA = HeapAlloc( GetProcessHeap(), 0, len );
108 109 110 111

    if (entryA)
    {
        WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );
112
        pGetProcAddress16 = (void *)GetProcAddress( GetModuleHandleW(kernel32), (LPCSTR)37 );
113 114 115
        if (pGetProcAddress16) ret = pGetProcAddress16( inst, entryA );
        HeapFree( GetProcessHeap(), 0, entryA );
    }
116 117 118 119 120 121
    return ret;
}

static void *get_entry_point32( HMODULE module, LPCWSTR entry, BOOL *unicode )
{
    void *ret;
122

123 124 125
    /* determine if the entry point is an ordinal */
    if (entry[0] == '#')
    {
126
        INT_PTR ordinal = atoiW( entry + 1 );
127 128
        if (ordinal <= 0)
            return NULL;
129

130 131 132 133
        *unicode = TRUE;
        ret = GetProcAddress( module, (LPCSTR)ordinal );
    }
    else
134
    {
135 136 137 138 139 140 141 142 143 144 145
        DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
        char *entryA = HeapAlloc( GetProcessHeap(), 0, len + 1 );

        if (!entryA)
            return NULL;

        WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );

        /* first try the W version */
        *unicode = TRUE;
        strcat( entryA, "W" );
146 147
        if (!(ret = GetProcAddress( module, entryA )))
        {
148 149 150 151 152 153 154 155 156
            /* now the A version */
            *unicode = FALSE;
            entryA[strlen(entryA)-1] = 'A';
            if (!(ret = GetProcAddress( module, entryA )))
            {
                /* now the version without suffix */
                entryA[strlen(entryA)-1] = 0;
                ret = GetProcAddress( module, entryA );
            }
157
        }
158
        HeapFree( GetProcessHeap(), 0, entryA );
159 160 161 162
    }
    return ret;
}

163
static LPWSTR get_next_arg(LPWSTR *cmdline)
Alexandre Julliard's avatar
Alexandre Julliard committed
164
{
165
    LPWSTR s;
166
    LPWSTR arg,d;
167 168
    BOOL in_quotes;
    int bcount,len=0;
169 170 171

    /* count the chars */
    bcount=0;
172
    in_quotes=FALSE;
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    s=*cmdline;
    while (1) {
        if (*s==0 || ((*s=='\t' || *s==' ') && !in_quotes)) {
            /* end of this command line argument */
            break;
        } else if (*s=='\\') {
            /* '\', count them */
            bcount++;
        } else if ((*s=='"') && ((bcount & 1)==0)) {
            /* unescaped '"' */
            in_quotes=!in_quotes;
            bcount=0;
        } else {
            /* a regular character */
            bcount=0;
        }
        s++;
        len++;
    }
    arg=HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
    if (!arg)
194
        return NULL;
195 196

    bcount=0;
197
    in_quotes=FALSE;
198 199 200 201 202 203 204 205 206 207 208 209 210
    d=arg;
    s=*cmdline;
    while (*s) {
        if ((*s=='\t' || *s==' ') && !in_quotes) {
            /* end of this command line argument */
            break;
        } else if (*s=='\\') {
            /* '\\' */
            *d++=*s++;
            bcount++;
        } else if (*s=='"') {
            /* '"' */
            if ((bcount & 1)==0) {
211
                /* Preceded by an even number of '\', this is half that
212 213 214 215 216 217
                 * number of '\', plus a quote which we erase.
                 */
                d-=bcount/2;
                in_quotes=!in_quotes;
                s++;
            } else {
218
                /* Preceded by an odd number of '\', this is half that
219 220 221 222 223 224 225 226 227 228 229
                 * number of '\' followed by a '"'
                 */
                d=d-bcount/2-1;
                *d++='"';
                s++;
            }
            bcount=0;
        } else {
            /* a regular character */
            *d++=*s++;
            bcount=0;
Alexandre Julliard's avatar
Alexandre Julliard committed
230
        }
231
    }
232 233 234 235 236 237 238 239 240
    *d=0;
    *cmdline=s;

    /* skip the remaining spaces */
    while (**cmdline=='\t' || **cmdline==' ') {
        (*cmdline)++;
    }

    return arg;
241 242
}

243
int WINAPI wWinMain(HINSTANCE instance, HINSTANCE hOldInstance, LPWSTR szCmdLine, int nCmdShow)
244
{
245 246
    HWND hWnd;
    LPWSTR szDllName,szEntryPoint;
247
    void *entry_point;
248
    BOOL unicode = FALSE, win16;
249
    STARTUPINFOW info;
250
    HMODULE hDll;
251 252 253 254 255 256

    hWnd=NULL;
    hDll=NULL;
    szDllName=NULL;

    /* Initialize the rundll32 class */
257
    register_class();
258
    hWnd = CreateWindowW(szWindowClass, szTitle,
259 260 261 262
          WS_OVERLAPPEDWINDOW|WS_VISIBLE,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, NULL, NULL);

    /* Get the dll name and API EntryPoint */
263
    WINE_TRACE("CmdLine=%s\n",wine_dbgstr_w(szCmdLine));
264
    szDllName = get_next_arg(&szCmdLine);
265 266 267
    if (!szDllName || *szDllName==0)
        goto CLEANUP;
    WINE_TRACE("DllName=%s\n",wine_dbgstr_w(szDllName));
268 269 270
    if ((szEntryPoint = strchrW(szDllName, ',' )))
        *szEntryPoint++=0;
    else
271
        szEntryPoint = get_next_arg(&szCmdLine);
272 273 274 275
    WINE_TRACE("EntryPoint=%s\n",wine_dbgstr_w(szEntryPoint));

    /* Load the library */
    hDll=LoadLibraryW(szDllName);
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    if (hDll)
    {
        win16 = FALSE;
        entry_point = get_entry_point32( hDll, szEntryPoint, &unicode );
    }
    else
    {
        HINSTANCE16 dll = load_dll16( szDllName );
        if (dll <= 32)
        {
            /* Windows has a MessageBox here... */
            WINE_ERR("Unable to load %s\n",wine_dbgstr_w(szDllName));
            goto CLEANUP;
        }
        win16 = TRUE;
        unicode = FALSE;
        entry_point = get_entry_point16( dll, szEntryPoint );
    }

    if (!entry_point)
296
    {
297
        /* Windows has a MessageBox here... */
298 299
        WINE_ERR( "Unable to find the entry point %s in %s\n",
                  wine_dbgstr_w(szEntryPoint), wine_dbgstr_w(szDllName) );
300
        goto CLEANUP;
301 302
    }

303 304 305 306
    GetStartupInfoW( &info );
    if (!(info.dwFlags & STARTF_USESHOWWINDOW)) info.wShowWindow = SW_SHOWDEFAULT;

    if (unicode)
307
    {
308
        EntryPointW pEntryPointW = entry_point;
309

310 311 312 313
        WINE_TRACE( "Calling %s (%p,%p,%s,%d)\n", wine_dbgstr_w(szEntryPoint),
                    hWnd, instance, wine_dbgstr_w(szCmdLine), info.wShowWindow );

        pEntryPointW( hWnd, instance, szCmdLine, info.wShowWindow );
314 315 316
    }
    else
    {
317 318
        DWORD len = WideCharToMultiByte( CP_ACP, 0, szCmdLine, -1, NULL, 0, NULL, NULL );
        char *cmdline = HeapAlloc( GetProcessHeap(), 0, len );
319 320 321 322

        if (!cmdline)
            goto CLEANUP;

323 324 325 326 327
        WideCharToMultiByte( CP_ACP, 0, szCmdLine, -1, cmdline, len, NULL, NULL );

        WINE_TRACE( "Calling %s (%p,%p,%s,%d)\n", wine_dbgstr_w(szEntryPoint),
                    hWnd, instance, wine_dbgstr_a(cmdline), info.wShowWindow );

328 329
        if (win16)
        {
330
            HMODULE shell = LoadLibraryW( shell32 );
331 332 333 334
            if (shell) pRunDLL_CallEntry16 = (void *)GetProcAddress( shell, (LPCSTR)122 );
            if (pRunDLL_CallEntry16)
                pRunDLL_CallEntry16( entry_point, hWnd, instance, cmdline, info.wShowWindow );
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
335 336
        else
        {
337 338
            EntryPointA pEntryPointA = entry_point;
            pEntryPointA( hWnd, instance, cmdline, info.wShowWindow );
339
        }
340
        HeapFree( GetProcessHeap(), 0, cmdline );
341 342 343 344 345 346 347
    }

CLEANUP:
    if (hWnd)
        DestroyWindow(hWnd);
    if (hDll)
        FreeLibrary(hDll);
348
    HeapFree(GetProcessHeap(),0,szDllName);
349
    return 0; /* rundll32 always returns 0! */
350
}