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.

71 lines
2.2 KiB

  1. #include "stdinc.h"
  2. #include "SxApwHandle.h"
  3. #include "SxApwCreate.h"
  4. typedef HRESULT (STDMETHODCALLTYPE* PFN_DLL_GET_CLASS_OBJECT)(REFCLSID rclsid, REFIID riid, LPVOID* ppv);
  5. HRESULT
  6. SxApwCreateObject(
  7. REFCLSID rclsid,
  8. REFIID riid,
  9. void** pp
  10. )
  11. /*
  12. Strategy:
  13. Enumerate .dlls in the same directory as the .exe, calling DllGetClassObject on each, etc..
  14. This is subject to change.
  15. This code is presently shared both by the host and the managers...maybe this is wrong.
  16. */
  17. {
  18. HRESULT hr = E_FAIL;
  19. CFindFile findFile;
  20. WCHAR exePath[MAX_PATH];
  21. WIN32_FIND_DATAW findData;
  22. PWSTR filePart;
  23. GetModuleFileName(GetModule()->m_hInst, exePath, RTL_NUMBER_OF(exePath));
  24. filePart = 1 + wcsrchr(exePath, '\\');
  25. wcscpy(filePart, L"*.dll");
  26. if (findFile.Win32Create(exePath, &findData))
  27. {
  28. do
  29. {
  30. PCWSTR dot = wcsrchr(findData.cFileName, '.');
  31. if (dot != NULL && _wcsicmp(dot, L".dll") == 0)
  32. {
  33. CDynamicLinkLibrary dll;
  34. PFN_DLL_GET_CLASS_OBJECT pfn;
  35. ATL::CComPtr<IClassFactory> classFactory;
  36. wcscpy(filePart, findData.cFileName);
  37. IFFALSE_WIN32TOHR_EXIT(hr, dll.Win32Create(exePath));
  38. if ( !dll.GetProcAddress( "DllGetClassObject", &pfn ) )
  39. {
  40. if ( ::GetLastError() == ERROR_PROC_NOT_FOUND )
  41. continue;
  42. else
  43. {
  44. DWORD LastError = ::GetLastError();
  45. TRACE_WIN32_FAILURE( GetProcAddress );
  46. hr = HRESULT_FROM_WIN32( LastError );
  47. goto Exit;
  48. }
  49. }
  50. if (FAILED(hr = pfn(rclsid, __uuidof(classFactory), reinterpret_cast<void**>(&classFactory))))
  51. continue;
  52. if (FAILED(hr = classFactory->CreateInstance(NULL, riid, pp)))
  53. continue;
  54. // hold the .dll open
  55. dll.Detach();
  56. goto Exit;
  57. }
  58. } while (FindNextFileW(findFile, &findData));
  59. }
  60. Exit:
  61. return hr;
  62. }