C++ (Qt)QThread *thread=new QThread();mythread *work=new mythread(); work->moveToThread(thread); //слот получающий имя устройства при старте потока connect(thread,SIGNAL(started()),work,SLOT(friendlyname())); //передаем имя устройства для поиска в класс connect(this,SIGNAL(setname(QString)),work,SLOT(setfind(QString)),Qt::DirectConnection); // после отработки функции она посылает нам сигнал с дружественным именем connect(work,SIGNAL(finis(QString)),this,SLOT(friname(QString)),Qt::DirectConnection); connect(work,SIGNAL(fan()),thread,SLOT(quit()),Qt::QueuedConnection); connect(work,SIGNAL(fan()),work,SLOT(deleteLater()),Qt::QueuedConnection); connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater()),Qt::QueuedConnection);emit setname(hardwareserial); //передаем в поток данныеthread->wait();thread->start();
C++ (Qt)#include "mythread.h" mythread::mythread(QObject *parent) : QObject(parent){ } void mythread::setfind(QString ser){ serial=ser;} void mythread::friendlyname(){ //нахождение имени VARIANT vtProp2; VARIANT vtProp4; IWbemLocator *pLoc = NULL; IWbemServices *pSvc= NULL; IWbemClassObject *pclsObj = NULL; //Создаем объект IWbemLocator HRESULT hres; hres = CoInitializeEx(0,COINIT_APARTMENTTHREADED); if (FAILED(hres)) { cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl; //return listdevice; // Program has failed. } hres = CoInitializeSecurity( NULL, -1, // COM authentication NULL, // Authentication services NULL, // Reserved RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation NULL, // Authentication info EOAC_NONE, // Additional capabilities NULL // Reserved );// if (FAILED(hres))// {// cout << "security. Error code = 0x"// << hex << hres << endl;// //return listdevice; // Program has failed.// } hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc); if (FAILED(hres)) { cout << "create inst loc. Error code = 0x" << hex << hres << endl; //return listdevice; // Program has failed. } hres = pLoc->ConnectServer( _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace NULL, // User name. NULL = current user NULL, // User password. NULL = current 0, // Locale. NULL indicates current NULL, // Security flags. 0, // Authority (for example, Kerberos) 0, // Context object &pSvc // pointer to IWbemServices proxy ); if (FAILED(hres)) { cout << "connectserver error. Error code = 0x" << hex << hres << endl; //return listdevice; // Program has failed. } IEnumWbemClassObject* pEnumerator = NULL; ULONG uReturn = 0; hres = CoSetProxyBlanket( pSvc, // Indicates the proxy to set RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx NULL, // Server principal name RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx NULL, // client identity EOAC_NONE // proxy capabilities ); if (FAILED(hres)) { cout << "proxyblanket. Error code = 0x" << hex << hres << endl; //return listdevice; // Program has failed. } hres = pSvc->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * FROM Win32_DiskDrive"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { cout << "execquery. Error code = 0x" << hex << hres << endl; //return listdevice; // Program has failed. } while (pEnumerator) { HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if(0 == uReturn) { break; } hr=pclsObj->Get(L"PNPDeviceID", 0, &vtProp4, 0, 0); hr=pclsObj->Get(L"Model",0,&vtProp2,0,0); BSTR rr=vtProp4.bstrVal; // QString pnpdeviceid((QChar*)rr,::SysStringLen(rr)); //QString pnpp((QChar*)pnp,::SysStringLen(pnp)); //сравниваем два номера pnpdeviceid в случае если номер совпадает //значит устройство является сменным носителем ифнормации if (pnpdeviceid.contains(serial)) { BSTR g=vtProp2.bstrVal; QString friendlyname((QChar*)g,::SysStringLen(g)); emit finis(friendlyname); } } if (pLoc) pLoc->Release(); if (pSvc) pSvc->Release(); if (pclsObj) pclsObj->Release(); if (pEnumerator) pEnumerator->Release(); CoUninitialize(); emit fan(); //заканчиваем поток}
C++ (Qt)void mythread::setfind(QString ser){ serial=ser;}
C++ (Qt)emit finis(friendlyname); //передаем в главную форму
C++ (Qt) QFuture <QString> future; QFutureWatcher<QString> watch; future=QtConcurrent::run(this,&MainWindow::getnameusbdevice,hardwareserial); watch.setFuture(future); QString gg=future.result();
C++ (Qt)future=QtConcurrent::run(this,&MainWindow::getnameusbdevice,hardwareserial); watch.setFuture(future); watch.waitForFinished(); QString gg=future.result();
C++ (Qt)QString MainWindow::getnameusbdevice(QString serial){ VARIANT vtProp2; VARIANT vtProp4; QString drl; IWbemLocator *pLoc = NULL; IWbemServices *pSvc= NULL; IWbemClassObject *pclsObj = NULL; //Создаем объект IWbemLocator HRESULT hres; hres = CoInitializeEx(0,COINIT_APARTMENTTHREADED); if (FAILED(hres)) { cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl; //return listdevice; // Program has failed. } hres = CoInitializeSecurity( NULL, -1, // COM authentication NULL, // Authentication services NULL, // Reserved RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation NULL, // Authentication info EOAC_NONE, // Additional capabilities NULL // Reserved );// if (FAILED(hres))// {// cout << "security. Error code = 0x"// << hex << hres << endl;// //return listdevice; // Program has failed.// } hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc); if (FAILED(hres)) { cout << "create inst loc. Error code = 0x" << hex << hres << endl; //return listdevice; // Program has failed. } hres = pLoc->ConnectServer( _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace NULL, // User name. NULL = current user NULL, // User password. NULL = current 0, // Locale. NULL indicates current NULL, // Security flags. 0, // Authority (for example, Kerberos) 0, // Context object &pSvc // pointer to IWbemServices proxy ); if (FAILED(hres)) { cout << "connectserver error. Error code = 0x" << hex << hres << endl; //return listdevice; // Program has failed. } IEnumWbemClassObject* pEnumerator = NULL; ULONG uReturn = 0; hres = CoSetProxyBlanket( pSvc, // Indicates the proxy to set RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx NULL, // Server principal name RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx NULL, // client identity EOAC_NONE // proxy capabilities ); if (FAILED(hres)) { cout << "proxyblanket. Error code = 0x" << hex << hres << endl; //return listdevice; // Program has failed. } hres = pSvc->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * FROM Win32_DiskDrive"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { cout << "execquery. Error code = 0x" << hex << hres << endl; //return listdevice; // Program has failed. } // QString drl; while (pEnumerator) { HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if(0 == uReturn) { break; } hr=pclsObj->Get(L"PNPDeviceID", 0, &vtProp4, 0, 0); hr=pclsObj->Get(L"Model",0,&vtProp2,0,0); BSTR rr=vtProp4.bstrVal; //сравниваем pnpdeviceid РґРІСѓС… объектов // QString pnpdeviceid((QChar*)rr,::SysStringLen(rr)); //QString pnpp((QChar*)pnp,::SysStringLen(pnp)); //сравниваем два номера pnpdeviceid в случае если номер совпадает //значит устройство является сменным носителем ифнормации if (pnpdeviceid.contains(serial)) { BSTR g=vtProp2.bstrVal; QString friendlyname((QChar*)g,::SysStringLen(g)); drl=friendlyname; } } if (pLoc) pLoc->Release(); if (pSvc) pSvc->Release(); if (pclsObj) pclsObj->Release(); if (pEnumerator) pEnumerator->Release(); CoUninitialize(); qDebug()<<"FFFFF="<<drl; return drl;}
C++ (Qt)QString gg=future.result();
C++ (Qt)future=QtConcurrent::run(this,&MainWindow::getnameusbdevice,hardwareserial);QString gg=future.result();