[pywin32-checkins] pywin32/com/win32com/src ErrorUtils.cpp, 1.32, 1.33
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
|
From: Mark H. <mha...@us...> - 2009-02-03 03:52:32
|
Update of /cvsroot/pywin32/pywin32/com/win32com/src In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv13276/com/win32com/src Modified Files: ErrorUtils.cpp Log Message: make PyWin_String_AsBstr a private function in the 1 place its used Index: ErrorUtils.cpp =================================================================== RCS file: /cvsroot/pywin32/pywin32/com/win32com/src/ErrorUtils.cpp,v retrieving revision 1.32 retrieving revision 1.33 diff -C2 -d -r1.32 -r1.33 *** ErrorUtils.cpp 26 Nov 2008 01:18:31 -0000 1.32 --- ErrorUtils.cpp 3 Feb 2009 03:52:29 -0000 1.33 *************** *** 25,28 **** --- 25,57 ---- PYCOM_EXPORT void PyCom_StreamMessage(const char *msg); + // Private helper to convert a "char *" to a BSTR for use in the error + // structures. + BSTR PyWin_String_AsBstr(const char *value) + { + if (value==NULL || *value=='\0') + return SysAllocStringLen(L"", 0); + /* use MultiByteToWideChar() as a "good" strlen() */ + /* NOTE: this will include the null-term in the length */ + int cchWideChar = MultiByteToWideChar(CP_ACP, 0, value, -1, NULL, 0); + + /* alloc a temporary conversion buffer, but dont use alloca, as super + large strings will blow our stack */ + LPWSTR wstr = (LPWSTR)malloc(cchWideChar * sizeof(WCHAR)); + if (wstr==NULL) { + PyErr_SetString(PyExc_MemoryError, "Not enough memory to allocate wide string buffer."); + return NULL; + } + + /* convert the input into the temporary buffer */ + MultiByteToWideChar(CP_ACP, 0, value, -1, wstr, cchWideChar); + + /* don't place the null-term into the BSTR */ + BSTR ret = SysAllocStringLen(wstr, cchWideChar - 1); + if (ret==NULL) + PyErr_SetString(PyExc_MemoryError, "allocating BSTR"); + free(wstr); + return ret; + } + //////////////////////////////////////////////////////////////////////// // |