1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
//Includes
#include "stdafx.h"
#include <Windows.h>
#include <setupapi.h>
#include <malloc.h>
#define MY_DEVICE_ID "Vid_04d8&Pid_0040"
using namespace System;
//fonction principale de la DLL
BOOL APIENTRY DLLMain (HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
// déclaration de la fonction connect
extern "C" __declspec(dllexport) void __cdecl connect(HANDLE *WriteH, HANDLE *ReadH, int *conok);
// fonction récupération des handles du périphérique
void __cdecl connect(HANDLE *WriteH, HANDLE *ReadH, int *conok)
{
GUID InterfaceClassGuid = {0x4d1e55b2, 0xf16f, 0x11cf, 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30};
HDEVINFO DeviceInfoTable = INVALID_HANDLE_VALUE;
PSP_DEVICE_INTERFACE_DATA InterfaceDataStructure = new SP_DEVICE_INTERFACE_DATA;
PSP_DEVICE_INTERFACE_DETAIL_DATA DetailedInterfaceDataStructure = new SP_DEVICE_INTERFACE_DETAIL_DATA;
SP_DEVINFO_DATA DevInfoData;
DWORD InterfaceIndex = 0;
DWORD StatusLastError = 0;
DWORD dwRegType;
DWORD dwRegSize;
DWORD StructureSize = 0;
PBYTE PropertyValueBuffer;
bool MatchFound = false;
DWORD ErrorStatus;
HANDLE WriteHandle = INVALID_HANDLE_VALUE; //Need to get a write "handle" to our device before we can write to it.
HANDLE ReadHandle = INVALID_HANDLE_VALUE; //Need to get a read "handle" to our device before we can read from it.
String^ DeviceIDToFind = MY_DEVICE_ID; // MY_DEVICE_ID "Vid_04d8&Pid_003F"
DeviceInfoTable = SetupDiGetClassDevs(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
while(true)
{
InterfaceDataStructure->cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
SetupDiEnumDeviceInterfaces(DeviceInfoTable, NULL, &InterfaceClassGuid, InterfaceIndex, InterfaceDataStructure);
ErrorStatus = GetLastError();
if(ERROR_NO_MORE_ITEMS == GetLastError())
{
SetupDiDestroyDeviceInfoList(DeviceInfoTable);
return;
}
DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
SetupDiEnumDeviceInfo(DeviceInfoTable, InterfaceIndex, &DevInfoData);
SetupDiGetDeviceRegistryProperty(DeviceInfoTable, &DevInfoData, SPDRP_HARDWAREID, &dwRegType, NULL, 0, &dwRegSize);
PropertyValueBuffer = (BYTE *) malloc (dwRegSize);
if(PropertyValueBuffer == NULL)
{
SetupDiDestroyDeviceInfoList(DeviceInfoTable);
return;
}
SetupDiGetDeviceRegistryProperty(DeviceInfoTable, &DevInfoData, SPDRP_HARDWAREID, &dwRegType, PropertyValueBuffer, dwRegSize, NULL);
#ifdef UNICODE
String^ DeviceIDFromRegistry = gcnew String((wchar_t *)PropertyValueBuffer);
#else
String^ DeviceIDFromRegistry = gcnew String((char *)PropertyValueBuffer);
#endif
DeviceIDFromRegistry = DeviceIDFromRegistry->ToLowerInvariant();
DeviceIDToFind = DeviceIDToFind->ToLowerInvariant();
MatchFound = DeviceIDFromRegistry->Contains(DeviceIDToFind);
if(MatchFound == true)
{
DetailedInterfaceDataStructure->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(DeviceInfoTable, InterfaceDataStructure, NULL, NULL, &StructureSize, NULL);
DetailedInterfaceDataStructure = (PSP_DEVICE_INTERFACE_DETAIL_DATA)(malloc(StructureSize));
if(DetailedInterfaceDataStructure == NULL)
{
SetupDiDestroyDeviceInfoList(DeviceInfoTable);
return;
}
DetailedInterfaceDataStructure->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(DeviceInfoTable, InterfaceDataStructure, DetailedInterfaceDataStructure, StructureSize, NULL, NULL);
WriteHandle = CreateFile((DetailedInterfaceDataStructure->DevicePath), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
ReadHandle = CreateFile((DetailedInterfaceDataStructure->DevicePath), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
ErrorStatus = GetLastError();
if(ErrorStatus == ERROR_SUCCESS)
{
*WriteH = WriteHandle;
*ReadH = ReadHandle;
*conok = 1;
}
else
{
*WriteH = INVALID_HANDLE_VALUE;
*ReadH = INVALID_HANDLE_VALUE;
*conok = 0;
}
SetupDiDestroyDeviceInfoList(DeviceInfoTable);
return;
}//fin du if (matchfound == true)
InterfaceIndex++;
}//end of while(true)
return;
}// end connect |
Partager