IPortableDevice *open_device(const wchar_t *pnp_id, IPortableDeviceValues *client_information) { // {{{ IPortableDevice *device = NULL; HRESULT hr; Py_BEGIN_ALLOW_THREADS; hr = CoCreateInstance(CLSID_PortableDevice, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&device)); Py_END_ALLOW_THREADS; if (FAILED(hr)) { hresult_set_exc("Failed to create IPortableDevice", hr); device = NULL; } else { Py_BEGIN_ALLOW_THREADS; hr = device->Open(pnp_id, client_information); Py_END_ALLOW_THREADS; if FAILED(hr) { Py_BEGIN_ALLOW_THREADS; device->Release(); Py_END_ALLOW_THREADS; device = NULL; hresult_set_exc((hr == E_ACCESSDENIED) ? "Read/write access to device is denied": "Failed to open device", hr); } } return device; } // }}}
JNIEXPORT void JNICALL Java_jmtp_PortableDeviceImplWin32_openImpl (JNIEnv* env, jobject obj, jobject values) { HRESULT hr; IPortableDevice* pDevice; jobject reference; jmethodID mid; IPortableDeviceValues* pClientInfo; LPWSTR wszDeviceID; jfieldID fid; jstring jsDeviceID; pDevice = GetPortableDevice(env, obj); //clientinfo value object opvragen mid = env->GetMethodID(env->GetObjectClass(values), "getReference", "()Lbe/derycke/pieter/com/COMReference;"); reference = env->CallObjectMethod(values, mid); mid = env->GetMethodID(env->FindClass("be/derycke/pieter/com/COMReference"), "getMemoryAddress", "()J"); pClientInfo = (IPortableDeviceValues*)env->CallLongMethod(reference, mid); //deviceID opvragen fid = env->GetFieldID(env->GetObjectClass(obj), "deviceID", "Ljava/lang/String;"); jsDeviceID = (jstring)env->GetObjectField(obj, fid); wszDeviceID = (WCHAR*)env->GetStringChars(jsDeviceID, NULL); hr = pDevice->Open(wszDeviceID, pClientInfo); env->ReleaseStringChars(jsDeviceID, (jchar*)wszDeviceID); if(FAILED(hr)) { ThrowCOMException(env, L"Couldn't open the device", hr); return; } }
void Parse_MTP::mtp_do_storage(bool clear) { IPortableDevice *pd; IPortableDeviceValues *pdv; IPortableDeviceContent *pdc = NULL; HRESULT hr; hr = CoCreateInstance(CLSID_PortableDevice, NULL, CLSCTX_INPROC_SERVER, IID_IPortableDevice, (VOID**)&pd); if (FAILED(hr)) { emit add_log(LOG_ERROR, QString("mtp_do_storage: Failed to create IPortableDevice: %1") .arg(hr, 0, 16)); return; } hr = CoCreateInstance(CLSID_PortableDeviceValues, NULL, CLSCTX_INPROC_SERVER, IID_IPortableDeviceValues, (VOID**)&pdv); if (FAILED(hr)) { emit add_log(LOG_ERROR, QString("mtp_do_storage: Failed to create IPortableDeviceValues: %1") .arg(hr, 0, 16)); return; } for (int i = 0; i < mtp_devices.size(); i++) { // after this we're creating objects which need to be cleaned up. hr = pd->Open(mtp_devices[i], pdv); if (FAILED(hr)) { emit add_log(LOG_ERROR, QString("mtp_do_storage: Failed to open IPortableDevice: %1") .arg(hr, 0, 16)); goto mtp_do_storage_cleanup; } hr = pd->Content(&pdc); if (FAILED(hr)) { emit add_log(LOG_ERROR, QString("mtp_do_storage: Failed to retrieve content from IPortableDevice: %1") .arg(hr, 0, 16)); goto mtp_do_storage_cleanup; } mtp_recurse_storage(WPD_DEVICE_OBJECT_ID, pdc, clear); mtp_do_storage_cleanup: if (pdc) { pdc->Release(); pdc = NULL; } if (pdv) { pdv->Release(); pdv = NULL; } pd->Close(); } }
bool Parse_MTP::is_device_mtp(DWORD index, LPCWSTR device_id) { bool is_mtp = false; IPortableDevice *pd; IPortableDeviceValues *pdv; IPortableDeviceKeyCollection *pdkc; IPortableDeviceProperties *pdp = NULL; IPortableDeviceContent *pdc = NULL; HRESULT hr; LPWSTR dev_protocol = NULL; QString mtp; hr = CoCreateInstance(CLSID_PortableDevice, NULL, CLSCTX_INPROC_SERVER, IID_IPortableDevice, (VOID**)&pd); if (FAILED(hr)) { emit add_log(LOG_ERROR, QString("is_device_mtp: Failed to create IPortableDevice: %1") .arg(hr, 0, 16)); return false; } hr = CoCreateInstance(CLSID_PortableDeviceValues, NULL, CLSCTX_INPROC_SERVER, IID_IPortableDeviceValues, (VOID**)&pdv); if (FAILED(hr)) { emit add_log(LOG_ERROR, QString("is_device_mtp: Failed to create IPortableDeviceValues: %1") .arg(hr, 0, 16)); return false; } hr = CoCreateInstance(CLSID_PortableDeviceKeyCollection, NULL, CLSCTX_INPROC_SERVER, IID_IPortableDeviceKeyCollection, (VOID**)&pdkc); if (FAILED(hr)) { emit add_log(LOG_ERROR, QString("is_device_mtp: Failed to create IPortableDeviceKeyCollection: %1") .arg(hr, 0, 16)); return false; } // after this we're creating objects which need to be cleaned up. hr = pd->Open(device_id, pdv); if (FAILED(hr)) { emit add_log(LOG_ERROR, QString("is_device_mtp: Failed to open IPortableDevice: %1") .arg(hr, 0, 16)); goto is_mtp_cleanup; } hr = pd->Content(&pdc); if (FAILED(hr)) { emit add_log(LOG_ERROR, QString("is_device_mtp: Failed to retrieve content from IPortableDevice: %1") .arg(hr, 0, 16)); goto is_mtp_cleanup; } hr = pdc->Properties(&pdp); if (FAILED(hr)) { emit add_log(LOG_ERROR, QString("is_device_mtp: Failed to get properties from IPortableDeviceContent: %1") .arg(hr, 0, 16)); goto is_mtp_cleanup; } hr = pdkc->Add(WPD_DEVICE_PROTOCOL); if (FAILED(hr)) { emit add_log(LOG_ERROR, QString("is_device_mtp: Failed to add to IPortableDeviceKeyCollection: %1") .arg(hr, 0, 16)); goto is_mtp_cleanup; } // WPD_DEVICE_OBJECT_ID is the top level object hr = pdp->GetValues(WPD_DEVICE_OBJECT_ID, pdkc, &pdv); if (FAILED(hr)) { emit add_log(LOG_ERROR, QString("is_device_mtp: Failed to get values from IPortableDeviceProperties: %1") .arg(hr, 0, 16)); goto is_mtp_cleanup; } hr = pdv->GetStringValue(WPD_DEVICE_PROTOCOL, &dev_protocol); if (FAILED(hr)) { emit add_log(LOG_ERROR, QString("is_device_mtp: Failed to GetStringValue: %1") .arg(hr, 0, 16)); goto is_mtp_cleanup; } mtp = QString::fromStdWString(dev_protocol); emit add_log(LOG_INFO, QString("Device %1: %2").arg(index).arg(mtp)); if (mtp.startsWith("MTP")) { is_mtp = true; emit add_log(LOG_INFO, QString("Device %1: Is a MTP device").arg(index)); } is_mtp_cleanup: if (dev_protocol) CoTaskMemFree(dev_protocol); if (pdc) { pdc->Release(); pdc = NULL; } if (pdp) { pdp->Release(); pdp = NULL; } if (pdv) { pdv->Release(); pdv = NULL; } return is_mtp; }