rundll32.c 9.87 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
static const TCHAR  *szTitle = "rundll32";
static const TCHAR  *szWindowClass = "class_rundll32";
58

59 60 61 62 63
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 );

64
static ATOM MyRegisterClass(HINSTANCE hInstance)
Alexandre Julliard's avatar
Alexandre Julliard committed
65
{
66 67 68 69 70
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

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

    return RegisterClassEx(&wcex);
Alexandre Julliard's avatar
Alexandre Julliard committed
83
}
84

85 86
static HINSTANCE16 load_dll16( LPCWSTR dll )
{
87
    HINSTANCE16 ret = 0;
88 89 90
    DWORD len = WideCharToMultiByte( CP_ACP, 0, dll, -1, NULL, 0, NULL, NULL );
    char *dllA = HeapAlloc( GetProcessHeap(), 0, len );
    WideCharToMultiByte( CP_ACP, 0, dll, -1, dllA, len, NULL, NULL );
91 92
    pLoadLibrary16 = (void *)GetProcAddress( GetModuleHandleA("kernel32.dll"), "LoadLibrary16" );
    if (pLoadLibrary16) ret = pLoadLibrary16( dllA );
93 94 95 96 97 98
    HeapFree( GetProcessHeap(), 0, dllA );
    return ret;
}

static FARPROC16 get_entry_point16( HINSTANCE16 inst, LPCWSTR entry )
{
99
    FARPROC16 ret = 0;
100 101 102
    DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
    char *entryA = HeapAlloc( GetProcessHeap(), 0, len );
    WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );
103 104
    pGetProcAddress16 = (void *)GetProcAddress( GetModuleHandleA("kernel32.dll"), "GetProcAddress16" );
    if (pGetProcAddress16) ret = pGetProcAddress16( inst, entryA );
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    HeapFree( GetProcessHeap(), 0, entryA );
    return ret;
}

static void *get_entry_point32( HMODULE module, LPCWSTR entry, BOOL *unicode )
{
    void *ret;
    DWORD len = WideCharToMultiByte( CP_ACP, 0, entry, -1, NULL, 0, NULL, NULL );
    char *entryA = HeapAlloc( GetProcessHeap(), 0, len + 1 );
    WideCharToMultiByte( CP_ACP, 0, entry, -1, entryA, len, NULL, NULL );

    /* first try the W version */
    *unicode = TRUE;
    strcat( entryA, "W" );
    if (!(ret = GetProcAddress( module, entryA )))
    {
        /* 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 );
        }
    }
    HeapFree( GetProcessHeap(), 0, entryA );
    return ret;
}

static LPWSTR GetNextArg(LPWSTR *cmdline)
Alexandre Julliard's avatar
Alexandre Julliard committed
136
{
137
    LPWSTR s;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    LPWSTR arg,d;
    int in_quotes,bcount,len=0;

    /* count the chars */
    bcount=0;
    in_quotes=0;
    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)
165
        return NULL;
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

    bcount=0;
    in_quotes=0;
    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) {
182
                /* Preceded by an even number of '\', this is half that
183 184 185 186 187 188
                 * number of '\', plus a quote which we erase.
                 */
                d-=bcount/2;
                in_quotes=!in_quotes;
                s++;
            } else {
189
                /* Preceded by an odd number of '\', this is half that
190 191 192 193 194 195 196 197 198 199 200
                 * 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
201
        }
202
    }
203 204 205 206 207 208 209 210 211
    *d=0;
    *cmdline=s;

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

    return arg;
212 213 214 215
}

int main(int argc, char* argv[])
{
216
    HWND hWnd;
217
    LPWSTR szCmdLine;
218
    LPWSTR szDllName,szEntryPoint;
219 220 221 222
    void *entry_point;
    BOOL unicode, win16;
    STARTUPINFOW info;
    HMODULE hDll, instance;
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

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

    /* Initialize the rundll32 class */
    MyRegisterClass( NULL );
    hWnd = CreateWindow(szWindowClass, szTitle,
          WS_OVERLAPPEDWINDOW|WS_VISIBLE,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, NULL, NULL);

    /* Skip the rundll32.exe path */
    szCmdLine=GetCommandLineW();
    WINE_TRACE("CmdLine=%s\n",wine_dbgstr_w(szCmdLine));
    szDllName=GetNextArg(&szCmdLine);
    if (!szDllName || *szDllName==0)
        goto CLEANUP;
    HeapFree(GetProcessHeap(),0,szDllName);

    /* Get the dll name and API EntryPoint */
    szDllName=GetNextArg(&szCmdLine);
    if (!szDllName || *szDllName==0)
        goto CLEANUP;
    WINE_TRACE("DllName=%s\n",wine_dbgstr_w(szDllName));
247 248 249 250
    if ((szEntryPoint = strchrW(szDllName, ',' )))
        *szEntryPoint++=0;
    else
        szEntryPoint = GetNextArg(&szCmdLine);
251 252 253 254
    WINE_TRACE("EntryPoint=%s\n",wine_dbgstr_w(szEntryPoint));

    /* Load the library */
    hDll=LoadLibraryW(szDllName);
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    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)
275
    {
276
        /* Windows has a MessageBox here... */
277 278
        WINE_ERR( "Unable to find the entry point %s in %s\n",
                  wine_dbgstr_w(szEntryPoint), wine_dbgstr_w(szDllName) );
279
        goto CLEANUP;
280 281
    }

282 283 284 285 286
    GetStartupInfoW( &info );
    if (!(info.dwFlags & STARTF_USESHOWWINDOW)) info.wShowWindow = SW_SHOWDEFAULT;
    instance = GetModuleHandleW(NULL);  /* Windows always uses that, not hDll */

    if (unicode)
287
    {
288
        EntryPointW pEntryPointW = entry_point;
289

290 291 292 293
        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 );
294 295 296
    }
    else
    {
297 298 299 300 301 302 303
        DWORD len = WideCharToMultiByte( CP_ACP, 0, szCmdLine, -1, NULL, 0, NULL, NULL );
        char *cmdline = HeapAlloc( GetProcessHeap(), 0, len );
        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 );

304 305 306 307 308 309 310
        if (win16)
        {
            HMODULE shell = LoadLibraryA( "shell32.dll" );
            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
311 312
        else
        {
313 314
            EntryPointA pEntryPointA = entry_point;
            pEntryPointA( hWnd, instance, cmdline, info.wShowWindow );
315
        }
316
        HeapFree( GetProcessHeap(), 0, cmdline );
317 318 319 320 321 322 323
    }

CLEANUP:
    if (hWnd)
        DestroyWindow(hWnd);
    if (hDll)
        FreeLibrary(hDll);
324
    HeapFree(GetProcessHeap(),0,szDllName);
325
    return 0; /* rundll32 always returns 0! */
326
}