First time poster, hopefully someone can provide pointers on my newbie issue.
Trying to open “FOO” device interface from userspace test app, and register for PnP events on same. Cannot seem to successfully open handle on interface, have tried various approaches but keep getting unknown path errors. My KMDF driver exists and interface created without error. Below is significant portion of code being used. Can someone suggest what might be wrong, and/or point to a suitable example for similar usecase?
Thanks,
Andrew
// {8A38B6CD-8C69-4142-A722-80B7BFAE570E}
static const GUID FOO_INTERFACE_GUID =
{ 0x8a38b6cd, 0x8c69, 0x4142, { 0xa7, 0x22, 0x80, 0xb7, 0xbf, 0xae, 0x57, 0xe } };
#define FOO_NTDEVICE_NAME_STRING L"\Device\FOO"
DWORD __callback CALLBACK
CMNotifyCb(
In HCMNOTIFICATION hNotify,
In_opt PVOID Context,
In CM_NOTIFY_ACTION Action,
In_reads_bytes(EventDataSize) PCM_NOTIFY_EVENT_DATA EventData,
In DWORD EventDataSize
)
{
PAPP_STATE_TYPE pAppState = (PAPP_STATE_TYPE)Context;
DWORD size;
UNREFERENCED_PARAMETER(hNotify);
if (!pAppState)
return ERROR_INVALID_ADDRESS;
wprintf(L"CMNotify callback: Action[%d] Data[%S]\n", Action, EventData->u.DeviceInterface.SymbolicLink);
// determine size of just symbolic link array
size = (EventDataSize - ((UINT_PTR)EventData->u.DeviceInterface.SymbolicLink - (UINT_PTR)EventData));
switch (Action) {
case CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL:
memcpy_s(pAppState->SymbolicLink, sizeof(pAppState->SymbolicLink),
EventData->u.DeviceInterface.SymbolicLink, size);
pAppState->IfaceExists = TRUE;
break;
case CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL:
pAppState->IfaceExists = FALSE;
break;
default:
// Do nothing
break;
}
return SUCCESS;
}
DWORD
RegisterInterface(PAPP_STATE_TYPE pAppState)
{
CM_NOTIFY_FILTER CMfilter;
CONFIGRET ret;
HANDLE handle;
DWORD status = SUCCESS;
// Register for interface state changes
// initialize CM filter
memset(&CMfilter, 0x0, sizeof(CMfilter));
CMfilter.cbSize = sizeof(CMfilter);
CMfilter.FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE;
CMfilter.u.DeviceInterface.ClassGuid = FOO_INTERFACE_GUID;
ret = CM_Register_Notification(&CMfilter, pAppState, CMNotifyCb, &pAppState->hCMNotify);
if (CR_SUCCESS != ret) {
wprintf(L"Failed on CM_Register_Notification!\n");
status = FAILURE;
goto ON_EXIT;
}
ON_EXIT:
return status;
}
DWORD
OpenDevice(PAPP_STATE_TYPE pAppState)
{
HANDLE hDevice = INVALID_HANDLE_VALUE;
DWORD status = SUCCESS;
#if 0
wprintf(L"Opening file: %ws\n", pAppState->SymbolicLink);
hDevice = CreateFileW(pAppState->SymbolicLink, // name
(GENERIC_READ|GENERIC_WRITE), // access
0, // share mode
NULL, // default security attributes
OPEN_EXISTING, // disposition
FILE_FLAG_OVERLAPPED, // file attributes
NULL); // do not copy file attributes
#else
{
WCHAR ntDeviceName[60] = {0,};
// WCHAR ntDeviceName[60] = L"\";
wcscat_s(ntDeviceName, sizeof(ntDeviceName), FOO_NTDEVICE_NAME_STRING);
wprintf(L"Opening file: %ws\n", ntDeviceName);
hDevice = CreateFileW(ntDeviceName, // name
(GENERIC_READ | GENERIC_WRITE), // access
0, // share mode
NULL, // default security attributes
OPEN_EXISTING, // disposition
FILE_FLAG_OVERLAPPED, // file attributes
NULL); // do not copy file attributes
}
#endif // 0
if (INVALID_HANDLE_VALUE == hDevice) {
DWORD err = GetLastError();
wprintf(L"Failed on CreateFile! status = %d\n", err);
status = FAILURE;
goto ON_EXIT;
}
pAppState->hDevice = hDevice;
ON_EXIT:
return status;
}
int
_tmain(int argc, _TCHAR* argv)
{
DWORD status = 0;
BOOL ret = FALSE;
APP_STATE_TYPE AppState = { 0, };
// Register for FOO interface notifications
status = RegisterInterface (&AppState);
if (SUCCESS != status) {
wprintf(L"Failed on RegisterInterface!\n");
goto ON_EXIT;
}
#if 0
// wait on FOO interface arrival
wprintf(L"Waiting for FOO interface…\n");
status = WaitForSingleObject(AppState.hEvent, 1000); // msec
if (WAIT_OBJECT_0 != status) {
wprintf(L"Failed on WaitForSingleObject!\n");
goto ON_EXIT;
}
ResetEvent(AppState.hEvent);
#else
Sleep(1000); // msec
#endif // 0
// open device interface
status = OpenDevice(&AppState);
if (SUCCESS != status) {
wprintf(L"Failed on OpenDevice!\n");
goto ON_EXIT;
}
// send IOCTL
ret = DeviceIoControl(AppState.hDevice,
(DWORD)IOCTL_FOO_POWER_UP,
NULL, 0, // Input buffer
NULL, 0, // Output buffer
NULL, // Bytes returned
NULL); // Overlapped
if (TRUE!= ret) {
wprintf(L"Failed on DeviceIoControl!\n");
status = FAILURE;
goto ON_EXIT;
}
ON_EXIT:
// close device interface
if (AppState.hDevice && !CloseHandle(AppState.hDevice)) {
wprintf(L"Failed on hDevice CloseHandle!\n");
}
if (AppState.hEvent && !CloseHandle(AppState.hEvent)) {
wprintf(L"Failed on hEvent CloseHandle!\n");
}
return status;
}