Hello. My problem is that i try to export a class from my DLL using an abstract interface. My DLL compiles just fine but despite adding project references from Properties in VS 2010 or including additional directories, i can't seem to open my header. They are under same folder by the way. Here is the code:
#include <string>
class AtaClass{
public:
virtual std::string get_POT() = 0;
virtual std::string get_RPM() = 0;
};
#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 "AtaClass.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);
typedef DWORD (*MPUSBGetDLLVersionPTR)(void);
typedef DWORD (*MPUSBGetDeviceCountPTR)(PCHAR);
typedef HANDLE (*MPUSBOpenPTR)(DWORD, PCHAR, PCHAR, DWORD, DWORD);
typedef BOOL (*MPUSBClosePTR)(HANDLE);
typedef DWORD (*MPUSBReadPTR)(HANDLE, PVOID, DWORD, PDWORD, DWORD);
typedef DWORD (*MPUSBWritePTR)(HANDLE, PVOID, DWORD, PDWORD, DWORD);
typedef DWORD (*MPUSBReadIntPTR)(HANDLE, PVOID, DWORD, PDWORD, DWORD);
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 Bisikletdll : public AtaClass
{
public:
SetupDiGetClassDevsUMPTR _SetupDiGetClassDevsUMPTR;
SetupDiEnumDeviceInterfacesUMPTR _SetupDiEnumDeviceInterfacesUMPTR;
SetupDiDestroyDeviceInfoListUMPTR _SetupDiDestroyDeviceInfoListUMPTR;
SetupDiEnumDeviceInfoUMPTR _SetupDiEnumDeviceInfoUMPTR;
SetupDiGetDeviceRegistryPropertyUMPTR _SetupDiGetDeviceRegistryPropertyUMPTR;
SetupDiSetDeviceRegistryPropertyUMPTR _SetupDiSetDeviceRegistryPropertyUMPTR;
SetupDiGetDeviceInterfaceDetailUMPTR _SetupDiGetDeviceInterfaceDetailUMPTR;
RegisterDeviceNotificationUMPTR _RegisterDeviceNotificationUMPTR;
MPUSBGetDLLVersionPTR _MPUSBGetDLLVersionPTR;
MPUSBGetDeviceCountPTR _MPUSBGetDeviceCountPTR;
MPUSBOpenPTR _MPUSBOpenPTR;
MPUSBClosePTR _MPUSBClosePTR;
MPUSBReadPTR _MPUSBReadPTR;
MPUSBWritePTR _MPUSBWritePTR;
MPUSBReadIntPTR _MPUSBReadIntPTR;
RegisterDeviceNotificationUMPTR _RegisterDeviceNotificationUMPTR;
DWORD ErrorStatusWrite;
DWORD ErrorStatusRead;
HINSTANCE hSetUpApi;
HINSTANCE hUser32;
HINSTANCE hMPUSBAPI;
string get_POT();
string get_RPM();
Bisikletdll();
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);
#ifdef UNICODE
String^ DeviceIDFromRegistry = gcnew String((wchar_t *)PropertyValueBuffer);
#else
string* DeviceIDFromRegistry = new string((char *)PropertyValueBuffer);
#endif
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:
~Bisikletdll()
{
if(AttachedState == TRUE)
{
CloseHandle(WriteHandleToUSBDevice);
CloseHandle(ReadHandleToUSBDevice);
}
}
};
string Bisikletdll::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 Bisikletdll::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];
}
}
}
Bisikletdll::Bisikletdll(){
hSetUpApi = LoadLibrary("setupapi.dll");
hUser32 = LoadLibrary("user32.dll");
hMPUSBAPI = LoadLibrary("MPUSBAPI.dll");
_SetupDiGetClassDevsUMPTR = (SetupDiGetClassDevsUMPTR)GetProcAddress(hSetUpApi, "SetupDiGetClassDevs");
_SetupDiEnumDeviceInterfacesUMPTR = (SetupDiEnumDeviceInterfacesUMPTR)GetProcAddress(hSetUpApi, "SetupDiEnumDeviceInterfaces");
_SetupDiDestroyDeviceInfoListUMPTR = (SetupDiDestroyDeviceInfoListUMPTR)GetProcAddress(hSetUpApi, "SetupDiDestroyDeviceInfoList");
_SetupDiEnumDeviceInfoUMPTR = (SetupDiEnumDeviceInfoUMPTR)GetProcAddress(hSetUpApi, "SetupDiEnumDeviceInfo");
_SetupDiGetDeviceRegistryPropertyUMPTR = (SetupDiGetDeviceRegistryPropertyUMPTR)GetProcAddress(hSetUpApi, "SetupDiGetDeviceRegistryProperty");
_SetupDiSetDeviceRegistryPropertyUMPTR = (SetupDiSetDeviceRegistryPropertyUMPTR)GetProcAddress(hSetUpApi, "SetupDiSetDeviceRegistryProperty");
_SetupDiGetDeviceInterfaceDetailUMPTR = (SetupDiGetDeviceInterfaceDetailUMPTR)GetProcAddress(hSetUpApi, "SetupDiGetDeviceInterfaceDetail");
_MPUSBGetDLLVersionPTR = (MPUSBGetDLLVersionPTR)GetProcAddress(hMPUSBAPI, "_MPUSBGetDLLVersion");
_MPUSBGetDeviceCountPTR = (MPUSBGetDeviceCountPTR)GetProcAddress(hMPUSBAPI, "_MPUSBGetDeviceCount");
_MPUSBOpenPTR = (MPUSBOpenPTR)GetProcAddress(hMPUSBAPI, "_MPUSBOpen");
_MPUSBClosePTR = (MPUSBClosePTR)GetProcAddress(hMPUSBAPI, "_MPUSBClose");
_MPUSBReadPTR = (MPUSBReadPTR)GetProcAddress(hMPUSBAPI, "_MPUSBRead");
_MPUSBWritePTR = (MPUSBWritePTR)GetProcAddress(hMPUSBAPI, "_MPUSBWrite");
_MPUSBReadIntPTR = (MPUSBReadIntPTR)GetProcAddress(hMPUSBAPI, "_MPUSBReadInt");
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;
}
}
extern "C" __declspec(dllexport) AtaClass* APIENTRY create_AtaClass()
{
return new Bisikletdll;
}
#include "stdafx.h"
#include "AtaClass.h" //can't include that
#include <Windows.h>
#include <iostream>
using namespace std;
typedef AtaClass* (APIENTRY *ataclass_factory)();
int main()
{
HINSTANCE dll_handle = ::LoadLibrary(TEXT("Bisikletdll.dll");
if(!dll_handle){
cerr << "Yukleme basarisiz!";
return 1;
}
ataclass_factory factory_func = reinterpret_cast<ataclass_factory>(::GetProcAddress(dll_handle, "create_AtaClass"));
if (!factory_func) {
cerr << "create_AtaClass DLL'den yuklenemedi!\n";
::FreeLibrary(dll_handle);
return 1;
}
AtaClass* instance = factory_func();
string t = instance->get_POT();
cout << "POT : " << t << endl;
string v = instance->get_RPM();
cout << "RPM : " << v << endl;
return 0;
}
Apologies for the lengthy code but i presumed you needed every detail. It's usual USB programming. Thanks for help.