Hello Tim,
My apologies for the lack of information in my previous post. The full AddDevice routine is below. I did see the inconsistency in string formatting, e.g. %d vs. %c and I am verifying that the ASCII string contains the correct integer. Thank you for your advice.
NTSTATUS
OmsAddDevice(
IN PDRIVER_OBJECT DriverObject,
IN PDEVICE_OBJECT PhysicalDeviceObject
)
{
NTSTATUS status = STATUS_SUCCESS;
PDEVICE_OBJECT DeviceObject = NULL;
PDEVICE_EXTENSION DeviceInfo;
UNICODE_STRING ntDeviceName;
UNICODE_STRING Win32DeviceName;
WCHAR deviceInfo[RESPONSE_BUFF_SIZE] = { ‘\0’ };
WCHAR deviceName[RESPONSE_BUFF_SIZE] = { ‘\0’ };
WCHAR dosDeviceName[RESPONSE_BUFF_SIZE] = { ‘\0’ };
char sTag[4] = { ‘\0’ };
ULONG length = 0;
const unsigned int idIndex = 32;
WCHAR controllerId = 0;
ULONG tag = 0;
PAGED_CODE();
OmsKdPrint((“OmsMAXk.sys: OmsAddDevice\n”));
status = IoGetDeviceProperty(PhysicalDeviceObject,
DevicePropertyHardwareID, RESPONSE_BUFF_SIZE, deviceInfo, &length);
if (!NT_SUCCESS(status) && (length > idIndex)) {
OmsKdPrint((“OmsMAXk.sys: Obtaining device property error\n”));
return status;
}
controllerId = deviceInfo[idIndex];
_snwprintf(deviceName, RESPONSE_BUFF_SIZE, L"\Device\OmsMAXk%c", controllerId);
_snwprintf(dosDeviceName, RESPONSE_BUFF_SIZE, L"\DosDevices\OmsMAXk%c", controllerId);
OmsKdPrint((“Device name is %0LX\n”, deviceName));
OmsKdPrint((“Dos device name is $0LX\n”, dosDeviceName));
sprintf(sTag, “%cSMO”, controllerId);
OmsKdPrint((“sTag is %0LX\n”, sTag));
tag = (sTag[0] << 24) | (sTag[1] << 16) | (sTag[2] << 8) | sTag[3];
//Use the NT device name defined in OmsMAXk1.c, OmsMAXk2.c,
// OmsMAXk3.c or OmsMAXk4.c
RtlInitUnicodeString(&ntDeviceName, deviceName);
// Create the device object for the controller
status = IoCreateDevice(DriverObject,
sizeof(DEVICE_EXTENSION),
&ntDeviceName,
OMS_FILE_DEVICE,
0,
FALSE,
&DeviceObject);
if (!NT_SUCCESS(status))
{
// Either not enough memory to create a DeviceObject or another
// DeviceObject with the same name exists.
OmsKdPrint((“OmsMAXk.sys: Failed to create device object\n”));
return status;
}
OmsKdPrint((“OmsMAXk.sys: Successfully created device object\n”));
//Link the NT device name with the Win32 name used by applications
RtlInitUnicodeString(&Win32DeviceName, dosDeviceName);
status = IoCreateSymbolicLink(&Win32DeviceName, &ntDeviceName);
if (!NT_SUCCESS(status)) // If we we couldn’t create the link then
{ // abort installation.
OmsKdPrint((“OmsMAXk.sys: Failed to create symbolic link\n”));
return status;
}
OmsKdPrint((“OmsMAXk.sys: Successfully created symbolic link\n”));
DeviceInfo = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
//Attach the driver object to the driver stack.
DeviceInfo->NextLowerDriver = IoAttachDeviceToDeviceStack(
DeviceObject,
PhysicalDeviceObject);
if (NULL == DeviceInfo->NextLowerDriver)
{
//IoDeleteSymbolicLink(&Win32DeviceName);
IoDeleteDevice(DeviceObject);
OmsKdPrint((“OmsMAXk.sys: Failed to attach driver object to driver stack\n”));
return STATUS_NO_SUCH_DEVICE;
}
OmsKdPrint((“OmsMAXk.sys: Successfully attached driver object to driver stack\n”));
//Initialize the device object delete lock structure
IoInitializeRemoveLock(&DeviceInfo->RemoveLock,
tag,
1, // MaxLockedMinutes
5); // HighWatermark, this parameter is
// used only on checked build.
//Windows 7/10 64-bit drivers must be pagable
DeviceObject->Flags |= DO_POWER_PAGABLE;
DeviceInfo->Removed = FALSE;
DeviceInfo->Started = FALSE;
status = IoInitializeTimer(DeviceObject, IoTimer, DeviceInfo);
if (!NT_SUCCESS(status)) // If we couldn’t initialize IO Timer
{ // then abort installation.
OmsKdPrint((“OmsMAXk.sys: Failed to initialize IO Timer\n”));
return status;
}
OmsKdPrint((“OmsMAXk.sys: Successfully initialized IO Timer\n”));
//The device object intialization is complete
DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
DeviceInfo->ControllerId = controllerId;
return STATUS_SUCCESS;
}
Also, the user app that calls the driver uses the following:
sprintf(m_strDevice, “\\.\OmsMAXk%d”, id);
// Open the motor controller device
m_hController = CreateFile ( m_strDevice, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);
where “id” is an int between 1 and 4.