Please see Edit below.
I am attempting to get the class/interface GUID of a connected device such as a TV or projector. I am using the function RegisterDeviceNotification() to give me the GUID when a TV is connected.
My Problem: When the TV is connected I can successfully be notified but when I go to access the GUID of the device(by inspecting the lParam structure) I get an access violation or error.
I cant figure out whats going wrong? Whenever I go to access or use the GUID my program fails, note my program doesn't crash but the output window writes something like First-chance exception at 0x001c1a9d in myProgram.exe... and I cant figure out whats going wrong?
HDEVNOTIFY *hDeviceNotify;
GUID interfaceClassGuid = { 0x25dbce51, 0x6c8f, 0x4a72, 0x8a,0x6d,0xb5,0x4c,0x2b,0x4f,0xc8,0x35 };
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
{
openConsoleWindow();
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = interfaceClassGuid;
*hDeviceNotify = RegisterDeviceNotification( hWnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE );
if (*hDeviceNotify == NULL)
printf("hDeviceNotify = NULL \n");
}
break;
case WM_DEVICECHANGE:
{
PDEV_BROADCAST_DEVICEINTERFACE b = (PDEV_BROADCAST_DEVICEINTERFACE) lParam;
// The following printf NEVER prints out.
// When accessing the below GUID I get an error:
// "First-chance exception at 0x001c1a9d in myProgram.exe: 0xC0000005: Access violation writing location 0x00000000."
printf("GUID: Data1: %x, Data2: %x, Data3: %x, Data4: %s \n", b->dbcc_classguid.Data1,
b->dbcc_classguid.Data2, b->dbcc_classguid.Data3, (char*)b->dbcc_classguid.Data4);
// The following function ALWAYS fails.
// GetLastError() gives the error "The parameter is incorrect"
HDEVINFO hDevInfo = SetupDiGetClassDevs(&b->dbcc_classguid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE) {
//printf("hDevInfo == INVALID_HANDLE_VALUE \n");
outputLastError(_T("hDevInfo == INVALID_HANDLE_VALUE \n"));
}
}
break;
...
Edit: I've applied Hans' advice but still the same issue occurs(printf() never prints and the exception occurs):
case WM_DEVICECHANGE:
{
if (wParam != DBT_DEVNODES_CHANGED) {
printf("1: \n");
break;
}
PDEV_BROADCAST_HDR h = (PDEV_BROADCAST_HDR) lParam;
if (h->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE) {
printf("2\n");
break;
}
PDEV_BROADCAST_DEVICEINTERFACE b = (PDEV_BROADCAST_DEVICEINTERFACE) h;
// When accessing the below GUID I get an error:
// "First-chance exception at 0x001c1a9d in TEST GUID Error.exe: 0xC0000005: Access violation writing location 0x00000000."
printf("GUID: Data1: %x, Data2: %x, Data3: %x, Data4: %s \n", b->dbcc_classguid.Data1,
b->dbcc_classguid.Data2, b->dbcc_classguid.Data3, (char*)b->dbcc_classguid.Data4);
// The following function ALWAYS fails.
// GetLastError() gives the error "The parameter is incorrect"
HDEVINFO hDevInfo = SetupDiGetClassDevs(&b->dbcc_classguid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE) {
//printf("hDevInfo == INVALID_HANDLE_VALUE \n");
outputLastError(_T("hDevInfo == INVALID_HANDLE_VALUE \n"));
}
}
break;
Related
I'm trying to get keyboard messages from another process using injected dll,but I don't know where have to call function in my own program.
here is my injected dll functions :
//this is my dll main function
BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
{
/* open file */
FILE *file;
fopen_s(&file, "d:\\dll\\temp.txt", "a+");
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
hInst = (HINSTANCE)hModule;
// should be function calling be here????
installhook();
break;
case DLL_PROCESS_DETACH:
fprintf(file, "DLL detach function called.\n");
break;
case DLL_THREAD_ATTACH:
fprintf(file, "DLL thread attach function called.\n");
break;
case DLL_THREAD_DETACH:
fprintf(file, "DLL thread detach function called.\n");
break;
}
hInst = (HINSTANCE)hModule;
/* close file */
fclose(file);
return TRUE;
}
here is my install hook function to installing keyboardproc to process
BOOL __declspec(dllexport)__stdcall installhook()
{
HWND targetWnd;
HANDLE hProcess;
unsigned long processID = 0;
hkb = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, hInst, GetCurrentThreadId());
return TRUE;
}
and this is my keyboardproc function body
LRESULT __declspec(dllexport)__stdcall CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
char ch;
MessageBoxA(nullptr, "key touched\n", "DLL_PROCESS_ATTACH", MB_OK | MB_ICONWARNING);
do
{
if (((DWORD)lParam & 0x40000000) && (HC_ACTION == nCode))
{
if ((wParam == VK_SPACE) || (wParam == VK_RETURN) || (wParam >= 0x2f) && (wParam <= 0x100))
{
FILE *file;
fopen_s(&file, "d:\\dll\\temp.txt", "a+");
fprintf(file, nCode + ".\n");
}
}
} while (0);
return CallNextHookEx(hkb, nCode, wParam, lParam);
}
and finally here is my main program where I injected dll to the destination process
int procID = 9448;
HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procID);
if (process == NULL) {
printf("Error: the specified process couldn't be found.\n");
}
/*
* Get address of the LoadLibrary function.
*/
LPVOID addr = (LPVOID)GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryA");
if (addr == NULL) {
printf("Error: the LoadLibraryA function was not found inside kernel32.dll library.\n");
}
/*
* Allocate new memory region inside the process's address space.
*/
LPVOID arg = (LPVOID)VirtualAllocEx(process, NULL, strlen(buffer), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (arg == NULL) {
printf("Error: the memory could not be allocated inside the chosen process.\n");
}
/*
* Write the argument to LoadLibraryA to the process's newly allocated memory region.
*/
int n = WriteProcessMemory(process, arg, buffer, strlen(buffer), NULL);
if (n == 0) {
printf("Error: there was no bytes written to the process's address space.\n");
}
cout << procID << "\nhandle:" << process << "\nAddress:" << addr << "\nVirtualArg:" << arg << "\nWM:"<<n<<"\n";
/*
* Inject our DLL into the process's address space.
*/
HANDLE threadID = CreateRemoteThread(process, NULL, 0, (LPTHREAD_START_ROUTINE)addr, arg, NULL, NULL);
if (threadID == NULL) {
printf("Error: the remote thread could not be created.\n");
}
else {
printf("Success: the remote thread was successfully created.\n");
}
/*
* Close the handle to the process, becuase we've already injected the DLL.
*/
CloseHandle(process);
what is the wrong in my code and where must be change to get desired result!
Yes, it can be called from DLL_PROCESS_ATTACH.
But according to msdn
hMod [in] Type: HINSTANCE A handle to the DLL containing the hook
procedure pointed to by the lpfn parameter. The hMod parameter must be
set to NULL if the dwThreadId parameter specifies a thread created by
the current process and if the hook procedure is within the code
associated with the current process.
So change the hMod to be NULL
hkb = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, NULL, GetCurrentThreadId());
I'm writing a simple and small SNMP manager program with WinSNMP.
But it cannot receive trap events...
Can anyone help me?
Here's sample code:
#include <stdio.h>
#include <Winsnmp.h>
SNMPAPI_STATUS CALLBACK MySnmpCallback(
HSNMP_SESSION hSession,
HWND hWnd,
UINT wMsg,
WPARAM wParam,
LPARAM lParam,
LPVOID lpClientData
)
{
printf("MySnmpCallback!\n");
return SNMPAPI_SUCCESS;
}
void SnmpTest()
{
smiUINT32 nMajorVersion;
smiUINT32 nMinorVersion;
smiUINT32 nLevel;
smiUINT32 nTranslateMode;
smiUINT32 nRetransmitMode;
SNMPAPI_STATUS statusStartup = SnmpStartupEx(
&nMajorVersion,
&nMinorVersion,
&nLevel,
&nTranslateMode,
&nRetransmitMode
);
if (SNMPAPI_SUCCESS == statusStartup)
{
printf(" MajorVersion = %u\n", nMajorVersion);
printf(" MinorVersion = %u\n", nMinorVersion);
printf(" Level = %u\n", nLevel);
printf("RetransmitMode = %u\n", nRetransmitMode);
SnmpSetTranslateMode(SNMPAPI_UNTRANSLATED_V2);
SnmpGetTranslateMode(&nTranslateMode);
printf(" TranslateMode = %u\n", nTranslateMode);
}
else
{
printf("SnmpStartup Failed. (%u)\n", SnmpGetLastError(NULL));
}
if (SNMPAPI_SUCCESS == statusStartup)
{
HSNMP_SESSION hSession = SnmpCreateSession(NULL, NULL, (SNMPAPI_CALLBACK)&MySnmpCallback, NULL);
if (SNMPAPI_FAILURE != hSession)
{
HSNMP_ENTITY localEntity = SnmpStrToEntity(hSession, "0.0.0.0");
SNMPAPI_STATUS regStatus = SnmpRegister(hSession, localEntity, NULL, NULL, NULL, SNMPAPI_ON);
if (SNMPAPI_SUCCESS == regStatus)
{
while (TRUE)
{
Sleep(100);
if (GetKeyState('A') && 0x8000)
break;
}
SnmpClose(hSession);
}
else
{
printf("SnmpRegister Failed. (%u)\n", SnmpGetLastError(hSession));
}
}
else
{
printf("SnmpCreateSession Failed. (%u)\n", SnmpGetLastError(NULL));
}
}
if (SNMPAPI_SUCCESS == statusStartup)
{
SnmpCleanup();
}
}
int _tmain(int argc, _TCHAR* argv[])
{
SnmpTest();
return 0;
}
I've tried other approach that uses the window notification.
But it didn't work.
I tried to verify this code with PowerShell event.
Opening evntwin, and adding PowerShell event.(ID:400/401/402/403)
To bring about trap event, I launched powershell and quit.
SNMP Service and Trap Sevice are runnning.
And the loop process in the above code is running.
But the sample program cannot receive any traps.
* Other manager such as Snmpb can receive traps.
I am attempting to get information(location info, location path, etc.) about a device that is currently connected to the computer in C++ Win32. I know how to get this information by using the function SetupDiGetDeviceRegistryProperty()
Before I use the function SetupDiGetDeviceRegistryProperty(), I must first call SetupDiGetSelectedDevice() because I need to pass a SP_DEVINFO_DATA as a parameter inside SetupDiGetDeviceRegistryProperty(). Is this correct?
My Problem: I can never get the device using the function SetupDiGetSelectedDevice(). When I call that function it always fails, ie, returns FALSE. GetLastError() returns the code e0000211 which I am not sure what that means.
Whats going wrong with my following code? If I am using the wrong function to get a device then what function do I use to get a device?
INT_PTR WINAPI WinProcCallback( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch (message)
{
case WM_DEVICECHANGE:
{
TCHAR strBuff[256];
PDEV_BROADCAST_HDR h = (PDEV_BROADCAST_HDR) lParam;
if (h->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE) {
printf("h->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE\n");
break;
}
switch (wParam)
{
case DBT_DEVICEARRIVAL:
{
DWORD dataT = 0;
SP_DEVINFO_DATA deviceInfoData = {0};
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
deviceInfoData.ClassGuid = h->dbcc_classguid;
// The following function always works and is successful
HDEVINFO hDevInfo = SetupDiGetClassDevs(&h->dbcc_classguid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE) {
printf("hDevInfo == INVALID_HANDLE_VALUE\n");
break;
}
// ERROR OCCURS HERE: The following function ALWAYS returns false: whats going wrong?
if (SetupDiGetSelectedDevice(hDevInfo, &deviceInfoData) == FALSE) {
printf("SetupDiGetSelectedDevice(hDevInfo, &deviceInfoData) == FALSE\n");
break;
}
// Get device location information
DWORD buffersize = 0;
LPTSTR buffer = NULL;
while (!SetupDiGetDeviceRegistryProperty(hDevInfo, &deviceInfoData, SPDRP_LOCATION_INFORMATION, &dataT,
(PBYTE)buffer, buffersize, &buffersize))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Change the buffer size.
if (buffer)
LocalFree(buffer);
buffer = (LPTSTR)LocalAlloc(LPTR, buffersize);
}
}
printf("Data: %d: %s\n", i, buffer);
}
break;
I am attempting to list the device information for all the monitors currently connected to the computer. I have a function that can do this and its 90% done except when I go to call the function SetupDiGetClassDevs() with the 2nd parameter set(not NULL) then the function always fails(returns INVALID_HANDLE_VALUE).
When I call GetLastError() I get the error 13(decimal), ie, "The data is invalid" which I am not sure what that means?
What is going wrong? Can you provide any advice on whats happening and how I can fix it?
Function Information:
HDEVINFO SetupDiGetClassDevs(
_In_opt_ const GUID *ClassGuid,
_In_opt_ PCTSTR Enumerator, // According to MSDN this param MUST be set if I want Device Information for a specific class(Monitors)
_In_opt_ HWND hwndParent,
_In_ DWORD Flags
);
My function that attempts to get a Device Information Set for Monitors only and output each monitors details(the error line is commented):
void printDeviceData(GUID guID)
{
// Device Classes: http://msdn.microsoft.com/en-us/library/windows/hardware/ff553426
// System Device Classes: http://msdn.microsoft.com/en-us/library/windows/hardware/ff553428
// Monitor Class GUI: {4d36e96e-e325-11ce-bfc1-08002be10318}
DWORD dataT = 0;
PCTSTR monitorGuID = _T("");
SP_DEVINFO_DATA deviceInfoData = {0};
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
deviceInfoData.ClassGuid = guID;
// Step 1: Get Device Information Set for Monitors only
// ERROR OCCURS HERE: SetupDiGetClassDevs() always fails
// Also tried these values for param 2: "Monitor" "PCI" but all cause the function to return INVALID_HANDLE_VALUE
HDEVINFO hDevInfo = SetupDiGetClassDevs(&guID, _T("{4d36e96e-e325-11ce-bfc1-08002be10318}"), NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE) {
//outputLastError(_T("Fail 1"));
printf("hDevInfo == INVALID_HANDLE_VALUE\n");
return;
}
else printf("SUCCESS 1\n");
if (SetupDiGetSelectedDevice(hDevInfo, &deviceInfoData) == FALSE) {
//outputLastError(_T("SetupDiGetSelectedDevice(hDevInfo, &deviceInfoData) == FALSE"));
printf("SetupDiGetSelectedDevice(hDevInfo, &deviceInfoData) == FALSE, %d, %x\n", GetLastError(), GetLastError());
return;
}
else printf("SUCCESS 2\n");
// Step 2: For each Monitor: Output Device information
const unsigned int FLAG_NUM = 30;
DWORD flags[] = {SPDRP_FRIENDLYNAME, SPDRP_ENUMERATOR_NAME, SPDRP_PHYSICAL_DEVICE_OBJECT_NAME, SPDRP_DEVICEDESC,
SPDRP_ADDRESS, SPDRP_BUSNUMBER, SPDRP_BUSTYPEGUID, SPDRP_CHARACTERISTICS, SPDRP_CLASS, SPDRP_CLASSGUID,
SPDRP_COMPATIBLEIDS, SPDRP_CONFIGFLAGS, SPDRP_DEVICE_POWER_DATA, SPDRP_DEVTYPE, SPDRP_DRIVER,
SPDRP_ENUMERATOR_NAME, SPDRP_EXCLUSIVE, SPDRP_HARDWAREID, SPDRP_INSTALL_STATE, SPDRP_LEGACYBUSTYPE,
SPDRP_LOCATION_INFORMATION, SPDRP_LOCATION_PATHS, SPDRP_LOWERFILTERS, SPDRP_MFG,
SPDRP_PHYSICAL_DEVICE_OBJECT_NAME, SPDRP_UI_NUMBER, SPDRP_UI_NUMBER_DESC_FORMAT, SPDRP_UPPERFILTERS,
SPDRP_SECURITY_SDS, SPDRP_SECURITY, SPDRP_SERVICE };
for (int i=0; i<=FLAG_NUM; i++) {
DWORD buffersize = 0;
LPTSTR buffer = NULL;
while (!SetupDiGetDeviceRegistryProperty(hDevInfo, &deviceInfoData, flags[i], &dataT,
(PBYTE)buffer, buffersize, &buffersize))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Change the buffer size.
if (buffer)
LocalFree(buffer);
buffer = (LPTSTR)LocalAlloc(LPTR, buffersize);
}
else {
// Insert error handling here.
break;
}
}
printf("Data: %d: %s\n", i, buffer);
if (buffer)
LocalFree(buffer);
}
SetupDiDestroyDeviceInfoList(hDevInfo);
}
According to the documentation, Enumerator must be set to a valid device Instance ID which according to http://msdn.microsoft.com/en-us/library/windows/hardware/ff541327 has to be specified like this
"PCI\VEN_1000&DEV_0001&SUBSYS_00000000&REV_02\1&08"
I haven't tested it, but I'd assume that's where the invalid data come from.
My application creates a window for the purpose of handling the WM_DEVICECHANGE Windows message. WndProc does get called several times, until my application calls a function to poll for keyboard events, but for whatever reason it does not get called when I remove or insert my USB device.
This is the GUID for my USB device. I'm sure it's correct:
static const GUID _guidForCP210xDevices = {
0xA2A39220, 0x39F4, 0x4B88, 0xAE, 0xCB, 0x3D, 0x86, 0xA3, 0x5D, 0xC7, 0x48
};
This is how my window is created:
m_hInstance = ::GetModuleHandle( NULL );
if ( m_hInstance == NULL )
{
TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to retrieve the module handle.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
THROW(::GetLastError());
}
m_wcx.cbSize = sizeof(WNDCLASSEX); // size of structure
m_wcx.style = CS_HREDRAW | CS_VREDRAW; // initially minimized
m_wcx.lpfnWndProc = &WndProc; // points to window procedure
m_wcx.cbClsExtra = 0; // no extra class memory
m_wcx.cbWndExtra = 0; // no extra window memory
m_wcx.hInstance = m_hInstance; // handle to instance
m_wcx.hIcon = ::LoadIcon( NULL, IDI_APPLICATION ); // default app icon
m_wcx.hCursor = ::LoadCursor( NULL, IDC_ARROW ); // standard arrow cursor
m_wcx.hbrBackground = NULL; // no background to paint
m_wcx.lpszMenuName = NULL; // no menu resource
m_wcx.lpszClassName = _pwcWindowClass; // name of window class
m_wcx.hIconSm = NULL; // search system resources for sm icon
m_atom = ::RegisterClassEx( &m_wcx );
if ( m_atom == 0 )
{
TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to register window class.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
THROW(::GetLastError());
}
m_hWnd = ::CreateWindow(
_pwcWindowClass,
_pwcWindowName,
WS_ICONIC,
0,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
m_hInstance,
NULL
);
if ( m_hWnd == NULL )
{
TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to create window.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
THROW(::GetLastError());
}
::ShowWindow( m_hWnd, SW_HIDE ); // function does not fail
if ( RegisterForNotification() != ERROR_SUCCESS )
{
TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to register for device notification.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
THROW(::GetLastError());
}
This is how I register for device notification:
static DEV_BROADCAST_DEVICEINTERFACE dbt = {0};
ASSERT(m_hWnd != NULL);
// Populate DEV_BROADCAST_DEVICEINTERFACE structure.
dbt.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
dbt.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
dbt.dbcc_classguid = _guidForCP210xDevices;
// Register for HID devic notifications
m_hNotify = RegisterDeviceNotification( m_hWnd, &dbt, DEVICE_NOTIFY_WINDOW_HANDLE );
if ( m_hNotify == NULL )
{
TRACE(_T("CNotifyWindow::RegisterForNotification : Failed to register for device notification.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
return ::GetLastError();
}
return ERROR_SUCCESS;
My WndProc function looks like this:
static LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
DEV_BROADCAST_HDR * pHeader = reinterpret_cast<DEV_BROADCAST_HDR *>(lParam);
switch ( uMsg )
{
case WM_DEVICECHANGE:
if ( pHeader != NULL )
{
if ( pHeader->dbch_devicetype == DBT_DEVTYP_PORT )
{
OnDeviceChange( wParam );
}
}
break;
default:
// Do nothing.
break;
}
return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
}
Does anyone know what I'm doing wrong? Thanks.
You're missing a message pump to retrieve the notifications from the queue and dispatch them to your WndProc. The message pump is effectively a loop that checks for messages and calls the appropriate WndProc synchronously. MSDN has some good information on them. I don't know what the context of your code is, so I'm not sure if you just need to insert a pump after RegisterForNotification or if a larger architectural change is necessary.