User-Mode
Handle Open/Close Code Fragments

Copyright © 2003 by Printing Communications Assoc., Inc. (PCAUSA). All rights reserved

After adding the PassThruEx skeleton I/O dispatch handlers (and building a driver with these additions...) it is possible to start developing the Win32 test application. If everything is glued together correctly then we should see this message when we run PTUserIo:

"PassThru Handle Open Successful"

Here are the key functions that are implemented in PTUserIo.cpp at this early stage:

 

/////////////////////////////////////////////////////////////////////////////
//// PtOpenControlChannel
//
// Purpose
// Open a "control channel" handle on the PassThru device.
//
// Parameters
//    None.
//
// Return Value
//   The INVALIE_HANDLE_VALUE if unsuccessful. Otherwise, a valid handle
//   to the passthru device.
//
// Remarks
//   There are no parameters to this function because the PassThru filespec
//   name is already known. For PassThru this is "\\.\PassThru" or
//   "\\.\Global\PassThru"
//
//   This call opens a "control channel". That is, a handle that can be
//   used for DeviceIoControl calls but is not associated with a specific
//   adapter.
//
//   Notice that the FILE_FLAG_OVERLAPPED attribute is not specified. The
//   returned handle is used for synchronous operations only.
//
//   A more sophisticated API would employ asynchronous I/O. However, a
//   sample of that complexity is beyond the scope of this article.
//
HANDLE
PtOpenControlChannel( void )
{
   DWORD	DesiredAccess;
   DWORD	ShareMode;
   LPSECURITY_ATTRIBUTES	lpSecurityAttributes = NULL;
   DWORD	CreationDistribution;
   DWORD	FlagsAndAttributes;
   HANDLE	TemplateFile;
   HANDLE	Handle;
   //
   // Use CreateFile to Open the Handle
   //
   DesiredAccess = GENERIC_READ|GENERIC_WRITE;
   ShareMode = 0;
   CreationDistribution = OPEN_EXISTING;
   FlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
   TemplateFile = (HANDLE)INVALID_HANDLE_VALUE;
   Handle = CreateFile(
      "\\\\.\\PassThru",
      DesiredAccess,
      ShareMode,
      lpSecurityAttributes,
      CreationDistribution,
      FlagsAndAttributes,
      TemplateFile
      );
   if( Handle == INVALID_HANDLE_VALUE )
   {
      // See Microsoft KB Article 259131
      Handle = CreateFile(
         "\\\\.\\Globals\\PassThru",
         DesiredAccess,
         ShareMode,
         lpSecurityAttributes,
         CreationDistribution,
         FlagsAndAttributes,
         TemplateFile
         );
   }
   return (Handle);
}
/////////////////////////////////////////////////////////////////////////////
//// _tmain
//
// Purpose
// PTUserIo MFC console application MAIN entry point.
//
// Parameters
//
// Return Value
//
// Remarks
//
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
   int nRetCode = 0;

   //
   // Open A Handle On The PassThru Device
   //
   HANDLE PtHandle = PtOpenControlChannel();
   if( PtHandle == INVALID_HANDLE_VALUE )
   {
      cout << "PassThru Handle Open Failed" << endl;
      nRetCode = 1;
      return nRetCode;
   }
   cout << "PassThru Handle Open Successful" << endl;
   CloseHandle( PtHandle );
   return nRetCode;
}