Call Driver Functions by Using Device Manager (Compact 7)
3/12/2014
As described in Plan Your Device Driver, applications do not directly call the device driver exported functions. Instead, calls that are made within the application cause Device Manager to call the device driver for you.
Before you can test your driver, you must call ActivateDeviceEx (or ActivateDevice). This call causes Device Manager to load your driver DLL. After the driver is successfully loaded, you can open the driver by calling CreateFile.
The following code example shows how to load, open, close, and deactivate the driver.
#include "stdafx.h"
int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
{
HANDLE hActiveDriver = NULL;
HANDLE hDriver = NULL;
bool bReturn = false;
// Ask Device Manager to load the driver
hActiveDriver = ActivateDeviceEx(L"\\Drivers\\streamdriver", NULL, 0, NULL);
if (hActiveDriver == INVALID_HANDLE_VALUE)
{
ERRORMSG(1, (TEXT("Unable to load stream driver.")));
return -1;
}
// Open the driver
hDriver = CreateFile (L"SDT1:",
GENERIC_READ| GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hDriver == INVALID_HANDLE_VALUE)
{
ERRORMSG(1, (TEXT("Unable to open stream driver.")));
return 0;
}
// Add test code here
// Close the driver
if (hDriver != INVALID_HANDLE_VALUE)
{
bReturn = CloseHandle(hDriver);
if (bReturn == FALSE)
{
ERRORMSG(1, (TEXT("Unable to close stream driver.")));
}
}
// Ask Device Manager to unload the driver
if (hActiveDriver != INVALID_HANDLE_VALUE)
{
bReturn = DeactivateDevice(hActiveDriver);
if (bReturn == FALSE)
{
ERRORMSG(1, (TEXT("Unable to unload stream driver.")));
}
}
return 0;
}