Thread: [pywin32-checkins] pywin32/win32/src win32apimodule.cpp,1.78,1.79
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Roger U. <ru...@us...> - 2007-12-17 03:57:30
|
Update of /cvsroot/pywin32/pywin32/win32/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5713 Modified Files: win32apimodule.cpp Log Message: Add GetDllDirectory and SetDllDirectory Fix buffer overflow in GetModuleFilename for paths longer than MAX_PATH Index: win32apimodule.cpp =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/src/win32apimodule.cpp,v retrieving revision 1.78 retrieving revision 1.79 diff -C2 -d -r1.78 -r1.79 *** win32apimodule.cpp 18 Jun 2007 15:44:21 -0000 1.78 --- win32apimodule.cpp 17 Dec 2007 03:57:33 -0000 1.79 *************** *** 52,55 **** --- 52,59 ---- typedef BOOL (WINAPI *SetSystemFileCacheSizefunc)(SIZE_T,SIZE_T,DWORD); static SetSystemFileCacheSizefunc pfnSetSystemFileCacheSize = NULL; + typedef DWORD (WINAPI *GetDllDirectoryfunc)(DWORD,LPWSTR); + static GetDllDirectoryfunc pfnGetDllDirectory = NULL; + typedef BOOL (WINAPI *SetDllDirectoryfunc)(LPCWSTR); + static SetDllDirectoryfunc pfnSetDllDirectory = NULL; // from secur32.dll *************** *** 1415,1420 **** { HMODULE hMod; ! PyObject *obhMod; ! char buf[_MAX_PATH]; // @pyparm <o PyHANDLE>|hModule||Specifies the handle to the module. if (!PyArg_ParseTuple(args, "O:GetModuleFileName", &obhMod)) --- 1419,1425 ---- { HMODULE hMod; ! PyObject *obhMod, *ret=NULL; ! char *buf=NULL; ! DWORD bufsize, reqdsize=MAX_PATH; // @pyparm <o PyHANDLE>|hModule||Specifies the handle to the module. if (!PyArg_ParseTuple(args, "O:GetModuleFileName", &obhMod)) *************** *** 1423,1441 **** return NULL; // @pyseeapi GetModuleFileName ! PyW32_BEGIN_ALLOW_THREADS ! long rc = ::GetModuleFileName(hMod, buf, sizeof(buf)); ! PyW32_END_ALLOW_THREADS ! if (rc==0) ! return ReturnAPIError("GetModuleFileName"); ! return PyString_FromString(buf); } ! // @pymethod unicode|win32api|GetModuleFileNameW|Retrieves the unicode filename of the specified module. static PyObject * PyGetModuleFileNameW(PyObject * self, PyObject * args) { HMODULE hMod; ! PyObject *obhMod; ! wchar_t buf[_MAX_PATH]; // @pyparm <o PyHANDLE>|hModule||Specifies the handle to the module. if (!PyArg_ParseTuple(args, "O:GetModuleFileNameW", &obhMod)) --- 1428,1468 ---- return NULL; // @pyseeapi GetModuleFileName ! while (true){ ! if (buf) ! free(buf); ! buf=(char *)malloc(reqdsize); ! if (buf==NULL){ ! PyErr_NoMemory(); ! break; ! } ! ! bufsize=reqdsize; ! PyW32_BEGIN_ALLOW_THREADS ! reqdsize=GetModuleFileName(hMod, buf, bufsize); ! PyW32_END_ALLOW_THREADS ! ! if (reqdsize==0){ ! PyWin_SetAPIError("GetModuleFileName"); ! break; ! } ! if (reqdsize < bufsize){ ! ret=PyString_FromString(buf); ! break; ! } ! reqdsize++; ! } ! if (buf) ! free(buf); ! return ret; } ! // @pymethod <o PyUnicode>|win32api|GetModuleFileNameW|Retrieves the unicode filename of the specified module. static PyObject * PyGetModuleFileNameW(PyObject * self, PyObject * args) { HMODULE hMod; ! PyObject *obhMod, *ret=NULL; ! WCHAR *buf=NULL; ! DWORD bufsize, reqdsize=MAX_PATH; // @pyparm <o PyHANDLE>|hModule||Specifies the handle to the module. if (!PyArg_ParseTuple(args, "O:GetModuleFileNameW", &obhMod)) *************** *** 1444,1453 **** return NULL; // @pyseeapi GetModuleFileName ! PyW32_BEGIN_ALLOW_THREADS ! long rc = ::GetModuleFileNameW(hMod, buf, sizeof(buf)); ! PyW32_END_ALLOW_THREADS ! if (rc==0) ! return ReturnAPIError("GetModuleFileNameW"); ! return PyUnicode_FromUnicode(buf, wcslen(buf)); } --- 1471,1501 ---- return NULL; // @pyseeapi GetModuleFileName ! while (true){ ! if (buf) ! free(buf); ! buf=(WCHAR *)malloc(reqdsize * sizeof(WCHAR)); ! if (buf==NULL){ ! PyErr_NoMemory(); ! break; ! } ! ! bufsize=reqdsize; ! PyW32_BEGIN_ALLOW_THREADS ! reqdsize=GetModuleFileNameW(hMod, buf, bufsize); ! PyW32_END_ALLOW_THREADS ! ! if (reqdsize==0){ ! PyWin_SetAPIError("GetModuleFileNameW"); ! break; ! } ! if (reqdsize < bufsize){ ! ret=PyUnicode_FromUnicode(buf, reqdsize); ! break; ! } ! reqdsize++; ! } ! if (buf) ! free(buf); ! return ret; } *************** *** 1689,1692 **** --- 1737,1793 ---- } + // @pymethod <o PyUnicode>|win32api|GetDllDirectory|Returns the DLL search path + // @pyseeapi GetDllDirectory + static PyObject *PyGetDllDirectory(PyObject * self, PyObject * args) + { + CHECK_PFN(GetDllDirectory); + WCHAR *dirs=NULL; + DWORD bufsize=0, reqdsize=256; + PyObject *ret=NULL; + while (true){ + if (dirs) + free(dirs); + dirs=(WCHAR *)malloc(reqdsize * sizeof(WCHAR)); + if (dirs==NULL){ + PyErr_NoMemory(); + break; + } + bufsize=reqdsize; + reqdsize=(*pfnGetDllDirectory)(bufsize, dirs); + if (reqdsize==0){ + PyWin_SetAPIError("GetDllDirectory"); + break; + } + if (reqdsize <= bufsize){ + ret=PyWinObject_FromWCHAR(dirs, reqdsize); + break; + } + } + if (dirs) + free(dirs); + return ret; + } + + // @pymethod |win32api|SetDllDirectory|Modifies the application-specific DLL search path + // @pyseeapi SetDllDirectory + static PyObject *PySetDllDirectory(PyObject * self, PyObject * args) + { + CHECK_PFN(SetDllDirectory); + PyObject *obdirname; + WCHAR *dirname; + + // @pyparm <o PyUnicode>|PathName||Directory to be added to search path, can be None to restore defaults + if (!PyArg_ParseTuple(args, "O:SetDllDirectory", &obdirname)) + return NULL; + if (!PyWinObject_AsWCHAR(obdirname, &dirname, TRUE)) + return NULL; + BOOL bsuccess=(*pfnSetDllDirectory)(dirname); + PyWinObject_FreeWCHAR(dirname); + if (!bsuccess) + return PyWin_SetAPIError("SetDllDirectory"); + Py_INCREF(Py_None); + return Py_None; + } + // @pymethod int/string|win32api|GetProfileVal|Retrieves entries from a windows INI file. This method encapsulates GetProfileString, GetProfileInt, GetPrivateProfileString and GetPrivateProfileInt. static PyObject * *************** *** 5544,5547 **** --- 5645,5649 ---- {"GetDiskFreeSpace", PyGetDiskFreeSpace, 1}, // @pymeth GetDiskFreeSpace|Retrieves information about a disk. {"GetDiskFreeSpaceEx", PyGetDiskFreeSpaceEx, 1}, // @pymeth GetDiskFreeSpaceEx|Retrieves information about a disk. + {"GetDllDirectory", PyGetDllDirectory, METH_NOARGS}, // @pymeth GetDllDirectory|Retrieves the DLL search path {"GetDomainName", PyGetDomainName, 1}, // @pymeth GetDomainName|Returns the current domain name {"GetEnvironmentVariable", PyGetEnvironmentVariable, 1}, // @pymeth GetEnvironmentVariable|Retrieves the value of an environment variable. *************** *** 5651,5654 **** --- 5753,5757 ---- {"SetConsoleTitle", PySetConsoleTitle, 1}, // @pymeth SetConsoleTitle|Sets the title for the current console. {"SetCursorPos", PySetCursorPos,1}, // @pymeth SetCursorPos|The SetCursorPos function moves the cursor to the specified screen coordinates. + {"SetDllDirectory", PySetDllDirectory, 1}, // @pymeth SetDllDirectory|Modifies the application-specific DLL search path {"SetErrorMode", PySetErrorMode, 1}, // @pymeth SetErrorMode|Controls whether the system will handle the specified types of serious errors, or whether the process will handle them. {"SetFileAttributes", PySetFileAttributes,1}, // @pymeth SetFileAttributes|Sets the named file's attributes. *************** *** 5777,5780 **** --- 5880,5885 ---- pfnGetSystemFileCacheSize=(GetSystemFileCacheSizefunc)GetProcAddress(hmodule,"GetSystemFileCacheSize"); pfnSetSystemFileCacheSize=(SetSystemFileCacheSizefunc)GetProcAddress(hmodule,"SetSystemFileCacheSize"); + pfnGetDllDirectory=(GetDllDirectoryfunc)GetProcAddress(hmodule,"GetDllDirectoryW"); + pfnSetDllDirectory=(SetDllDirectoryfunc)GetProcAddress(hmodule,"SetDllDirectoryW"); } |