[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

INT File being Rejested, Application CreateFile() Call fails when trying to access the driver.

Posted on 2009-04-18
4
Medium Priority
?
523 Views
Last Modified: 2013-12-03
Hi Guys,

I am completely new windows drivers.
I have been doing Linux/RTOS drivers extensively but NOT windows until now. So, I read some docs extensively from MSDN about WDK before coming here.

I am trying to debug a Windows Vista Firewall (FE)driver written by someone else.

About the driver:
Ther driver is a simple IOCTL based drivers, NO Read, NO Write.  
He just registers the driver and IOCTL only. He also create a queue.
The driver is designed to support Vista 32-bit OS. I am testing in on the same.
Also it is a non-pnp driver. It does not have the AddDevice Call implemented. Everything is done just in the DriverEntry Call.

Problem 1:
Application CreateFile() call fails with Error "2", which means "There is no file present". (In Linux we see thios error if the driver is NOT loaded properly/failed at open time.)
So, I started reading the code and found problem 2. below.
 
Problem 2:
With What I read, I think this is a Device Installation Application type of loading a driver. link below.
     http://msdn.microsoft.com/en-us/library/dd419816.aspx

It is a Firewall client Application.
When I look into the code, I see that the application tries to find the waclient.sys driver in the system32/drivers folder.  And if it is NOT present, it gets the driver data which is embedded within the application with FindResource, LoadResouce Calls.
Then it creates a waclient.sys and copies the data into it.
Thats it.
 
They do nothing after it. Will the load the driver. I doubt what they do?
What else is needed in this case to load the driver??
 
Problem 3:
Understaing that there is a problem above, I wanted to try to load the driver manually "New Hardware" Wizard.
With What I have read from doc in the link below
http://www.microsoft.com/whdc/driver/wdf/KMDF-build.mspx

The driver code does not have a INX in it. So, not INF file is generated. So, I wrote a simple (may be completely wrong)  INF file by seeing the docs in the link below.
 
I created a directory on my "E:\test_drv", copied the
    waclient.sys file (checked build),
    waclient.inf (written by me)
    and the C:\WinDDK\6001.18002\redist\wdf\x86\WdfCoInstaller01007.
 
Ran the Windows New Hardware Wizard and point it to that Directory. It said
"The folder you specified does't contain a compatible software driver for your device.
  If the folder contains a driver, make sure it is designed to work with Windows for 32-bit systems."
 
What is wrong in my INF file that it comes with this message?
 
I might be missing some information you need. Please ask me. Am I missing anything. What else am I required to do to load the driver on my test system.
 
- Mukund Jampala
 

INT FILE
----------
[DestinationDirs]
waclientClassInstallerCopyFiles = 11
 
[waclient.NT.CoInstallers]
AddReg=waclient_CoInstaller_AddReg
CopyFiles= waclientClassInstallerCopyFiles
 
[waclient_CoInstaller_AddReg]
HKR,,CoInstallers32,0x00010007, "WdfCoInstaller01007.dll,WdfCoInstaller"
 
[waclient_CoInstaller_CopyFiles]
WdfCoInstaller01007.dll
 
[SourceDisksFiles]
WdfCoInstaller01007.dll=1 ;
 
[waclient.NT.Wdf]
KmdfService = waclient, waclient_wdfsect
[waclient_wdfsect]
KmdfLibraryVersion = 1.7

Open in new window

0
Comment
Question by:bhanuj
3 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 24178760
Please show your code for the CreateFile() function call. It is most likely a simple mispelling of the filename, incorrect path, etc..
0
 
LVL 86

Accepted Solution

by:
jkr earned 1500 total points
ID: 24179106
'CreateFile()' does not load a driver, just connects to an already loaded one. Installing and loading is done as follows:
#include <windows.h>
#include <stdlib.h>
#include <string.h>
 
 
/****************************************************************************
*
*    FUNCTION: InstallDriver( IN SC_HANDLE, IN LPCTSTR, IN LPCTSTR)
*
*    PURPOSE: Creates a driver service.
*
****************************************************************************/
BOOL InstallDriver( IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName, IN LPCTSTR ServiceExe )
{
    SC_HANDLE  schService;
 
    //
    // NOTE: This creates an entry for a standalone driver. If this
    //       is modified for use with a driver that requires a Tag,
    //       Group, and/or Dependencies, it may be necessary to
    //       query the registry for existing driver information
    //       (in order to determine a unique Tag, etc.).
    //
 
    schService = CreateService( SchSCManager,          // SCManager database
                                DriverName,           // name of service
                                DriverName,           // name to display
                                SERVICE_ALL_ACCESS,    // desired access
                                SERVICE_KERNEL_DRIVER, // service type
                                SERVICE_DEMAND_START,  // start type
                                SERVICE_ERROR_NORMAL,  // error control type
                                ServiceExe,            // service's binary
                                NULL,                  // no load ordering group
                                NULL,                  // no tag identifier
                                NULL,                  // no dependencies
                                NULL,                  // LocalSystem account
                                NULL                   // no password
                                );
    if ( schService == NULL )
        return FALSE;
 
    CloseServiceHandle( schService );
 
    return TRUE;
}
 
 
/****************************************************************************
*
*    FUNCTION: StartDriver( IN SC_HANDLE, IN LPCTSTR)
*
*    PURPOSE: Starts the driver service.
*
****************************************************************************/
BOOL StartDriver( IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName )
{
    SC_HANDLE  schService;
    BOOL       ret;
 
    schService = OpenService( SchSCManager,
                              DriverName,
                              SERVICE_ALL_ACCESS
                              );
    if ( schService == NULL )
        return FALSE;
 
    ret = StartService( schService, 0, NULL )
       || GetLastError() == ERROR_SERVICE_ALREADY_RUNNING 
	   || GetLastError() == ERROR_SERVICE_DISABLED;
 
    CloseServiceHandle( schService );
 
    return ret;
}
 
 
 
/****************************************************************************
*
*    FUNCTION: OpenDevice( IN LPCTSTR, HANDLE *)
*
*    PURPOSE: Opens the device and returns a handle if desired.
*
****************************************************************************/
BOOL OpenDevice( IN LPCTSTR DriverName, HANDLE * lphDevice )
{
    TCHAR    completeDeviceName[64];
    HANDLE   hDevice;
 
    //
    // Create a \\.\XXX device name that CreateFile can use
    //
    // NOTE: We're making an assumption here that the driver
    //       has created a symbolic link using it's own name
    //       (i.e. if the driver has the name "XXX" we assume
    //       that it used IoCreateSymbolicLink to create a
    //       symbolic link "\DosDevices\XXX". Usually, there
    //       is this understanding between related apps/drivers.
    //
    //       An application might also peruse the DEVICEMAP
    //       section of the registry, or use the QueryDosDevice
    //       API to enumerate the existing symbolic links in the
    //       system.
    //
 
	if( GetVersion() & 0xFF >= 5 ) {
 
		//
		// We reference the global name so that the application can
		// be executed in Terminal Services sessions on Win2K
		//
		wsprintf( completeDeviceName, TEXT("\\\\.\\Global\\%s"), DriverName );
 
	} else {
 
		wsprintf( completeDeviceName, TEXT("\\\\.\\%s"), DriverName );
	}
    hDevice = CreateFile( completeDeviceName,
                          GENERIC_READ | GENERIC_WRITE,
                          0,
                          NULL,
                          OPEN_EXISTING,
                          FILE_ATTRIBUTE_NORMAL,
                          NULL
                          );
    if ( hDevice == ((HANDLE)-1) )
        return FALSE;
 
	// If user wants handle, give it to them.  Otherwise, just close it.
	if ( lphDevice )
		*lphDevice = hDevice;
	else
	    CloseHandle( hDevice );
 
    return TRUE;
}
 
 
 
/****************************************************************************
*
*    FUNCTION: StopDriver( IN SC_HANDLE, IN LPCTSTR)
*
*    PURPOSE: Has the configuration manager stop the driver (unload it)
*
****************************************************************************/
BOOL StopDriver( IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName )
{
    SC_HANDLE       schService;
    BOOL            ret;
    SERVICE_STATUS  serviceStatus;
 
    schService = OpenService( SchSCManager, DriverName, SERVICE_ALL_ACCESS );
    if ( schService == NULL )
        return FALSE;
 
    ret = ControlService( schService, SERVICE_CONTROL_STOP, &serviceStatus );
 
    CloseServiceHandle( schService );
 
    return ret;
}
 
 
/****************************************************************************
*
*    FUNCTION: RemoveDriver( IN SC_HANDLE, IN LPCTSTR)
*
*    PURPOSE: Deletes the driver service.
*
****************************************************************************/
BOOL RemoveDriver( IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName )
{
    SC_HANDLE  schService;
    BOOL       ret;
 
    schService = OpenService( SchSCManager,
                              DriverName,
                              SERVICE_ALL_ACCESS
                              );
 
    if ( schService == NULL )
        return FALSE;
 
    ret = DeleteService( schService );
 
    CloseServiceHandle( schService );
 
    return ret;
}
 
 
/****************************************************************************
*
*    FUNCTION: UnloadDeviceDriver( const TCHAR *)
*
*    PURPOSE: Stops the driver and has the configuration manager unload it.
*
****************************************************************************/
BOOL UnloadDeviceDriver( const TCHAR * Name )
{
	SC_HANDLE	schSCManager;
 
	schSCManager = OpenSCManager(	NULL,                 // machine (NULL == local)
                              		NULL,                 // database (NULL == default)
									SC_MANAGER_ALL_ACCESS // access required
								);
 
	StopDriver( schSCManager, Name );
	RemoveDriver( schSCManager, Name );
	 
	CloseServiceHandle( schSCManager );
 
	return TRUE;
}
 
 
 
/****************************************************************************
*
*    FUNCTION: LoadDeviceDriver( const TCHAR, const TCHAR, HANDLE *)
*
*    PURPOSE: Registers a driver with the system configuration manager 
*	 and then loads it.
*
****************************************************************************/
BOOL LoadDeviceDriver( const TCHAR * Name, const TCHAR * Path, 
					  HANDLE * lphDevice, PDWORD Error )
{
	SC_HANDLE	schSCManager;
	BOOL		okay;
 
	schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
 
	// Remove previous instance
	RemoveDriver( schSCManager, Name );
 
	// Ignore success of installation: it may already be installed.
	InstallDriver( schSCManager, Name, Path );
 
	// Ignore success of start: it may already be started.
	StartDriver( schSCManager, Name );
 
	// Do make sure we can open it.
	okay = OpenDevice( Name, lphDevice );
	*Error = GetLastError();
 	CloseServiceHandle( schSCManager );
 
	return okay;
}

Open in new window

0
 

Author Closing Comment

by:bhanuj
ID: 31571905
Thanks for the solution,
I got a direction with this help to where to go form here.
The real problem was that the driver was not being built properly and the service calls that were to used create and start service were incorrect. The create/start service fialure cases was not handled properly. Fixed these places and the App goes Kool.

Thank you
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question