Hi, it's me and my problematic USB DLL again. I'm trying to create a DLL for USB interaction. This time i'm bumping into Run Time Check Error 0. This DLL exports abstract interface class for client. I hope this is the final push. Apologies for the lengthy code. I'm posting all of it since i assume you need the get whole picture:
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h> //Definitions for various common and not so common types like DWORD, PCHAR, HANDLE, etc.
#include <Dbt.h> //Need this for definitions of WM_DEVICECHANGE messages
#include <setupapi.h>
#include <string>
#include "AbstrClass.h"
using namespace std;
#define Cihaz_ID "Vid_a0a1&Pid_4147"
#define __YAZI__ "Esetron USB Kart V1.0"
typedef GUID* LPGUID;
typedef HDEVINFO ( *SetupDiGetClassDevsUMPTR)(LPGUID, PCTSTR, HWND, DWORD);
typedef WINSETUPAPI BOOL ( *SetupDiEnumDeviceInterfacesUMPTR)(HDEVINFO, PSP_DEVINFO_DATA, LPGUID, DWORD, PSP_DEVICE_INTERFACE_DATA);
typedef WINSETUPAPI BOOL ( *SetupDiDestroyDeviceInfoListUMPTR)(HDEVINFO);
typedef WINSETUPAPI BOOL ( *SetupDiEnumDeviceInfoUMPTR)(HDEVINFO, DWORD, PSP_DEVINFO_DATA);
typedef WINSETUPAPI BOOL ( *SetupDiGetDeviceRegistryPropertyUMPTR)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, PDWORD, PBYTE, DWORD, PDWORD);
typedef WINSETUPAPI BOOL ( *SetupDiSetDeviceRegistryPropertyUMPTR)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, const BYTE*, DWORD);
typedef BOOL ( *SetupDiGetDeviceInterfaceDetailUMPTR)(HDEVINFO, PSP_DEVICE_INTERFACE_DATA, PSP_DEVICE_INTERFACE_DETAIL_DATA, DWORD, PDWORD, PSP_DEVINFO_DATA);
typedef HDEVNOTIFY ( *RegisterDeviceNotificationUMPTR)(HANDLE, LPVOID, DWORD);
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;
unsigned char LED_durum = 0;
HANDLE EP1INHandle = INVALID_HANDLE_VALUE;
HANDLE EP1OUTHandle = INVALID_HANDLE_VALUE;
bool durum = false;
unsigned char gond_paket[65]; unsigned char gelen_paket[65];
DWORD Gond_say;
DWORD Gelen_say;
class Bikedll : public AbstrClass
{
public:
SetupDiGetClassDevsUMPTR _SetupDiGetClassDevsUMPTR;
SetupDiEnumDeviceInterfacesUMPTR _SetupDiEnumDeviceInterfacesUMPTR;
SetupDiDestroyDeviceInfoListUMPTR _SetupDiDestroyDeviceInfoListUMPTR;
SetupDiEnumDeviceInfoUMPTR _SetupDiEnumDeviceInfoUMPTR;
SetupDiGetDeviceRegistryPropertyUMPTR _SetupDiGetDeviceRegistryPropertyUMPTR;
SetupDiSetDeviceRegistryPropertyUMPTR _SetupDiSetDeviceRegistryPropertyUMPTR;
SetupDiGetDeviceInterfaceDetailUMPTR _SetupDiGetDeviceInterfaceDetailUMPTR;
RegisterDeviceNotificationUMPTR _RegisterDeviceNotificationUMPTR;
DWORD ErrorStatusWrite;
DWORD ErrorStatusRead;
HINSTANCE hSetUpApi;
HINSTANCE hUser32;
string get_POT();
string get_RPM();
Bikedll();
BOOL CheckIfPresentAndGetUSBDevicePath(void)
{
HDEVINFO DeviceInfoTable = INVALID_HANDLE_VALUE;
PSP_DEVICE_INTERFACE_DATA InterfaceDataStructure = new SP_DEVICE_INTERFACE_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;
BOOL BoolStatus = FALSE;
DWORD LoopCounter = 0;
string DeviceIDToFind = Cihaz_ID;
DeviceInfoTable = _SetupDiGetClassDevsUMPTR(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
while(true)
{
InterfaceDataStructure->cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
if(_SetupDiEnumDeviceInterfacesUMPTR(DeviceInfoTable, NULL, &InterfaceClassGuid, InterfaceIndex, InterfaceDataStructure)) {
ErrorStatus = GetLastError();
if(ERROR_NO_MORE_ITEMS == GetLastError()) { _SetupDiDestroyDeviceInfoListUMPTR(DeviceInfoTable); return FALSE;
}
}
else {
ErrorStatus = GetLastError();
_SetupDiDestroyDeviceInfoListUMPTR(DeviceInfoTable); return FALSE;
}
DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
_SetupDiEnumDeviceInfoUMPTR(DeviceInfoTable, InterfaceIndex, &DevInfoData);
_SetupDiGetDeviceRegistryPropertyUMPTR(DeviceInfoTable, &DevInfoData, SPDRP_HARDWAREID, &dwRegType, NULL, 0, &dwRegSize);
PropertyValueBuffer = (BYTE *) malloc (dwRegSize);
if(PropertyValueBuffer == NULL) { _SetupDiDestroyDeviceInfoListUMPTR(DeviceInfoTable); return FALSE;
}
_SetupDiGetDeviceRegistryPropertyUMPTR(DeviceInfoTable, &DevInfoData, SPDRP_HARDWAREID, &dwRegType, PropertyValueBuffer, dwRegSize, NULL);
string* DeviceIDFromRegistry = new string((char *)PropertyValueBuffer);
free(PropertyValueBuffer);
if (std::string::npos != DeviceIDFromRegistry->find(DeviceIDToFind))
{
MatchFound = true;
}
if(MatchFound == true)
{
DetailedInterfaceDataStructure->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
_SetupDiGetDeviceInterfaceDetailUMPTR(DeviceInfoTable, InterfaceDataStructure, NULL, NULL, &StructureSize, NULL);
DetailedInterfaceDataStructure = (PSP_DEVICE_INTERFACE_DETAIL_DATA)(malloc(StructureSize)); if(DetailedInterfaceDataStructure == NULL) { _SetupDiDestroyDeviceInfoListUMPTR(DeviceInfoTable); return FALSE;
}
DetailedInterfaceDataStructure->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
_SetupDiGetDeviceInterfaceDetailUMPTR(DeviceInfoTable, InterfaceDataStructure, DetailedInterfaceDataStructure, StructureSize, NULL, NULL);
BYTE *pbuf = new BYTE[sizeof(TEXT(__YAZI__))];
pbuf = (BYTE*)TEXT(__YAZI__);
_SetupDiSetDeviceRegistryPropertyUMPTR(DeviceInfoTable, &DevInfoData, SPDRP_FRIENDLYNAME , pbuf, sizeof(TEXT(__YAZI__)));
_SetupDiDestroyDeviceInfoListUMPTR(DeviceInfoTable); return TRUE;
}
InterfaceIndex++;
LoopCounter++;
if(LoopCounter == 10000000) {
return FALSE;
}
}
}
protected:
~Bikedll()
{
if(AttachedState == TRUE)
{
CloseHandle(WriteHandleToUSBDevice);
CloseHandle(ReadHandleToUSBDevice);
}
}
};
string Bikedll::get_RPM(){
gond_paket[0] = 0;
gond_paket[1] = 160;
gond_paket[2] = 0;
gond_paket[3] = 0;
string str;
for(unsigned char i = 4; i <65; i++)
gond_paket[i] = 0xFF;
WriteFile(WriteHandleToUSBDevice, &gond_paket, 65, &Gond_say, 0);
if (ReadFile(ReadHandleToUSBDevice,&gelen_paket,65,&Gelen_say,0))
{
if (gelen_paket[1] == 0xBA)
{
return str = gelen_paket[3];
}
}
}
string Bikedll::get_POT(){
gond_paket[0] = 0;
gond_paket[1] = 160;
gond_paket[2] = 0;
gond_paket[3] = 0;
string str;
for(unsigned char i = 4; i <65; i++)
gond_paket[i] = 0xFF;
WriteFile(WriteHandleToUSBDevice, &gond_paket, 65, &Gond_say, 0);
if (ReadFile(ReadHandleToUSBDevice,&gelen_paket,65,&Gelen_say,0))
{
if (gelen_paket[1] == 0xBA)
{
return str = gelen_paket[5];
}
}
}
Bikedll::Bikedll(){
hSetUpApi = LoadLibrary("setupapi.dll");
hUser32 = LoadLibrary("user32.dll");
_SetupDiGetClassDevsUMPTR = (SetupDiGetClassDevsUMPTR)GetProcAddress(hSetUpApi, "SetupDiGetClassDevsA");
_SetupDiEnumDeviceInterfacesUMPTR = (SetupDiEnumDeviceInterfacesUMPTR)GetProcAddress(hSetUpApi, "SetupDiEnumDeviceInterfaces");
_SetupDiDestroyDeviceInfoListUMPTR = (SetupDiDestroyDeviceInfoListUMPTR)GetProcAddress(hSetUpApi, "SetupDiDestroyDeviceInfoList");
_SetupDiEnumDeviceInfoUMPTR = (SetupDiEnumDeviceInfoUMPTR)GetProcAddress(hSetUpApi, "SetupDiEnumDeviceInfo");
_SetupDiGetDeviceRegistryPropertyUMPTR = (SetupDiGetDeviceRegistryPropertyUMPTR)GetProcAddress(hSetUpApi, "SetupDiGetDeviceRegistryPropertyA");
_SetupDiSetDeviceRegistryPropertyUMPTR = (SetupDiSetDeviceRegistryPropertyUMPTR)GetProcAddress(hSetUpApi, "SetupDiSetDeviceRegistryPropertyA");
_SetupDiGetDeviceInterfaceDetailUMPTR = (SetupDiGetDeviceInterfaceDetailUMPTR)GetProcAddress(hSetUpApi, "SetupDiGetDeviceInterfaceDetailA");
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;
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;
}
}
class AbstrClass{
public:
virtual std::string get_POT() = 0;
virtual std::string get_RPM() = 0;
};
#include "stdafx.h"
#include "Bikedll.h"
extern "C" __declspec(dllexport) AbstrClass* __cdecl create_AbstrClass()
{
return new Bikedll;
}
#include "stdafx.h"
#include "AbstrClass.h"
#include <Windows.h>
#include <iostream>
using namespace std;
typedef AbstrClass* (__cdecl *abstrClass_factory)();
int main()
{
HINSTANCE dll_handle = ::LoadLibrary(TEXT("Bikedll.dll"));
if(!dll_handle){
cerr << "Installation failed!";
return 1;
}
abstrClass_factory factory_func = reinterpret_cast<abstrClass_factory>(::GetProcAddress(dll_handle, "create_AbstrClass"));
if (!factory_func) {
cerr << "function was not imported from dll!\n";
::FreeLibrary(dll_handle);
return 1;
}
AbstrClass* instance = factory_func();
string t = instance->get_POT();
cout << "POT : " << t << endl;
string v = instance->get_RPM();
cout << "RPM : " << v << endl;
return 0;
}