JNIEXPORT jobject JNICALL Java_jmtp_PortableDeviceContentImplWin32_getProperties
	(JNIEnv* env, jobject obj)
{
	//variabelen
	HRESULT hr;
	IPortableDeviceContent* pContent;
	IPortableDeviceProperties* pProperties;
	jclass cls;
	jmethodID mid;
	jobject jobjReference;


	//Methode implementatie
	pContent = GetPortableDeviceContent(env, obj);
	hr = pContent->Properties(&pProperties);

	if(SUCCEEDED(hr))
	{
		//smart reference object aanmaken
		cls = env->FindClass("be/derycke/pieter/com/COMReference");
		mid = env->GetMethodID(cls, "<init>", "(J)V");
		jobjReference = env->NewObject(cls, mid, pProperties);
		
		cls = env->FindClass("jmtp/PortableDevicePropertiesImplWin32");
		mid = env->GetMethodID(cls, "<init>", "(Lbe/derycke/pieter/com/COMReference;)V");
		return env->NewObject(cls, mid, jobjReference);
	}
	else
	{
		ThrowCOMException(env, L"Failed to retrieve the property object", hr);
		return NULL;
	}
}
JNIEXPORT void JNICALL Java_jmtp_PortableDeviceContentImplWin32_delete
	(JNIEnv* env, jobject obj, jint jiOptions, jobject jobjObjectIDs)
{
	//variabelen
	HRESULT hr;
	IPortableDeviceContent* pDeviceContent;
	IPortableDevicePropVariantCollection* pObjectIDs;
	

	//Methode implementatie
	if(jiOptions == 0 || jiOptions == 1)
	{
		pDeviceContent = GetPortableDeviceContent(env, obj);
		pObjectIDs = (IPortableDevicePropVariantCollection*)GetComReferencePointerFromComReferenceable(env, jobjObjectIDs);
		hr = pDeviceContent->Delete(jiOptions, pObjectIDs, NULL);

		if(FAILED(hr))
		{
			ThrowCOMException(env, L"Failed to delete the files", hr);
			return;
		}
	}
	else
	{
		env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"), "The parameter options has an invalid value.");
		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();
    }
}
JNIEXPORT jobjectArray JNICALL Java_jmtp_PortableDeviceContentImplWin32_listChildObjects
	(JNIEnv* env, jobject obj, jstring jsParentID)
{
	//variabelen
	HRESULT hr;
	IPortableDeviceContent* pDeviceContent;
	LPWSTR wszParentID;
	CComPtr<IEnumPortableDeviceObjectIDs> pEnum;
	std::list<LPWSTR> childList;
	std::list<LPWSTR>::iterator iterator;
	LPWSTR* wszObjectID;
	ULONG fetched;
	jobjectArray jobjaChildArray;


	//Methode implementatie
	pDeviceContent = GetPortableDeviceContent(env, obj);
	wszParentID = (WCHAR*)env->GetStringChars(jsParentID, NULL);
	hr = pDeviceContent->EnumObjects(0, wszParentID, NULL, &pEnum);
	env->ReleaseStringChars(jsParentID, (jchar*)wszParentID);
	
	if(SUCCEEDED(hr))
	{
		wszObjectID = new LPWSTR[1];
		while(SUCCEEDED(pEnum->Next(1, wszObjectID, &fetched)) && fetched > 0)
		{
			childList.push_back(wszObjectID[0]);
		}
		delete[] wszObjectID;

		jobjaChildArray = env->NewObjectArray(childList.size(), env->FindClass("Ljava/lang/String;"), NULL);
		int i = 0;
		for (iterator = childList.begin(); iterator != childList.end(); iterator++)
		{
			env->SetObjectArrayElement(jobjaChildArray, i, env->NewString((jchar*)*iterator, wcslen(*iterator)));
			CoTaskMemFree(*iterator);
			i++;
		}

		return jobjaChildArray;
	}
	else
	{
		ThrowCOMException(env, L"Failed to retrieve the enumerator", hr);
	}
	
	return NULL;
}
JNIEXPORT jobject JNICALL Java_jmtp_PortableDeviceContentImplWin32_getObjectIDsFromPersistentUniqueIDs
	(JNIEnv* env, jobject obj, jobject jobjPropVariantCollection)
{
	//variabelen
	HRESULT hr;
	IPortableDeviceContent* pContent;
	IPortableDevicePropVariantCollection* pPersistentUniqueIDs;
	IPortableDevicePropVariantCollection* pObjectIDs;
	jclass cls;
	jmethodID mid;
	jobject jobjReference;

	//Methode implementatie
	if(jobjPropVariantCollection != NULL)
	{
		pContent = GetPortableDeviceContent(env, obj);
		pPersistentUniqueIDs =
			(IPortableDevicePropVariantCollection*)GetComReferencePointerFromComReferenceable(env, jobjPropVariantCollection);

		hr = pContent->GetObjectIDsFromPersistentUniqueIDs(pPersistentUniqueIDs, &pObjectIDs);

		if(SUCCEEDED(hr))
		{
			//smart reference object aanmaken
			cls = env->FindClass("be/derycke/pieter/com/COMReference");
			mid = env->GetMethodID(cls, "<init>", "(J)V");
			jobjReference = env->NewObject(cls, mid, pObjectIDs);
			
			cls = env->FindClass("jmtp/PortableDevicePropVariantCollectionImplWin32");
			mid = env->GetMethodID(cls, "<init>", "(Lbe/derycke/pieter/com/COMReference;)V");
			return env->NewObject(cls, mid, jobjReference);
		}
		else
		{
			ThrowCOMException(env, L"Failed to retrieve the ObjectIDs", hr);
			return NULL;
		}
	}
	else
	{
		env->ThrowNew(env->FindClass("java/lang/NullPointerException"), "persistentUniqueIDs can't be null");
		return NULL;
	}
}
JNIEXPORT jstring JNICALL Java_jmtp_PortableDeviceContentImplWin32_createObjectWithPropertiesOnly
	(JNIEnv* env, jobject obj, jobject jobjValues)
{
	//variabelen
	HRESULT hr;
	IPortableDeviceContent* pDeviceContent;
	IPortableDeviceValues* pValues;
	LPWSTR wszObjectID;
	jstring jsObjectID;

	
	//Methode implementatie
	if(jobjValues != NULL)
	{
		pDeviceContent = GetPortableDeviceContent(env, obj);
		pValues = (IPortableDeviceValues*)GetComReferencePointerFromComReferenceable(env, jobjValues);
		hr = pDeviceContent->CreateObjectWithPropertiesOnly(pValues, &wszObjectID);

		if(SUCCEEDED(hr))
		{
			jsObjectID = (jstring)env->NewString((jchar*)wszObjectID, wcslen(wszObjectID));
			CoTaskMemFree(wszObjectID);
			return jsObjectID;
		}
		else
		{
			ThrowCOMException(env, L"Couldn't create the file", hr);
			return NULL;
		}
	}
	else
	{
		env->ThrowNew(env->FindClass("java/lang/NullPointerException"), "values can't be null");
		return NULL;
	}
}
示例#7
0
PyObject* get_storage_info(IPortableDevice *device) { // {{{
    HRESULT hr, hr2;
    IPortableDeviceContent *content = NULL;
    IEnumPortableDeviceObjectIDs *objects = NULL;
    IPortableDeviceProperties *properties = NULL;
    IPortableDeviceKeyCollection *storage_properties = NULL;
    IPortableDeviceValues *values = NULL;
    PyObject *ans = NULL, *storage = NULL, *so = NULL, *desc = NULL, *soid = NULL;
    DWORD fetched, i;
    PWSTR object_ids[10];
    GUID guid;
    ULONGLONG capacity, free_space, capacity_objects, free_objects;
    ULONG access, storage_type = WPD_STORAGE_TYPE_UNDEFINED;
    LPWSTR storage_desc = NULL, st = NULL;

    storage = PyList_New(0);
    if (storage == NULL) { PyErr_NoMemory(); goto end; }

    Py_BEGIN_ALLOW_THREADS;
    hr = device->Content(&content);
    Py_END_ALLOW_THREADS;
    if (FAILED(hr)) {hresult_set_exc("Failed to get content interface from device", hr); goto end;}

    Py_BEGIN_ALLOW_THREADS;
    hr = content->Properties(&properties);
    Py_END_ALLOW_THREADS;
    if (FAILED(hr)) {hresult_set_exc("Failed to get properties interface", hr); goto end;}

    Py_BEGIN_ALLOW_THREADS;
    hr = CoCreateInstance(CLSID_PortableDeviceKeyCollection, NULL,
            CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&storage_properties));
    Py_END_ALLOW_THREADS;
    if (FAILED(hr)) {hresult_set_exc("Failed to create storage properties collection", hr); goto end;}

    Py_BEGIN_ALLOW_THREADS;
    hr = storage_properties->Add(WPD_OBJECT_CONTENT_TYPE);
    hr = storage_properties->Add(WPD_FUNCTIONAL_OBJECT_CATEGORY);
    hr = storage_properties->Add(WPD_STORAGE_DESCRIPTION);
    hr = storage_properties->Add(WPD_STORAGE_CAPACITY);
    hr = storage_properties->Add(WPD_STORAGE_CAPACITY_IN_OBJECTS);
    hr = storage_properties->Add(WPD_STORAGE_FREE_SPACE_IN_BYTES);
    hr = storage_properties->Add(WPD_STORAGE_FREE_SPACE_IN_OBJECTS);
    hr = storage_properties->Add(WPD_STORAGE_ACCESS_CAPABILITY);
    hr = storage_properties->Add(WPD_STORAGE_FILE_SYSTEM_TYPE);
    hr = storage_properties->Add(WPD_STORAGE_TYPE);
    hr = storage_properties->Add(WPD_OBJECT_NAME);
    Py_END_ALLOW_THREADS;
    if (FAILED(hr)) {hresult_set_exc("Failed to create collection of properties for storage query", hr); goto end; }

    Py_BEGIN_ALLOW_THREADS;
    hr = content->EnumObjects(0, WPD_DEVICE_OBJECT_ID, NULL, &objects);
    Py_END_ALLOW_THREADS;
    if (FAILED(hr)) {hresult_set_exc("Failed to get objects from device", hr); goto end;}

    hr = S_OK;
    while (hr == S_OK) {
        Py_BEGIN_ALLOW_THREADS;
        hr = objects->Next(10, object_ids, &fetched);
        Py_END_ALLOW_THREADS;
        if (SUCCEEDED(hr)) {
            for(i = 0; i < fetched; i++) {
                Py_BEGIN_ALLOW_THREADS;
                hr2 = properties->GetValues(object_ids[i], storage_properties, &values);
                Py_END_ALLOW_THREADS;
                if SUCCEEDED(hr2) {
                    if (
                        SUCCEEDED(values->GetGuidValue(WPD_OBJECT_CONTENT_TYPE, &guid)) && IsEqualGUID(guid, WPD_CONTENT_TYPE_FUNCTIONAL_OBJECT) &&
                        SUCCEEDED(values->GetGuidValue(WPD_FUNCTIONAL_OBJECT_CATEGORY, &guid)) && IsEqualGUID(guid, WPD_FUNCTIONAL_CATEGORY_STORAGE)
                       ) {
                        capacity = 0; capacity_objects = 0; free_space = 0; free_objects = 0;
                        values->GetUnsignedLargeIntegerValue(WPD_STORAGE_CAPACITY, &capacity);
                        values->GetUnsignedLargeIntegerValue(WPD_STORAGE_CAPACITY_IN_OBJECTS, &capacity_objects);
                        values->GetUnsignedLargeIntegerValue(WPD_STORAGE_FREE_SPACE_IN_BYTES, &free_space);
                        values->GetUnsignedLargeIntegerValue(WPD_STORAGE_FREE_SPACE_IN_OBJECTS, &free_objects);
                        values->GetUnsignedIntegerValue(WPD_STORAGE_TYPE, &storage_type);
                        desc = Py_False;
                        if (SUCCEEDED(values->GetUnsignedIntegerValue(WPD_STORAGE_ACCESS_CAPABILITY, &access)) && access == WPD_STORAGE_ACCESS_CAPABILITY_READWRITE) desc = Py_True;
                        soid = PyUnicode_FromWideChar(object_ids[i], wcslen(object_ids[i]));
                        if (soid == NULL) { PyErr_NoMemory(); goto end; }
                        so = Py_BuildValue("{s:K, s:K, s:K, s:K, s:O, s:N}",
                                "capacity", capacity, "capacity_objects", capacity_objects, "free_space", free_space, "free_objects", free_objects, "rw", desc, "id", soid);
                        if (so == NULL) { PyErr_NoMemory(); goto end; }
                        if (SUCCEEDED(values->GetStringValue(WPD_STORAGE_DESCRIPTION, &storage_desc))) {
                                desc = PyUnicode_FromWideChar(storage_desc, wcslen(storage_desc));
                                if (desc != NULL) { PyDict_SetItemString(so, "description", desc); Py_DECREF(desc);}
                                CoTaskMemFree(storage_desc); storage_desc = NULL;
                        }
                        if (SUCCEEDED(values->GetStringValue(WPD_OBJECT_NAME, &storage_desc))) {
                                desc = PyUnicode_FromWideChar(storage_desc, wcslen(storage_desc));
                                if (desc != NULL) { PyDict_SetItemString(so, "name", desc); Py_DECREF(desc);}
                                CoTaskMemFree(storage_desc); storage_desc = NULL;
                        }
                        if (SUCCEEDED(values->GetStringValue(WPD_STORAGE_FILE_SYSTEM_TYPE, &storage_desc))) {
                                desc = PyUnicode_FromWideChar(storage_desc, wcslen(storage_desc));
                                if (desc != NULL) { PyDict_SetItemString(so, "filesystem", desc); Py_DECREF(desc);}
                                CoTaskMemFree(storage_desc); storage_desc = NULL;
                        }
                        switch(storage_type) {
                            case WPD_STORAGE_TYPE_REMOVABLE_RAM:
                                st = L"removable_ram";
                                break;
                            case WPD_STORAGE_TYPE_REMOVABLE_ROM:
                                st = L"removable_rom";
                                break;
                            case WPD_STORAGE_TYPE_FIXED_RAM:
                                st = L"fixed_ram";
                                break;
                            case WPD_STORAGE_TYPE_FIXED_ROM:
                                st = L"fixed_rom";
                                break;
                            default:
                                st = L"unknown_unknown";
                        }
                        desc = PyUnicode_FromWideChar(st, wcslen(st));
                        if (desc != NULL) {PyDict_SetItemString(so, "type", desc); Py_DECREF(desc);}
                        desc = NULL;
                        PyList_Append(storage, so);
                        Py_DECREF(so);
                    }
                }
            }
            for (i = 0; i < fetched; i ++) { CoTaskMemFree(object_ids[i]); object_ids[i] = NULL;}
        }// if(SUCCEEDED(hr))
    }
    ans = storage;

end:
    if (content != NULL) content->Release();
    if (objects != NULL) objects->Release();
    if (properties != NULL) properties->Release();
    if (storage_properties != NULL) storage_properties->Release();
    if (values != NULL) values->Release();
    return ans;
} // }}}
示例#8
0
PyObject* get_device_information(IPortableDevice *device, IPortableDevicePropertiesBulk **pb) { // {{{
    IPortableDeviceContent *content = NULL;
    IPortableDeviceProperties *properties = NULL;
    IPortableDevicePropertiesBulk *properties_bulk = NULL;
    IPortableDeviceKeyCollection *keys = NULL;
    IPortableDeviceValues *values = NULL;
    IPortableDeviceCapabilities *capabilities = NULL;
    IPortableDevicePropVariantCollection *categories = NULL;
    HRESULT hr;
    DWORD num_of_categories, i;
    LPWSTR temp;
    ULONG ti;
    PyObject *t, *ans = NULL, *storage = NULL;
    const char *type = NULL;

    Py_BEGIN_ALLOW_THREADS;
    hr = CoCreateInstance(CLSID_PortableDeviceKeyCollection, NULL,
            CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&keys));
    Py_END_ALLOW_THREADS;
    if (FAILED(hr)) {hresult_set_exc("Failed to create IPortableDeviceKeyCollection", hr); goto end;}

    Py_BEGIN_ALLOW_THREADS;
    hr = keys->Add(WPD_DEVICE_PROTOCOL);
    // Despite the MSDN documentation, this does not exist in PortableDevice.h
    // hr = keys->Add(WPD_DEVICE_TRANSPORT);
    hr = keys->Add(WPD_DEVICE_FRIENDLY_NAME);
    hr = keys->Add(WPD_DEVICE_MANUFACTURER);
    hr = keys->Add(WPD_DEVICE_MODEL);
    hr = keys->Add(WPD_DEVICE_SERIAL_NUMBER);
    hr = keys->Add(WPD_DEVICE_FIRMWARE_VERSION);
    hr = keys->Add(WPD_DEVICE_TYPE);
    Py_END_ALLOW_THREADS;
    if (FAILED(hr)) {hresult_set_exc("Failed to add keys to IPortableDeviceKeyCollection", hr); goto end;}

    Py_BEGIN_ALLOW_THREADS;
    hr = device->Content(&content);
    Py_END_ALLOW_THREADS;
    if (FAILED(hr)) {hresult_set_exc("Failed to get IPortableDeviceContent", hr); goto end; }

    Py_BEGIN_ALLOW_THREADS;
    hr = content->Properties(&properties);
    Py_END_ALLOW_THREADS;
    if (FAILED(hr)) {hresult_set_exc("Failed to get IPortableDeviceProperties", hr); goto end; }

    Py_BEGIN_ALLOW_THREADS;
    hr = properties->GetValues(WPD_DEVICE_OBJECT_ID, keys, &values);
    Py_END_ALLOW_THREADS;
    if(FAILED(hr)) {hresult_set_exc("Failed to get device info", hr); goto end; }

    Py_BEGIN_ALLOW_THREADS;
    hr = device->Capabilities(&capabilities);
    Py_END_ALLOW_THREADS;
    if(FAILED(hr)) {hresult_set_exc("Failed to get device capabilities", hr); goto end; }

    Py_BEGIN_ALLOW_THREADS;
    hr = capabilities->GetFunctionalCategories(&categories);
    Py_END_ALLOW_THREADS;
    if(FAILED(hr)) {hresult_set_exc("Failed to get device functional categories", hr); goto end; }

    Py_BEGIN_ALLOW_THREADS;
    hr = categories->GetCount(&num_of_categories);
    Py_END_ALLOW_THREADS;
    if(FAILED(hr)) {hresult_set_exc("Failed to get device functional categories number", hr); goto end; }

    ans = PyDict_New();
    if (ans == NULL) {PyErr_NoMemory(); goto end;}

    if (SUCCEEDED(values->GetStringValue(WPD_DEVICE_PROTOCOL, &temp))) {
        t = PyUnicode_FromWideChar(temp, wcslen(temp));
        if (t != NULL) {PyDict_SetItemString(ans, "protocol", t); Py_DECREF(t);}
        CoTaskMemFree(temp);
    }

    // if (SUCCEEDED(values->GetUnsignedIntegerValue(WPD_DEVICE_TRANSPORT, &ti))) {
    //     PyDict_SetItemString(ans, "isusb", (ti == WPD_DEVICE_TRANSPORT_USB) ? Py_True : Py_False);
    //     t = PyLong_FromUnsignedLong(ti);
    // }

    if (SUCCEEDED(values->GetUnsignedIntegerValue(WPD_DEVICE_TYPE, &ti))) {
        switch (ti) {
            case WPD_DEVICE_TYPE_CAMERA:
                type = "camera"; break;
            case WPD_DEVICE_TYPE_MEDIA_PLAYER:
                type = "media player"; break;
            case WPD_DEVICE_TYPE_PHONE:
                type = "phone"; break;
            case WPD_DEVICE_TYPE_VIDEO:
                type = "video"; break;
            case WPD_DEVICE_TYPE_PERSONAL_INFORMATION_MANAGER:
                type = "personal information manager"; break;
            case WPD_DEVICE_TYPE_AUDIO_RECORDER:
                type = "audio recorder"; break;
            default:
                type = "unknown";
        }
#if PY_MAJOR_VERSION >= 3
        t = PyUnicode_FromString(type);
#else
        t = PyString_FromString(type);
#endif
        if (t != NULL) {
            PyDict_SetItemString(ans, "type", t); Py_DECREF(t);
        }
    }

    if (SUCCEEDED(values->GetStringValue(WPD_DEVICE_FRIENDLY_NAME, &temp))) {
        t = PyUnicode_FromWideChar(temp, wcslen(temp));
        if (t != NULL) {PyDict_SetItemString(ans, "friendly_name", t); Py_DECREF(t);}
        CoTaskMemFree(temp);
    }

    if (SUCCEEDED(values->GetStringValue(WPD_DEVICE_MANUFACTURER, &temp))) {
        t = PyUnicode_FromWideChar(temp, wcslen(temp));
        if (t != NULL) {PyDict_SetItemString(ans, "manufacturer_name", t); Py_DECREF(t);}
        CoTaskMemFree(temp);
    }

    if (SUCCEEDED(values->GetStringValue(WPD_DEVICE_MODEL, &temp))) {
        t = PyUnicode_FromWideChar(temp, wcslen(temp));
        if (t != NULL) {PyDict_SetItemString(ans, "model_name", t); Py_DECREF(t);}
        CoTaskMemFree(temp);
    }

    if (SUCCEEDED(values->GetStringValue(WPD_DEVICE_SERIAL_NUMBER, &temp))) {
        t = PyUnicode_FromWideChar(temp, wcslen(temp));
        if (t != NULL) {PyDict_SetItemString(ans, "serial_number", t); Py_DECREF(t);}
        CoTaskMemFree(temp);
    }

    if (SUCCEEDED(values->GetStringValue(WPD_DEVICE_FIRMWARE_VERSION, &temp))) {
        t = PyUnicode_FromWideChar(temp, wcslen(temp));
        if (t != NULL) {PyDict_SetItemString(ans, "device_version", t); Py_DECREF(t);}
        CoTaskMemFree(temp);
    }

    t = Py_False;
    for (i = 0; i < num_of_categories; i++) {
        PROPVARIANT pv;
        PropVariantInit(&pv);
        if (SUCCEEDED(categories->GetAt(i, &pv)) && pv.puuid != NULL) {
            if (IsEqualGUID(WPD_FUNCTIONAL_CATEGORY_STORAGE, *pv.puuid)) {
                t = Py_True;
            }
        }
        PropVariantClear(&pv);
        if (t == Py_True) break;
    }
    PyDict_SetItemString(ans, "has_storage", t);

    if (t == Py_True) {
        storage = get_storage_info(device);
        if (storage == NULL) {
            PyObject *exc_type, *exc_value, *exc_tb;
            PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
            if (exc_type != NULL && exc_value != NULL) {
                PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb);
                PyDict_SetItemString(ans, "storage_error", exc_value);
                Py_DECREF(exc_value); exc_value = NULL;
            }
            Py_XDECREF(exc_type); Py_XDECREF(exc_value); Py_XDECREF(exc_tb);
            goto end;
        }
        PyDict_SetItemString(ans, "storage", storage);

    }

    Py_BEGIN_ALLOW_THREADS;
    hr = properties->QueryInterface(IID_PPV_ARGS(&properties_bulk));
    Py_END_ALLOW_THREADS;
    PyDict_SetItemString(ans, "has_bulk_properties", (FAILED(hr)) ? Py_False: Py_True);
    if (pb != NULL) *pb = (SUCCEEDED(hr)) ? properties_bulk : NULL;

end:
    if (keys != NULL) keys->Release();
    if (values != NULL) values->Release();
    if (properties != NULL) properties->Release();
    if (properties_bulk != NULL && pb == NULL) properties_bulk->Release();
    if (content != NULL) content->Release();
    if (capabilities != NULL) capabilities->Release();
    if (categories != NULL) categories->Release();
    return ans;
} // }}}
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;
}
示例#10
0
JNIEXPORT jstring JNICALL Java_jmtp_PortableDeviceContentImplWin32_createObjectWithPropertiesAndData
	(JNIEnv* env, jobject obj, jobject jobjValues, jobject jobjFile)
{
	//variabelen
	HRESULT hr;
	IPortableDeviceContent* pDeviceContent;
	IPortableDeviceValues* pDeviceObjectValues;
	jobject jobjValuesReference;
	jstring jsFileLocation;
	LPWSTR wszFileLocation;
	DWORD dwBufferSize;
	CComPtr<IStream> pFileStream;
	CComPtr<IStream> pDeviceStream;
	CComPtr<IPortableDeviceDataStream> pDeviceDataStream;
	STATSTG fileStats;
	BYTE* pBuffer;
	DWORD dwReadFromStream;
	LPWSTR wszObjectID;
	jstring jsObjectID;
	jmethodID mid;


	//Methode implementatie
	pDeviceContent = GetPortableDeviceContent(env, obj);
	jobjValuesReference = RetrieveCOMReferenceFromCOMReferenceable(env, jobjValues);
	pDeviceObjectValues = (IPortableDeviceValues*)ConvertComReferenceToPointer(env, jobjValuesReference);
	
	//COM stream object aanmaken
	mid = env->GetMethodID(env->FindClass("java/io/File"), "getAbsolutePath", "()Ljava/lang/String;");
	jsFileLocation = (jstring)env->CallObjectMethod(jobjFile, mid);
	wszFileLocation = (WCHAR*)env->GetStringChars(jsFileLocation, NULL);
	hr = SHCreateStreamOnFileW(wszFileLocation, STGM_READ, &pFileStream);
	env->ReleaseStringChars(jsFileLocation, (jchar*)wszFileLocation); //string resources terug vrijgeven

	if(SUCCEEDED(hr))
	{
		//groote van het bestand bepalen
		//(door een beperking in java op het gebied van unsigned integers, moeten we het wel in c++ doen)
		pFileStream->Stat(&fileStats, STATFLAG_NONAME);
		pDeviceObjectValues->SetUnsignedLargeIntegerValue(WPD_OBJECT_SIZE, fileStats.cbSize.QuadPart);

		hr = pDeviceContent->CreateObjectWithPropertiesAndData(pDeviceObjectValues, 
			&pDeviceStream, &dwBufferSize, NULL);
		
		if(SUCCEEDED(hr))
		{
			pDeviceStream->QueryInterface(IID_IPortableDeviceDataStream, (void**)&pDeviceDataStream);

			//data kopieren
			pBuffer = new BYTE[dwBufferSize];
			dwReadFromStream = 0;
			do
			{
				pFileStream->Read(pBuffer, dwBufferSize, &dwReadFromStream);
				pDeviceDataStream->Write(pBuffer, dwReadFromStream, NULL);
			}
			while(dwReadFromStream > 0);
			delete[] pBuffer;
			hr = pDeviceDataStream->Commit(STGC_DEFAULT);

			if(SUCCEEDED(hr))
			{
				pDeviceDataStream->GetObjectID(&wszObjectID);
				jsObjectID = (jstring)env->NewString((jchar*)wszObjectID, wcslen(wszObjectID));
				CoTaskMemFree(wszObjectID);
				return jsObjectID;
			}
			else
			{
				ThrowCOMException(env, L"Couldn't commit the data to the portable device", hr);
			}
		}
		else
		{
			ThrowCOMException(env, L"Couldn't create a COM stream object to the portable device", hr);
		}
	}
	else
	{
		ThrowCOMException(env, L"Couldn't create a COM stream object to the file", hr);
	}

	return NULL;
}