Пример #1
0
BOOL PyWinObject_AsMAPIStr(PyObject *stringObject, LPTSTR *pResult, BOOL asUnicode, BOOL bNoneOK /*= FALSE*/, DWORD *pResultLen /* = NULL */)
{
#if PY_MAJOR_VERSION >= 3
	if (asUnicode)
		return PyWinObject_AsWCHAR(stringObject, (LPWSTR *)pResult, bNoneOK, pResultLen);
	else
		return PyWinObject_AsString(stringObject, (LPSTR *)pResult, bNoneOK, pResultLen);
#else
	if (asUnicode && PyUnicode_Check(stringObject))
		return PyWinObject_AsWCHAR(stringObject, (LPWSTR *)pResult, bNoneOK, pResultLen);
	// allows already encoded string pass-through workaround (backwards compat)
	return PyWinObject_AsString(stringObject, (LPSTR *)pResult, bNoneOK, pResultLen);
#endif
}
Пример #2
0
// @pymethod |PyIExtractIcon|Extract|Description of Extract.
PyObject *PyIExtractIcon::Extract(PyObject *self, PyObject *args)
{
	IExtractIconA *pIEI = GetI(self);
	if ( pIEI == NULL )
		return NULL;
	// @pyparm <o unicode>|pszFile||Description for pszFile
	// @pyparm int|nIconIndex||Description for nIconIndex
	// @pyparm int|nIconSize||Description for nIconIndex
	HICON hiconLarge;
	HICON hiconSmall;
	PyObject *obpszFile;
	char *pszFile;
	UINT nIconIndex;
	UINT nIconSize;
	if ( !PyArg_ParseTuple(args, "Oii:Extract", &obpszFile, &nIconIndex, &nIconSize) )
		return NULL;
	BOOL bPythonIsHappy = TRUE;
	if (!PyWinObject_AsString(obpszFile, &pszFile)) bPythonIsHappy = FALSE;
	if (!bPythonIsHappy) return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIEI->Extract( pszFile, nIconIndex, &hiconLarge, &hiconSmall, nIconSize );
	PyWinObject_FreeString(pszFile);
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIEI, IID_IExtractIcon );
	if (hr==S_FALSE)
		return Py_BuildValue("OO", Py_None, Py_None);
	return Py_BuildValue("NN", PyWinLong_FromHANDLE(hiconLarge),
			           PyWinLong_FromHANDLE(hiconSmall));
	// @rdesc The result is (hicon_large, hicon_small), or
	// (None,None) if the underlying function returns S_FALSE, indicating
	// the calling application should extract it.
}
Пример #3
0
STDMETHODIMP PyGExtractIcon::GetIconLocation(
		/* [unique][in] */ UINT uFlags,
		/* [unique][out] */ LPSTR szIconFile,
		/* [unique][in] */ UINT cchMax,
		/* [unique][out] */ LPINT piIndex,
		/* [unique][out] */ UINT *pflags)
{
	PY_GATEWAY_METHOD;
	if (cchMax && szIconFile)
		szIconFile[0] = 0;
	*piIndex = 0;
	*pflags = 0;
	PyObject *result;
	HRESULT hr=InvokeViaPolicy("GetIconLocation", &result, "i", uFlags);
	if (FAILED(hr)) return hr;
	PyObject *obFileName;
	// Process the Python results, and convert back to the real params
	if (PyInt_Check(result) || PyLong_Check(result))
		hr = PyInt_AsLong(result);
	else {
		if (PyArg_ParseTuple(result, "Oii", &obFileName, piIndex, pflags)) {
			char *filename;
			if (PyWinObject_AsString(obFileName, &filename)) {
				strncpy(szIconFile, filename, cchMax);
				PyWinObject_FreeString(filename);
			}
		}
		hr = MAKE_PYCOM_GATEWAY_FAILURE_CODE("GetIconLocation");
	}
	Py_DECREF(result);
	return hr;
}
Пример #4
0
BOOL PyWinObject_AsCharArray(PyObject *str_seq, char ***pchars, DWORD *str_cnt, BOOL bNoneOK)
{
	BOOL ret=FALSE;
	PyObject *str_tuple=NULL, *tuple_item;
	DWORD bufsize, tuple_index;
	*pchars=NULL;
	*str_cnt=0;

	if (bNoneOK && str_seq==Py_None)
		return TRUE;
	if ((str_tuple=PyWinSequence_Tuple(str_seq, str_cnt))==NULL)
		return FALSE;
	bufsize=*str_cnt * sizeof(char *);
	*pchars=(char **)malloc(bufsize);
	if (*pchars==NULL){
		PyErr_Format(PyExc_MemoryError, "Unable to allocate %d bytes", bufsize);
		goto done;
		}
	ZeroMemory(*pchars, bufsize);
	for (tuple_index=0;tuple_index<*str_cnt;tuple_index++){
		tuple_item=PyTuple_GET_ITEM(str_tuple, tuple_index);
		if (!PyWinObject_AsString(tuple_item, &((*pchars)[tuple_index]), FALSE)){
			PyWinObject_FreeCharArray(*pchars, *str_cnt);
			*pchars=NULL;
			*str_cnt=0;
			goto done;
			}
		}
	ret=TRUE;
done:
	Py_DECREF(str_tuple);
	return ret;
}
Пример #5
0
// @pymethod |PyICopyHookA|CopyCallback|Description of CopyCallback.
PyObject *PyICopyHookA::CopyCallback(PyObject *self, PyObject *args)
{
    ICopyHookA *pICH = GetI(self);
    if ( pICH == NULL )
        return NULL;
    // @pyparm HWND|hwnd||Description for hwnd
    // @pyparm int|wFunc||Description for wFunc
    // @pyparm int|wFlags||Description for wFlags
    // @pyparm string/<o unicode>|srcFile||Description for srcFile
    // @pyparm int|srcAttribs||Description for srcAttribs
    // @pyparm string/<o unicode>|destFile||Description for destFile
    // @pyparm int|destAttribs||Description for destAttribs
    PyObject *obsrcFile;
    PyObject *obdestFile;
    HWND hwnd;
    PyObject *obhwnd;
    UINT wFunc;
    UINT wFlags;
    LPSTR srcFile;
    DWORD srcAttribs;
    LPSTR destFile;
    DWORD destAttribs;
    if ( !PyArg_ParseTuple(args, "OiiOlOl:CopyCallback", &obhwnd, &wFunc, &wFlags, &obsrcFile, &srcAttribs, &obdestFile, &destAttribs) )
        return NULL;
    if (!PyWinObject_AsHANDLE(obhwnd, (HANDLE *)&hwnd))
        return NULL;
    BOOL bPythonIsHappy = TRUE;
    if (bPythonIsHappy && !PyWinObject_AsString(obsrcFile, &srcFile)) bPythonIsHappy = FALSE;
    if (bPythonIsHappy && !PyWinObject_AsString(obdestFile, &destFile)) bPythonIsHappy = FALSE;
    if (!bPythonIsHappy) return NULL;
    HRESULT hr;
    PY_INTERFACE_PRECALL;
    hr = pICH->CopyCallback( hwnd, wFunc, wFlags, srcFile, srcAttribs, destFile, destAttribs );
    PyWinObject_FreeString(srcFile);
    PyWinObject_FreeString(destFile);

    PY_INTERFACE_POSTCALL;

    if ( FAILED(hr) )
        return PyCom_BuildPyException(hr, pICH, IID_IShellCopyHook );
    Py_INCREF(Py_None);
    return Py_None;

}
Пример #6
0
// @object PyResourceId|Identifies a resource or function in a module.
//	This can be a WORD-sized integer value (0-65536), or string/unicode
//	depending on whether the *A or *W API function is to be called.
//	Class atoms as used with <om win32gui.CreateWindow> are also treated
//	as resource ids since they can also be represented by a name or WORD id.
//	When passing resource names and types as strings, they are usually formatted
//	as a pound sign followed by decimal form of the id.  ('#42' for example)
BOOL PyWinObject_AsResourceIdA(PyObject *ob, char **presource_id, BOOL bNoneOK)
{
	// Plain character conversion
	if (PyWinObject_AsString(ob, presource_id, bNoneOK))
		return TRUE;
	PyErr_Clear();
	if (PyWinLong_AsVoidPtr(ob, (void **)presource_id) && IS_INTRESOURCE(*presource_id))
		return TRUE;
	*presource_id=NULL;
	PyErr_SetString(PyExc_TypeError, "Resource id/name must be string or int in the range 0-65536");
	return FALSE;
}
Пример #7
0
// @object PySPropValue|A MAPI property value.  Property values can either be passed from
// python into MAPI functions, or returned from MAPI functions to Python.
BOOL PyMAPIObject_AsSPropValue(PyObject *Valob, SPropValue *pv, void *pAllocMoreLinkBlock)
{
    PyObject *ob;
    // @pyparm int|propType||The type of the MAPI property
    // @pyparm object|value||The property value
    if (!PyArg_ParseTuple(Valob, "lO:SPropValue item", &pv->ulPropTag, &ob )) {
        PyErr_Clear();
        PyErr_SetString(PyExc_TypeError, "An SPropValue item must be a tuple of (integer, object)");
        return NULL;
    }
    BOOL ok = TRUE;
    unsigned int i;
    PyErr_Clear();
    // @comm The parameters can be one of the following pairs of values.
    // @flagh propType|value
    switch (PROP_TYPE(pv->ulPropTag)) {
    // @flag PT_I2|An integer
    case PT_I2:	//		case PT_SHORT:
        pv->Value.i = (int)PyInt_AsLong(ob);
        break;
    // @flag PT_MV_I2|A sequence of integers
    case PT_MV_I2:
        MAKE_MV(short int, pAllocMoreLinkBlock, pv->Value.MVi.lpi, pv->Value.MVi.cValues, PyInt_AsLong)
    // @flag PT_I4|An integer
    case PT_I4: //		case PT_LONG:
        pv->Value.l = PyInt_AsLong(ob);
        break;
    // @flag PT_MV_I4|A sequence of integers
    case PT_MV_I4:
        MAKE_MV(long, pAllocMoreLinkBlock, pv->Value.MVl.lpl, pv->Value.MVl.cValues, PyInt_AsLong)
    // @flag PT_R4|A float
    case PT_R4: //		case PT_FLOAT:
        pv->Value.flt = (float)PyFloat_AsDouble(ob);
        break;
    // @flag PT_MV_R4|A sequence of floats
    case PT_MV_R4:
        MAKE_MV(float, pAllocMoreLinkBlock, pv->Value.MVflt.lpflt, pv->Value.MVflt.cValues, PyFloat_AsDouble)
    // @flag PT_R8|A float
    case PT_R8: //		case PT_DOUBLE:
        pv->Value.dbl = PyFloat_AsDouble(ob);
        break;
    // @flag PT_MV_R8|A sequence of floats
    case PT_MV_R8:
        MAKE_MV(double, pAllocMoreLinkBlock, pv->Value.MVdbl.lpdbl, pv->Value.MVdbl.cValues, PyFloat_AsDouble)
    // @flag PT_BOOLEAN|A boolean value (or an int)
    case PT_BOOLEAN:
        pv->Value.b = PyInt_AsLong(ob) ? VARIANT_TRUE : VARIANT_FALSE;
        break;

    /*
    		case PT_CURRENCY:
    			p->Value.cur ??
    			break;

    */
    // @flag PT_APPTIME|A <o PyTime> object
    case PT_APPTIME :
        ok = PyWinObject_AsDATE(ob, &pv->Value.at);
        break;

    // @flag PT_MV_APPTIME|An sequence of <o PyTime> object
    case PT_MV_APPTIME:
        MAKEB_MV(double, pAllocMoreLinkBlock, pv->Value.MVat.lpat, pv->Value.MVat.cValues, PyWinObject_AsDATE)

    // @flag PT_SYSTIME|A <o PyTime> object
    case PT_SYSTIME:
        ok = PyWinObject_AsFILETIME(ob, &pv->Value.ft);
        break;

    // @flag PT_MV_APPTIME|An sequence of <o PyTime> object
    case PT_MV_SYSTIME:
        MAKEB_MV(FILETIME, pAllocMoreLinkBlock, pv->Value.MVft.lpft, pv->Value.MVft.cValues, PyWinObject_AsFILETIME)

    // @flag PT_STRING8|A string or <o PyUnicode>
    case PT_STRING8:
    {   // Copy into new MAPI memory block
        DWORD bufLen;
        char *str;
        ok = PyWinObject_AsString(ob, &str, FALSE, &bufLen);
        if (ok) {
            bufLen++;
            HRESULT hr = MAPIAllocateMore(bufLen, pAllocMoreLinkBlock, (void **)&pv->Value.lpszA);
            if (S_OK!=hr) {
                OleSetOleError(hr);
                ok = FALSE;
            } else {
                memcpy(pv->Value.lpszA, str, bufLen-sizeof(char));
                // Null terminate
                memcpy(((char *)pv->Value.lpszA)+(bufLen-sizeof(char)),"\0", sizeof(char));
            }
        }
        PyWinObject_FreeString(str);
        break;
    }

    // @flag PT_STRING8|A sequence of string or <o PyUnicode> objects.
    case PT_MV_STRING8:
        ok = AllocMVBuffer( ob, sizeof(char *), pAllocMoreLinkBlock, (void **)&pv->Value.MVszA.lppszA, &pv->Value.MVszA.cValues);
        if (!ok) break;
        for (i=0; ok && !PyErr_Occurred() && i<pv->Value.MVszA.cValues; i++) {
            PyObject *obmv=PySequence_GetItem(ob,i);
            if (obmv==NULL) break;

            DWORD bufLen;
            char *str;
            ok = PyWinObject_AsString(obmv, &str, FALSE, &bufLen);
            if (ok) {
                bufLen++;
                HRESULT hr = MAPIAllocateMore(bufLen, pAllocMoreLinkBlock, (void **)&pv->Value.MVszA.lppszA[i]);
                if (S_OK!=hr) {
                    OleSetOleError(hr);
                    ok = FALSE;
                } else {
                    memcpy(pv->Value.MVszA.lppszA[i], str, bufLen-sizeof(char));
                    // Null terminate
                    memcpy(((char *)pv->Value.MVszA.lppszA[i])+(bufLen-sizeof(char)),"\0", sizeof(char));
                }
            }
            PyWinObject_FreeString(str);
            Py_DECREF(obmv);
        }
        break;

    // @flag PT_UNICODE|A string or <o PyUnicode>
    case PT_UNICODE:
    {   // Bit of a hack - need to copy into MAPI block.
        BSTR wstr = NULL;
        ok = PyWinObject_AsBstr(ob, &wstr, FALSE);
        if (ok) {
            DWORD bufSize = sizeof(WCHAR) * (SysStringLen(wstr)+1);
            HRESULT hr = MAPIAllocateMore(bufSize, pAllocMoreLinkBlock, (void **)&pv->Value.lpszW);
            if (S_OK!=hr) {
                OleSetOleError(hr);
                ok = FALSE;
            } else {
                memcpy(pv->Value.lpszW, wstr, bufSize-2);
                // Null terminate
                memcpy(((char *)pv->Value.lpszW)+(bufSize-2),"\0\0", 2);
            }
        }
        SysFreeString(wstr);
        break;
    }

    // @flag PT_MV_UNICODE|A sequence of string or <o PyUnicode>
    case PT_MV_UNICODE:
        ok = AllocMVBuffer( ob, sizeof(char *), pAllocMoreLinkBlock, (void **)&pv->Value.MVszW.lppszW, &pv->Value.MVszW.cValues);
        if (!ok) break;
        for (i=0; ok && !PyErr_Occurred() && i<pv->Value.MVszW.cValues; i++) {
            PyObject *obmv=PySequence_GetItem(ob,i);
            if (obmv==NULL) break;

            BSTR wstr = NULL;
            ok = PyWinObject_AsBstr(obmv, &wstr, FALSE);
            if (ok) {
                DWORD bufSize = sizeof(WCHAR) * (SysStringLen(wstr)+1);
                HRESULT hr = MAPIAllocateMore(bufSize, pAllocMoreLinkBlock, (void **)&pv->Value.MVszW.lppszW[i]);
                if (S_OK!=hr) {
                    OleSetOleError(hr);
                    ok = FALSE;
                } else {
                    memcpy(pv->Value.MVszW.lppszW[i], wstr, bufSize-2);
                    // Null terminate
                    memcpy(((char *)pv->Value.MVszW.lppszW[i])+(bufSize-2),"\0\0", 2);
                }
            }
            SysFreeString(wstr);
            Py_DECREF(obmv);
        }
        break;

    // @flag PT_BINARY|A string containing binary data
    case PT_BINARY:
        pv->Value.bin.lpb = (unsigned char *)PyString_AsString(ob);
        pv->Value.bin.cb = PyString_Size(ob);
        break;

    // @flag PT_MV_BINARY|A sequence of strings containing binary data
    case PT_MV_BINARY:
        ok = AllocMVBuffer( ob, sizeof(SBinary), pAllocMoreLinkBlock, (void **)&pv->Value.MVbin.lpbin, &pv->Value.MVbin.cValues);
        for (i=0; !PyErr_Occurred() && i<pv->Value.MVbin.cValues; i++) {
            PyObject *obmv=PySequence_GetItem(ob,i);
            if (obmv==NULL) break;
            pv->Value.MVbin.lpbin[i].lpb = (unsigned char *)PyString_AsString(ob);
            pv->Value.MVbin.lpbin[i].cb = PyString_Size(ob);
            Py_DECREF(obmv);
        }
        break;

    // @flag PT_CLSID|A <o PyIID>
    case PT_CLSID:
    {
        HRESULT hr = MAPIAllocateMore(sizeof(CLSID), pAllocMoreLinkBlock, (void **)&pv->Value.lpguid);
        if (S_OK != hr) {
            OleSetOleError(hr);
            ok = FALSE;
        } else
            ok = PyWinObject_AsIID(ob, pv->Value.lpguid);
        break;
    }

    // @flag PT_MV_CLSID|A sequence of <o PyIID> objects
    case PT_MV_CLSID:
        MAKEB_MV(CLSID, pAllocMoreLinkBlock, pv->Value.MVguid.lpguid, pv->Value.MVguid.cValues, PyWinObject_AsIID)

    // @flag PT_I8|A <o PyLARGE_INTEGER>
    case PT_I8:
//		case PT_LONGLONG:
        ok = PyWinObject_AsLARGE_INTEGER(ob, &pv->Value.li);
        break;

    // @flag PT_MV_I8|A sequence of <o PyLARGE_INTEGER>
    case PT_MV_I8:
        MAKEB_MV(LARGE_INTEGER, pAllocMoreLinkBlock, pv->Value.MVli.lpli, pv->Value.MVli.cValues, PyWinObject_AsLARGE_INTEGER)

    // @flag PT_ERROR|An integer error code.
    case PT_ERROR:
        pv->Value.err = (SCODE)PyInt_AsLong(ob);
        break;

    // @flag PT_NULL|Anything!
    case PT_NULL:
        pv->Value.x = 0;
        break;

    default: {
        char buf[128];
        sprintf(buf, "Unsupported MAPI property type 0x%X", PROP_TYPE(pv->ulPropTag));
        PyErr_SetString(PyExc_TypeError, buf);
        ok = FALSE;
    }
    }
    ok = (ok && !PyErr_Occurred());
    return ok;
}
Пример #8
0
//*****************************************************************************
//
// @pymethod int|win32clipboard|SetClipboardText|Convienience function to
// call SetClipboardData with text.
// @comm You may pass a Unicode or string/bytes object to this function,
// but depending on the value of the 'format' param, it may be converted
// to the appropriate type for that param.
// @comm Many applications will want to call this function twice, with the
// same string specified but CF_UNICODETEXT specified the second.
static PyObject *
py_set_clipboard_text(PyObject* self, PyObject* args)
{
	int format = CF_TEXT;
	PyObject *obtext, *ret=NULL;
	if (!PyArg_ParseTuple(args, "O|i:SetClipboardText",
		&obtext,		// @pyparm str/unicode|text||The text to place on the clipboard.
		&format))		// @pyparm int|format|CF_TEXT|The clipboard format to use - must be CF_TEXT or CF_UNICODETEXT
		return NULL;

	const void *src = 0;
	DWORD cb = 0; // number of bytes *excluding* NULL
	size_t size_null = 0;
	if (format == CF_TEXT) {
		if (!PyWinObject_AsString(obtext, (char **)&src, FALSE, &cb))
			return NULL;
		size_null = sizeof(char);
	} else if (format == CF_UNICODETEXT) {
		DWORD cchars;
		if (!PyWinObject_AsWCHAR(obtext, (WCHAR **)&src, FALSE, &cchars))
			return NULL;
		cb = cchars * sizeof(WCHAR);
		size_null = sizeof(WCHAR);
	} else {
		return PyErr_Format(PyExc_ValueError, "Format arg must be one of CF_TEXT (%d) or CF_UNICODETEXT (%d) - got %d",
				    CF_TEXT, CF_UNICODETEXT, format);
	}

	HGLOBAL    hMem;
	BYTE *dest=NULL;

	hMem = GlobalAlloc(GHND, cb + size_null);
	if (hMem == NULL)
		PyWin_SetAPIError("GlobalAlloc");
	else{
		dest = (BYTE *)GlobalLock(hMem);
		memcpy(dest, src, cb);
		// whack the terminator on.
		memset(dest+cb, 0, size_null);
		GlobalUnlock(hMem);
		HANDLE data;
		Py_BEGIN_ALLOW_THREADS;
		data = SetClipboardData((UINT)format, hMem);
		Py_END_ALLOW_THREADS;
		if (!data)
			PyWin_SetAPIError("SetClipboardText");
		else
			ret = PyWinLong_FromHANDLE(data);
		}
	if (format == CF_TEXT)
		PyWinObject_FreeString((char *)src);
	else
		PyWinObject_FreeWCHAR((WCHAR *)src);
	return ret;
	// @pyseeapi SetClipboardData

	// @rdesc If the function succeeds, the return value is integer handle
	// of the data.<nl>
	// If the function fails, win32api.error is raised with the GetLastError
	// info.

}