Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.8 KiB

  1. /*
  2. How Rundll Works
  3. Rundll performs the following steps:
  4. 1. It parses the command line.
  5. 2. It loads the specified DLL via LoadLibrary().
  6. It obtains the address of the <entrypoint> function via GetProcAddress().
  7. It calls the <entrypoint> function, passing the command line tail which is the <optional arguments>.
  8. When the <entrypoint> function returns, Rundll.exe unloads the DLL and exits.
  9. */
  10. // RUNDLL32.EXE SETUPX.DLL,InstallHinfSection 132 C:\WINDOWS\INF\SHELL.INF
  11. #include <windows.h>
  12. #include <stdio.h>
  13. int wmain(int argc, WCHAR *argv[])
  14. {
  15. if (argc <2) return 1;
  16. for (int i=0; i<argc; i++)
  17. {
  18. RETAILMSG(1, (_T("argv[%i]=%s \n"), i, argv[i]));
  19. }
  20. WCHAR* wsDllName=NULL;
  21. if ( !(wsDllName=wcsstr(argv[1], L".dll")) && !(wsDllName=wcsstr(argv[1], L".DLL") )) return 1;
  22. wsDllName=argv[1];
  23. WCHAR* wsProcName=NULL;
  24. if( !(wsProcName=wcsstr(argv[1], L",") )) return 1;
  25. *wsProcName++ = 0;
  26. /* WCHAR wsParStr[MAX_PATH];
  27. wcscpy(wsParStr, L"");
  28. for(i=2; i<argc; i++)
  29. {
  30. wcscat(wsParStr, argv[i]);
  31. wcscat(wsParStr, L" ");
  32. }
  33. */
  34. RETAILMSG(1, (_T("wsDllName=%s\nwsProcName=%s\n"),wsDllName, wsProcName));
  35. //DebugBreak();
  36. HRESULT hr=S_OK;
  37. DWORD err;
  38. HMODULE hModule=LoadLibrary(wsDllName);
  39. if (!hModule)
  40. {
  41. err=GetLastError();
  42. hr=HRESULT_FROM_WIN32(err);
  43. }
  44. if(FAILED(hr))
  45. {
  46. RETAILMSG(1, (_T("File: %s Line :%d, hr=%08x\n"),_T(__FILE__),__LINE__, hr));
  47. FreeLibrary(hModule);
  48. return 1;
  49. }
  50. FARPROC proc=GetProcAddress( hModule, wsProcName);
  51. RETAILMSG(1, (_T("File: %s Line :%d, proc=%08x\n"),_T(__FILE__),__LINE__, proc));
  52. hr = proc();
  53. // hr=HRESULT_FROM_WIN32(err);
  54. FreeLibrary(hModule);
  55. if (FAILED(hr)) return (1);
  56. else return (0);
  57. }