CODE
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <conio.h>
using namespace std;
#define DIGCF_PRESENT 0x00000002
//-------Hid.dll functions prototypes-------
//HidD_Hello is a pointer that points at the function which accepts two parameters (PCHAR and ULONG)
//and returns result of ULONG type
typedef ULONG (WINAPI *PHidD_Hello)(PCHAR, ULONG);
typedef void (WINAPI *PHidD_GetHidGuid)(LPGUID);
PHidD_Hello HidD_Hello;
PHidD_GetHidGuid HidD_GetHidGuid;
//-------SetupApi.dll functions prototypes--------
typedef PVOID HDEVINFO;
typedef HDEVINFO (WINAPI *PSetupDiGetClassDevs)(const GUID*, PCTSTR, HWND, DWORD);
PSetupDiGetClassDevs SetupDiGetClassDevs;
void main(int argc, char *argv[])
{
//Load hid.dll
HMODULE HidDllDescriptor=LoadLibrary((LPCTSTR)"hid.dll");
if (HidDllDescriptor==NULL)
{
cout << "Error #" << GetLastError() << ". Can't load hid.dll." << endl;
}
else
{
cout << "hid.dll have been loaded successfully" << endl;
char HidDllAdress[50];
if (GetModuleFileName(HidDllDescriptor,(LPSTR)HidDllAdress,50)==0)
{
cout << "Can't get full path to hid.dll. Error # " << GetLastError() << endl;
}
else
{
cout << "hid.dll adress is " << HidDllAdress <<endl;
//Get entry points of HID.dll functions
HidD_Hello=(PHidD_Hello)GetProcAddress((HMODULE)HidDllDescriptor, "HidD_Hello");
HidD_GetHidGuid=(PHidD_GetHidGuid)GetProcAddress((HMODULE)HidDllDescriptor, "HidD_GetHidGuid");
}
}
//Load setupapi.dll
HMODULE SetupApiDllDescriptor=LoadLibrary((LPCSTR)"setupapi.dll");
if (SetupApiDllDescriptor==NULL)
{
cout << "Can't load setupapi.dll. Error # " << GetLastError() << endl;
}
else
{
cout << "setupapi.dll have been loaded successfully" << endl;
char SetupApiDllAdress[50];
if (GetModuleFileName(SetupApiDllDescriptor,(LPSTR)SetupApiDllAdress,50)==0)
{
cout << "Can't get full path to setupapi.dll. Error # " << GetLastError << endl;
}
else
{
cout << "Setupapi.dll adress is " << SetupApiDllAdress << endl;
//Get entry points of setupapi.dll functions
SetupDiGetClassDevs=(PSetupDiGetClassDevs)GetProcAddress((HMODULE)SetupApiDllDes
criptor,"SetupDiGetClassDevs");
}
}
GUID HidGuid;
HidD_GetHidGuid(&HidGuid);
typedef struct _HIDD_ATTRIBUTES
{
ULONG Size; // = sizeof (struct _HIDD_ATTRIBUTES)
USHORT VendorID;
USHORT ProductID;
USHORT VersionNumber;
} HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES;
HDEVINFO hHidInfo;
hHidInfo=SetupDiGetClassDevs(&HidGuid,NULL,NULL,DIGCF_PRESENT); //Здесь появляется ошибка!
if (hHidInfo==INVALID_HANDLE_VALUE)
{
cout << "Can't build a list of HIDs. Error " << GetLastError << endl;
}
else
{
cout << "List of HIDs have been built" << endl;
}
//Unload hid and setupapi libraries
FreeLibrary(HidDllDescriptor);
FreeLibrary(SetupApiDllDescriptor);
cin.get();
};
#include <stdio.h>
#include <iostream>
#include <conio.h>
using namespace std;
#define DIGCF_PRESENT 0x00000002
//-------Hid.dll functions prototypes-------
//HidD_Hello is a pointer that points at the function which accepts two parameters (PCHAR and ULONG)
//and returns result of ULONG type
typedef ULONG (WINAPI *PHidD_Hello)(PCHAR, ULONG);
typedef void (WINAPI *PHidD_GetHidGuid)(LPGUID);
PHidD_Hello HidD_Hello;
PHidD_GetHidGuid HidD_GetHidGuid;
//-------SetupApi.dll functions prototypes--------
typedef PVOID HDEVINFO;
typedef HDEVINFO (WINAPI *PSetupDiGetClassDevs)(const GUID*, PCTSTR, HWND, DWORD);
PSetupDiGetClassDevs SetupDiGetClassDevs;
void main(int argc, char *argv[])
{
//Load hid.dll
HMODULE HidDllDescriptor=LoadLibrary((LPCTSTR)"hid.dll");
if (HidDllDescriptor==NULL)
{
cout << "Error #" << GetLastError() << ". Can't load hid.dll." << endl;
}
else
{
cout << "hid.dll have been loaded successfully" << endl;
char HidDllAdress[50];
if (GetModuleFileName(HidDllDescriptor,(LPSTR)HidDllAdress,50)==0)
{
cout << "Can't get full path to hid.dll. Error # " << GetLastError() << endl;
}
else
{
cout << "hid.dll adress is " << HidDllAdress <<endl;
//Get entry points of HID.dll functions
HidD_Hello=(PHidD_Hello)GetProcAddress((HMODULE)HidDllDescriptor, "HidD_Hello");
HidD_GetHidGuid=(PHidD_GetHidGuid)GetProcAddress((HMODULE)HidDllDescriptor, "HidD_GetHidGuid");
}
}
//Load setupapi.dll
HMODULE SetupApiDllDescriptor=LoadLibrary((LPCSTR)"setupapi.dll");
if (SetupApiDllDescriptor==NULL)
{
cout << "Can't load setupapi.dll. Error # " << GetLastError() << endl;
}
else
{
cout << "setupapi.dll have been loaded successfully" << endl;
char SetupApiDllAdress[50];
if (GetModuleFileName(SetupApiDllDescriptor,(LPSTR)SetupApiDllAdress,50)==0)
{
cout << "Can't get full path to setupapi.dll. Error # " << GetLastError << endl;
}
else
{
cout << "Setupapi.dll adress is " << SetupApiDllAdress << endl;
//Get entry points of setupapi.dll functions
SetupDiGetClassDevs=(PSetupDiGetClassDevs)GetProcAddress((HMODULE)SetupApiDllDes
criptor,"SetupDiGetClassDevs");
}
}
GUID HidGuid;
HidD_GetHidGuid(&HidGuid);
typedef struct _HIDD_ATTRIBUTES
{
ULONG Size; // = sizeof (struct _HIDD_ATTRIBUTES)
USHORT VendorID;
USHORT ProductID;
USHORT VersionNumber;
} HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES;
HDEVINFO hHidInfo;
hHidInfo=SetupDiGetClassDevs(&HidGuid,NULL,NULL,DIGCF_PRESENT); //Здесь появляется ошибка!
if (hHidInfo==INVALID_HANDLE_VALUE)
{
cout << "Can't build a list of HIDs. Error " << GetLastError << endl;
}
else
{
cout << "List of HIDs have been built" << endl;
}
//Unload hid and setupapi libraries
FreeLibrary(HidDllDescriptor);
FreeLibrary(SetupApiDllDescriptor);
cin.get();
};
Компилируется без ошибок и предупреждений, но при запуске выдает ошибку:
Необработанное исключение в "0x00000000" в "hid1.exe": 0xC0000005: Нарушение прав доступа при чтении "0x00000000".
Погуглив, я узнал, что это связано с разыменованием пустого указателя PVOID, каким по-сути является hHidInfo. Замена куска кода
CODE
HDEVINFO hHidInfo;
hHidInfo=SetupDiGetClassDevs(&HidGuid,NULL,NULL,DIGCF_PRESENT);
hHidInfo=SetupDiGetClassDevs(&HidGuid,NULL,NULL,DIGCF_PRESENT);
на
CODE
DWORD *hHidInfo;
hHidInfo=(DWORD *)SetupDiGetClassDevs(&HidGuid,NULL,NULL,DIGCF_PRESENT);
hHidInfo=(DWORD *)SetupDiGetClassDevs(&HidGuid,NULL,NULL,DIGCF_PRESENT);
не помогла решить проблему. Как и в случае замены HDEVINFO на char *, word *, void *.
Помогите, пожалуйста, разобраться с этой ошибкой.