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.

172 lines
4.7 KiB

  1. // Dot4Port.cpp: implementation of the Dot4Port class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "precomp.h"
  5. #include "initguid.h"
  6. #include "ntddpar.h"
  7. const TCHAR cszCFGMGR32[]=TEXT("cfgmgr32.dll");
  8. const CHAR cszReenumFunc[]="CM_Reenumerate_DevNode_Ex";
  9. //////////////////////////////////////////////////////////////////////
  10. // Construction/Destruction
  11. //////////////////////////////////////////////////////////////////////
  12. CDot4Port::CDot4Port( BOOL bActive, LPTSTR pszPortName, LPTSTR pszDevicePath )
  13. : CBasePort( bActive, pszPortName, pszDevicePath, cszDot4PortDesc )
  14. {
  15. // Basically let the default constructor do the work.
  16. }
  17. CDot4Port::~CDot4Port()
  18. {
  19. }
  20. PORTTYPE CDot4Port::getPortType()
  21. {
  22. return DOT4PORT;
  23. }
  24. BOOL CDot4Port::checkPnP()
  25. {
  26. SETUPAPI_INFO SetupApiInfo;
  27. BOOL bRet = FALSE;
  28. DWORD dwIndex, dwLastError;
  29. UINT uOldErrorMode;
  30. HANDLE hToken = NULL;
  31. HDEVINFO hDevList = INVALID_HANDLE_VALUE;
  32. SP_DEVINFO_DATA DeviceInfoData;
  33. HINSTANCE hCfgMgr32 = 0; // Library instance.
  34. // Pointers to the pnp function...
  35. pfCM_Reenumerate_DevNode_Ex pfnReenumDevNode;
  36. if ( !LoadSetupApiDll( &SetupApiInfo ) )
  37. return FALSE;
  38. // For a dot4 device we need to force a pnp event on the parallel port to get the
  39. // dot4 stack rebuilt.
  40. // If any of these fail, fail the call just as if the port couldn't be opened.
  41. //
  42. // Load the pnp dll.
  43. uOldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
  44. hCfgMgr32 = LoadLibrary( cszCFGMGR32 );
  45. if(!hCfgMgr32)
  46. {
  47. SetErrorMode(uOldErrorMode);
  48. goto Done;
  49. }
  50. SetErrorMode(uOldErrorMode);
  51. //
  52. // Get the Addressed of pnp functions we want to call...
  53. //
  54. pfnReenumDevNode = (pfCM_Reenumerate_DevNode_Ex)GetProcAddress( hCfgMgr32, cszReenumFunc );
  55. if( !pfnReenumDevNode )
  56. goto Done;
  57. hToken = RevertToPrinterSelf();
  58. if ( !hToken )
  59. {
  60. dwLastError = GetLastError();
  61. goto Done;
  62. }
  63. hDevList = SetupApiInfo.GetClassDevs( (LPGUID) &GUID_PARALLEL_DEVICE,
  64. NULL, NULL, DIGCF_INTERFACEDEVICE);
  65. if ( hDevList == INVALID_HANDLE_VALUE )
  66. {
  67. dwLastError = GetLastError();
  68. goto Done;
  69. }
  70. // Now get the DevInst handles for each DevNode
  71. dwLastError = ERROR_SUCCESS;
  72. dwIndex = 0;
  73. do
  74. {
  75. DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  76. if ( !SetupApiInfo.EnumDeviceInfo( hDevList,
  77. dwIndex,
  78. &DeviceInfoData) )
  79. {
  80. dwLastError = GetLastError();
  81. if ( dwLastError == ERROR_NO_MORE_ITEMS )
  82. break; // Normal exit
  83. DBGMSG(DBG_WARNING,
  84. ("DynaMon: Dot4.CheckPnP: SetupDiEnumDeviceInfo failed with %d for index %d\n",
  85. dwLastError, dwIndex));
  86. goto Next;
  87. }
  88. // ReEnum on the current parallel port DevNode
  89. pfnReenumDevNode( DeviceInfoData.DevInst, CM_REENUMERATE_NORMAL, NULL );
  90. Next:
  91. dwLastError = ERROR_SUCCESS;
  92. ++dwIndex;
  93. } while ( dwLastError == ERROR_SUCCESS );
  94. // Revert back to the user's context.
  95. if ( !ImpersonatePrinterClient(hToken) )
  96. {
  97. // Failure - Clear token so it won't happen again and save the error
  98. hToken = NULL;
  99. dwLastError = GetLastError();
  100. goto Done;
  101. }
  102. // Impersonate worked so clear token
  103. hToken = NULL;
  104. // Try and open the port again.
  105. // If we fail, then the device must not be there any more or still switched off - fail as usual.
  106. m_hDeviceHandle = CreateFile( m_szDevicePath,
  107. GENERIC_WRITE | GENERIC_READ,
  108. FILE_SHARE_READ | FILE_SHARE_WRITE,
  109. NULL,
  110. OPEN_EXISTING,
  111. FILE_FLAG_OVERLAPPED,
  112. NULL);
  113. if ( m_hDeviceHandle == INVALID_HANDLE_VALUE )
  114. goto Done;
  115. bRet = TRUE;
  116. Done:
  117. if ( hDevList != INVALID_HANDLE_VALUE )
  118. SetupApiInfo.DestroyDeviceInfoList( hDevList );
  119. if (hToken)
  120. {
  121. if ( !ImpersonatePrinterClient(hToken) )
  122. {
  123. if (bRet)
  124. {
  125. dwLastError = GetLastError();
  126. bRet = FALSE;
  127. }
  128. }
  129. }
  130. if ( hCfgMgr32 )
  131. FreeLibrary( hCfgMgr32 );
  132. if ( SetupApiInfo.hSetupApi )
  133. FreeLibrary(SetupApiInfo.hSetupApi);
  134. SetLastError( dwLastError );
  135. return bRet;
  136. }