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.

188 lines
5.4 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. void DumpDevice( IWiaDevMgr *pWiaDevMgr, HANDLE hFile, const CSimpleStringWide &strDeviceID )
  11. {
  12. //
  13. // Create the device manager
  14. //
  15. fprintf( stderr, "Creating [%ls]\n", strDeviceID.String() );
  16. CComPtr<IWiaItem> pWiaItem;
  17. HRESULT hr = pWiaDevMgr->CreateDevice( CSimpleBStr(strDeviceID), &pWiaItem );
  18. if (SUCCEEDED(hr))
  19. {
  20. CSimpleStringWide strDeviceName;
  21. PropStorageHelpers::GetProperty( pWiaItem, WIA_DIP_DEV_NAME, strDeviceName );
  22. CWiaDebugDumpToFileHandle DebugDump( hFile );
  23. DebugDump.Print( TEXT("") );
  24. DebugDump.Print( CSimpleString().Format( TEXT("Device: %ws"), strDeviceName.String() ) );
  25. DebugDump.Print( TEXT("===============================================") );
  26. DebugDump.DumpRecursive(pWiaItem);
  27. }
  28. }
  29. bool GetStringArgument( int argc, wchar_t *argv[], CSimpleString &strArgument, int &nCurrArg )
  30. {
  31. bool bResult = true;
  32. if (lstrlenW(argv[nCurrArg]) > 2)
  33. {
  34. if (argv[nCurrArg][2] == L':')
  35. {
  36. strArgument = argv[nCurrArg] + 3;
  37. }
  38. else
  39. {
  40. strArgument = argv[nCurrArg] + 2;
  41. }
  42. }
  43. else if (nCurrArg < argc-1)
  44. {
  45. strArgument = argv[++nCurrArg];
  46. }
  47. else bResult = false;
  48. return bResult;
  49. }
  50. bool ParseArguments( int argc, wchar_t *argv[], CSimpleDynamicArray<CSimpleString> &DeviceIDs, CSimpleString &strOutputFile, bool &bHidden )
  51. {
  52. int nCurrArg = 1;
  53. while (nCurrArg < argc)
  54. {
  55. if (argv[nCurrArg][0] == L'-')
  56. {
  57. switch (argv[nCurrArg][1])
  58. {
  59. case 'd':
  60. {
  61. CSimpleString strArg;
  62. if (GetStringArgument( argc, argv, strArg, nCurrArg ))
  63. {
  64. DeviceIDs.Append(strArg);
  65. }
  66. }
  67. break;
  68. case 'o':
  69. {
  70. CSimpleString strArg;
  71. if (GetStringArgument( argc, argv, strArg, nCurrArg ))
  72. {
  73. strOutputFile = strArg;
  74. }
  75. }
  76. break;
  77. case 'h':
  78. {
  79. bHidden = true;
  80. }
  81. break;
  82. }
  83. }
  84. nCurrArg++;
  85. }
  86. return true;
  87. }
  88. class CCoInitialize
  89. {
  90. private:
  91. HRESULT m_hr;
  92. private:
  93. CCoInitialize( const CCoInitialize & );
  94. CCoInitialize &operator=( const CCoInitialize & );
  95. public:
  96. CCoInitialize()
  97. {
  98. m_hr = CoInitialize(NULL);
  99. }
  100. ~CCoInitialize()
  101. {
  102. if (SUCCEEDED(m_hr))
  103. {
  104. CoUninitialize();
  105. }
  106. }
  107. HRESULT Result(void) const
  108. {
  109. return m_hr;
  110. }
  111. };
  112. int __cdecl wmain( int argc, wchar_t *argv[] )
  113. {
  114. CSimpleDynamicArray<CSimpleString> DeviceIDs;
  115. CSimpleString strOutputFile;
  116. bool bHidden = false;
  117. if (ParseArguments(argc,argv,DeviceIDs,strOutputFile,bHidden))
  118. {
  119. HANDLE hFile = INVALID_HANDLE_VALUE;
  120. if (strOutputFile.Length())
  121. {
  122. hFile = CreateFile( strOutputFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  123. }
  124. else
  125. {
  126. hFile = GetStdHandle(STD_OUTPUT_HANDLE);
  127. }
  128. if (INVALID_HANDLE_VALUE != hFile)
  129. {
  130. //
  131. // Initialize COM
  132. //
  133. CCoInitialize coinit;
  134. if (SUCCEEDED(coinit.Result()))
  135. {
  136. //
  137. // Create the device manager
  138. //
  139. CComPtr<IWiaDevMgr> pWiaDevMgr;
  140. HRESULT hr = CoCreateInstance( CLSID_WiaDevMgr, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr, (void**)&pWiaDevMgr );
  141. if (SUCCEEDED(hr))
  142. {
  143. if (DeviceIDs.Size())
  144. {
  145. for (int i=0;i<DeviceIDs.Size();i++)
  146. {
  147. DumpDevice( pWiaDevMgr, hFile, CSimpleStringConvert::WideString(DeviceIDs[i]) );
  148. }
  149. }
  150. else
  151. {
  152. CDeviceList DeviceList(pWiaDevMgr,StiDeviceTypeDefault,bHidden ? 0xFFFFFFFF : 0);
  153. for (int i=0;i<DeviceList.Size();i++)
  154. {
  155. CSimpleStringWide strDeviceID;
  156. if (PropStorageHelpers::GetProperty( DeviceList[i], WIA_DIP_DEV_ID, strDeviceID ))
  157. {
  158. DumpDevice( pWiaDevMgr, hFile, CSimpleStringConvert::WideString(strDeviceID) );
  159. }
  160. else
  161. {
  162. fprintf( stderr, "Unable to get the device ID\n" );
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. else
  170. {
  171. fprintf( stderr, "Unable to open %ls for writing\n", strOutputFile.String() );
  172. }
  173. }
  174. return 0;
  175. }