Leaked source code of windows server 2003
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.

126 lines
3.7 KiB

  1. #include <windows.h>
  2. #include <objbase.h>
  3. #include <atlbase.h>
  4. #include <wianew.h>
  5. #include <simreg.h>
  6. #include <dumpprop.h>
  7. #include <devlist.h>
  8. #include <simbstr.h>
  9. #include <stdio.h>
  10. typedef HANDLE (WINAPI *WiaAddDeviceProc)();
  11. typedef BOOL (WINAPI *WiaRemoveDeviceProc)(STI_DEVICE_INFORMATION *);
  12. HINSTANCE g_hInstance;
  13. bool RemoveDevice( LPCWSTR pszDeviceId )
  14. {
  15. bool bResult = false;
  16. CComPtr<IStillImage> pStillImage;
  17. if (SUCCEEDED(StiCreateInstance( g_hInstance, STI_VERSION, &pStillImage, NULL)) && pStillImage)
  18. {
  19. HINSTANCE hClassInstaller = LoadLibrary(TEXT("sti_ci.dll"));
  20. if (hClassInstaller)
  21. {
  22. WiaRemoveDeviceProc pfnWiaRemoveDeviceProc = reinterpret_cast<WiaRemoveDeviceProc>(GetProcAddress(hClassInstaller, "WiaRemoveDevice"));
  23. if (pfnWiaRemoveDeviceProc)
  24. {
  25. STI_DEVICE_INFORMATION *pStiDeviceInformation = NULL;
  26. if (SUCCEEDED(pStillImage->GetDeviceInfo( const_cast<LPWSTR>(pszDeviceId), reinterpret_cast<LPVOID*>(&pStiDeviceInformation))) && pStiDeviceInformation )
  27. {
  28. bResult = (pfnWiaRemoveDeviceProc( pStiDeviceInformation ) != FALSE);
  29. if (!bResult)
  30. {
  31. wprintf( L"WiaRemoveDeviceProc failed\n");
  32. }
  33. LocalFree(pStiDeviceInformation);
  34. }
  35. else
  36. {
  37. wprintf( L"GetDeviceInfo on %ws failed\n", pszDeviceId );
  38. }
  39. }
  40. else
  41. {
  42. wprintf( L"GetProcAddress on WiaRemoveDeviceProc failed\n");
  43. }
  44. FreeLibrary( hClassInstaller );
  45. }
  46. else
  47. {
  48. wprintf( L"LoadLibrary failed\n");
  49. }
  50. }
  51. else
  52. {
  53. wprintf( L"StiCreateInstance failed\n");
  54. }
  55. return bResult;
  56. }
  57. void RemoveAllDevices()
  58. {
  59. HRESULT hr = CoInitialize(NULL);
  60. if (SUCCEEDED(hr))
  61. {
  62. CComPtr<IWiaDevMgr> pWiaDevMgr;
  63. if (SUCCEEDED(CoCreateInstance( CLSID_WiaDevMgr, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr, (void**)&pWiaDevMgr )))
  64. {
  65. CDeviceList DeviceList( pWiaDevMgr );
  66. for (int i=0;i<DeviceList.Size();++i)
  67. {
  68. CSimpleStringWide strwDeviceId;
  69. if (PropStorageHelpers::GetProperty( DeviceList[i], WIA_DIP_DEV_ID, strwDeviceId ))
  70. {
  71. CSimpleStringWide strwDeviceName;
  72. PropStorageHelpers::GetProperty( DeviceList[i], WIA_DIP_DEV_NAME, strwDeviceName );
  73. wprintf( L"Removing %ws -- %ws\n", strwDeviceId.String(), strwDeviceName.String() );
  74. wprintf( L"%ws\n", RemoveDevice( strwDeviceId ) ? L"succeeded" : L"failed" );
  75. }
  76. }
  77. }
  78. }
  79. CoUninitialize();
  80. }
  81. int __cdecl wmain( int argc, wchar_t *argv[] )
  82. {
  83. g_hInstance = GetModuleHandle(NULL);
  84. WIA_DEBUG_CREATE(g_hInstance);
  85. bool bRemove = false;
  86. for (int i=1;i<argc;i++)
  87. {
  88. if (L'/' == argv[i][0] || L'-' == argv[i][0])
  89. {
  90. switch (argv[i][1])
  91. {
  92. case L'y':
  93. case L'Y':
  94. bRemove = true;
  95. break;
  96. }
  97. }
  98. }
  99. if (!bRemove)
  100. {
  101. wprintf( L"Do you want to remove all of your WIA devices? (y/n): " );
  102. int chResponse = getchar();
  103. if (L'y' == chResponse || L'Y' == chResponse)
  104. {
  105. bRemove = true;
  106. }
  107. }
  108. if (bRemove)
  109. {
  110. RemoveAllDevices();
  111. }
  112. return 0;
  113. }