INFO: Windows Rundll and Rundll32 Interface
This article was previously published under Q164787
Microsoft Windows 95, Windows 98, and Windows Millennium Edition (Me) contains two command-line utility programs named Rundll.exe andRundll32.exe that allow you to invoke a function exported from a DLL,either 16-bit or 32-bit. However, Rundll and Rundll32 programs do notallow you to call any exported function from any DLL. For example, you cannot use these utility programs to call the Win32 API (ApplicationProgramming Interface) calls exported from the system DLLs. The programsonly allow you to call functions from a DLL that are explicitly written tobe called by them. This article provides more details on the use of Rundlland Rundll32 programs under the Windows operating systems listed above.
MIcrosoft Windows NT 4.0, Windows 2000, and Windows XP ship with only Rundll32. There is no support for Rundll (the Win16 utility) on either platform.
The Rundll and Rundll32 utility programs were originally designed only forinternal use at Microsoft. But the functionality provided by them issufficiently generic that they are now available for general use.Note that Windows NT 4.0 ships only with the Rundll32 utility program andsupports only Rundll32.
Rundll vs. Rundll32Rundll loads and runs 16-bit DLLs, whereas Rundll32 loads and runs 32-bitDLLs. If you pass the wrong type of DLL to Rundll or Rundll32, it mayfail to run without indicating any error messages.
Rundll command lineThe command line for Rundll is as follows:
An example is as follows:
RUNDLL.EXE <dllname>,<entrypoint> <optional arguments>
There are 3 issues to consider carefully in the above command line:
RUNDLL.EXE SETUPX.DLL,InstallHinfSection 132 C:\WINDOWS\INF\SHELL.INF
- Rundll or Rundll32 search for the given DLL filename in the standard places (see the documentation for the LoadLibrary() function for details). It is recommended that you provide a full path to the DLL to ensure that the correct one is found. For best results, use the short file name instead of the long file name to ensure that no illegal characters will appear. Note in particular that this means a DLL in the "C:\Program Files" folder should be converted to its short name.
- The <dllname> may not contain any spaces or commas or quotation marks. This is a limitation in the Rundll command line parser.
- In the above command line, the comma (,) between the <dllname> and the <entrypont> function name is extremely important. If the comma separator is missing, Rundll or Rundll32 will fail without indicating any errors. In addition, there cannot be any white spaces in between the <dllname>, the comma, and the <entrypoint> function.
How Rundll WorksRundll performs the following steps:
- It parses the command line.
- It loads the specified DLL via LoadLibrary().
- It obtains the address of the <entrypoint> function via GetProcAddress().
- It calls the <entrypoint> function, passing the command line tail which is the <optional arguments>.
- When the <entrypoint> function returns, Rundll.exe unloads the DLL and exits.
How to Write Your DLLIn your DLL, write the <entrypoint> function with the following prototype:
void FAR PASCAL __loadds EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);
Again, there are 3 issues to consider with the EntryPoint function:
void CALLBACK EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);
- Obviously, the name "EntryPoint" should be replaced with the actual name of your entry point function. Note that the Rundll32's entry point is completely unrelated to the DllEntryPoint function in a 32-bit DLL which handles process and thread attach/detach notifications.
- The entry point function for Rundll32 must be defined with the _stdcall calling convention (CALLBACK defaults to using the _stdcall attribute). If the _stdcall attribute is missing, then the function defaults to _cdecl calling convention and then Rundll32 will terminate abnormally after calling the function.
- Since you must declare the function with _stdcall calling convention as described above, it follows that the Visual C++ compiler will actually export it as _EntryPoint@16 if the DLL is written in C or will use further name decoration if the DLL is written in C++. So, be careful to use the correctly exported name in the command line for Rundll or Rundll32. If you want to avoid using decorated names, use a .def file and export the entry point function by name. Please refer to the product documentation and the following article for further information on name decoration when using Visual C++ compilers:140485Exporting PASCAL-Like Symbols in 32-bit DLLs
hwnd - window handle that should be used as the owner window for any windows your DLL creates hinst - your DLL's instance handle lpszCmdLine - ASCIIZ command line your DLL should parse nCmdShow - describes how your DLL's windows should be displayedIn the following example:
Rundll would call the InstallHinfSection() entrypoint function inSetupx.dll and pass it the following parameters:
RUNDLL.EXE SETUPX.DLL,InstallHinfSection 132 C:\WINDOWS\INF\SHELL.INF
hwnd = (parent window handle) hinst = HINSTANCE of SETUPX.DLL lpszCmdLine = "132 C:\WINDOWS\INF\SHELL.INF" nCmdShow = (whatever the nCmdShow was passed to CreateProcess)Note that it is the <entrypoint> function (or InstallHinfSection() in theabove example) that has to parse its own command line (the lpszCmdLineparameter above) and use the individual parameters as necessary.Rundll.exe parses only up to the optional arguments passed to its commandline. The rest of the parsing is up to the <entrypoint> function.
Special Notes On Differences Between Windows 95 And Windows NTOn Windows NT, Windows 2000, and Windows XP the behavior of Rundll32.exe is slightly different, in order to accommodate UNICODE command lines.
Windows NT first attempts to GetProcAddress for <EntryPoint>W. If this entry point is found, then the prototype is assumed to be:
This is the same as the ANSI EntryPoint, except that the lpszCmdLineparameter is now a UNICODE string.
void CALLBACK EntryPointW(HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow);
If the <EntryPoint>W entry point is not found, then Windows NT willGetProcAddress for <entrypoint>A and for <entrypoint>. If either is found,then it is considered an ANSI entry point and is treated the same way asWindows 95/98/Me. Therefore, if you want your DLL to run on Windows 95 with ANSI support and on Windows NT/2000/XP with UNICODE support, you should export two functions: EntryPointW and EntryPoint. On Windows NT/2000/Me, the EntryPointW function will be called with a UNICODE command line; on Windows 95/98/Me, the EntryPoint function will be called with an ANSI Command line.
رقم الموضوع: 164787 - آخر مراجعة: 11/21/2006 15:45:33 - المراجعة: 4.4
Microsoft Win32 Application Programming Interface
- kbdll kbfaq kbinfo kbkernbase kbprogramming kbusage KB164787