#pragma once //Includes #include <Windows.h> #include <Dbt.h> #include <setupapi.h> #include #include "Form2.h" #include "Menu_principal.h" namespace HIDPnPDemo { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; #pragma region DLL Imports using namespace System::Threading; using namespace System::Runtime::InteropServices; #ifdef UNICODE #define Seeifdef Unicode #else #define Seeifdef Ansi #endif [DllImport("setupapi.dll" , CharSet = CharSet::Seeifdef, EntryPoint="SetupDiGetClassDevs", CallingConvention=CallingConvention::Winapi)] extern "C" HDEVINFO SetupDiGetClassDevsUM( LPGUID ClassGuid, PCTSTR Enumerator, HWND hwndParent, DWORD Flags); [DllImport("setupapi.dll" , CharSet = CharSet::Seeifdef, EntryPoint="SetupDiEnumDeviceInterfaces", CallingConvention=CallingConvention::Winapi)] extern "C" WINSETUPAPI BOOL WINAPI SetupDiEnumDeviceInterfacesUM( HDEVINFO DeviceInfoSet, //Input: Give it the HDEVINFO we got from SetupDiGetClassDevs() PSP_DEVINFO_DATA DeviceInfoData, //Input (optional) LPGUID InterfaceClassGuid, //Input DWORD MemberIndex, //Input: "Index" of the device you are interested in getting the path for. PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData);//Output: This function fills in an "SP_DEVICE_INTERFACE_DATA" structure. [DllImport("setupapi.dll" , CharSet = CharSet::Seeifdef, EntryPoint="SetupDiDestroyDeviceInfoList", CallingConvention=CallingConvention::Winapi)] extern "C" WINSETUPAPI BOOL WINAPI SetupDiDestroyDeviceInfoListUM( HDEVINFO DeviceInfoSet); //Input: Give it a handle to a device info list to deallocate from RAM. [DllImport("setupapi.dll" , CharSet = CharSet::Seeifdef, EntryPoint="SetupDiEnumDeviceInfo", CallingConvention=CallingConvention::Winapi)] extern "C" WINSETUPAPI BOOL WINAPI SetupDiEnumDeviceInfoUM( HDEVINFO DeviceInfoSet, DWORD MemberIndex, PSP_DEVINFO_DATA DeviceInfoData); [DllImport("setupapi.dll" , CharSet = CharSet::Seeifdef, EntryPoint="SetupDiGetDeviceRegistryProperty", CallingConvention=CallingConvention::Winapi)] extern "C" WINSETUPAPI BOOL WINAPI SetupDiGetDeviceRegistryPropertyUM( HDEVINFO DeviceInfoSet, PSP_DEVINFO_DATA DeviceInfoData, DWORD Property, PDWORD PropertyRegDataType, PBYTE PropertyBuffer, DWORD PropertyBufferSize, PDWORD RequiredSize); [DllImport("setupapi.dll" , CharSet = CharSet::Seeifdef, EntryPoint="SetupDiGetDeviceInterfaceDetail", CallingConvention=CallingConvention::Winapi)] extern "C" BOOL SetupDiGetDeviceInterfaceDetailUM( HDEVINFO DeviceInfoSet, //Input: Wants HDEVINFO which can be obtained from SetupDiGetClassDevs() PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData, //Input: Pointer to an structure which defines the device interface. PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData, //Output: Pointer to a strucutre, which will contain the device path. DWORD DeviceInterfaceDetailDataSize, //Input: Number of bytes to retrieve. PDWORD RequiredSize, //Output (optional): Te number of bytes needed to hold the entire struct PSP_DEVINFO_DATA DeviceInfoData); [DllImport("user32.dll" , CharSet = CharSet::Seeifdef, EntryPoint="RegisterDeviceNotification", CallingConvention=CallingConvention::Winapi)] extern "C" HDEVNOTIFY WINAPI RegisterDeviceNotificationUM( HANDLE hRecipient, LPVOID NotificationFilter, DWORD Flags); #pragma endregion #pragma region Global Variables GUID InterfaceClassGuid = {0x4d1e55b2, 0xf16f, 0x11cf, 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}; BOOL AttachedState = FALSE; BOOL AttachedButBroken = FALSE; PSP_DEVICE_INTERFACE_DETAIL_DATA DetailedInterfaceDataStructure = new SP_DEVICE_INTERFACE_DETAIL_DATA; HANDLE WriteHandleToUSBDevice = INVALID_HANDLE_VALUE; HANDLE ReadHandleToUSBDevice = INVALID_HANDLE_VALUE; BOOL PushbuttonPressed = FALSE; BOOL ResetCounterRequest= FALSE; BOOL CountdownModeRequest= FALSE; BOOL RelaisToggleRequest= FALSE; unsigned long int ADCValue = 0; unsigned long int TMRValue = 0; #pragma endregion /// <summary> /// Summary for Form1 /// /// </summary> public ref class Form1 : public System::Windows::Forms::Form {private: Form2 ^ form2; public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here form2 = gcnew Form2(); //Additional constructor code DEV_BROADCAST_DEVICEINTERFACE MyDeviceBroadcastHeader; MyDeviceBroadcastHeader.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; MyDeviceBroadcastHeader.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE); MyDeviceBroadcastHeader.dbcc_reserved = 0; MyDeviceBroadcastHeader.dbcc_classguid = InterfaceClassGuid; RegisterDeviceNotificationUM((HANDLE)this->Handle, &MyDeviceBroadcastHeader, DEVICE_NOTIFY_WINDOW_HANDLE); if(CheckIfPresentAndGetUSBDevicePath()) { DWORD ErrorStatusWrite; DWORD ErrorStatusRead; WriteHandleToUSBDevice = CreateFile(DetailedInterfaceDataStructure->DevicePath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0); ErrorStatusWrite = GetLastError(); ReadHandleToUSBDevice = CreateFile(DetailedInterfaceDataStructure->DevicePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0); ErrorStatusRead = GetLastError(); if((ErrorStatusWrite ERROR_SUCCESS) && (ErrorStatusRead ERROR_SUCCESS)) { AttachedState = TRUE; AttachedButBroken = FALSE; } else { AttachedState = FALSE; AttachedButBroken = TRUE; if(ErrorStatusWrite == ERROR_SUCCESS) CloseHandle(WriteHandleToUSBDevice); if(ErrorStatusRead == ERROR_SUCCESS) CloseHandle(ReadHandleToUSBDevice); } } else { AttachedState = FALSE; AttachedButBroken = FALSE; } ReadWriteThread->RunWorkerAsync(); } protected: /// <summary> /// </summary> ~Form1() {
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question