xpr_bool_t WnetMgr::getNetDesc(const xpr_tchar_t *aServer, xpr_tchar_t *aDesc) { if (XPR_IS_NULL(aServer) || XPR_IS_NULL(aDesc)) return XPR_FALSE; xpr_bool_t sResult = XPR_FALSE; if (aServer[0] == XPR_STRING_LITERAL('\\') && aServer[1] == XPR_STRING_LITERAL('\\')) { NETRESOURCE sNetResource = {0}; sNetResource.dwScope = RESOURCE_GLOBALNET; sNetResource.dwType = RESOURCETYPE_ANY; sNetResource.dwDisplayType = RESOURCEDISPLAYTYPE_SERVER; sNetResource.dwUsage = RESOURCEUSAGE_CONTAINER; sNetResource.lpRemoteName = (xpr_tchar_t *)aServer; DWORD sBufferLen = sizeof(NETRESOURCE); xpr_byte_t *sBuffer = new xpr_byte_t[sBufferLen]; DWORD sWNetResult = NO_ERROR; try { xpr_tchar_t *sSystem = XPR_NULL; while ((sWNetResult = WNetGetResourceInformation(&sNetResource, sBuffer, &sBufferLen, &sSystem)) == ERROR_MORE_DATA) { XPR_SAFE_DELETE_ARRAY(sBuffer); sBuffer = new xpr_byte_t[sBufferLen]; } } catch (...) { } if (sWNetResult == NO_ERROR) { if (sNetResource.lpComment) { _tcscpy(aDesc, sNetResource.lpComment); sResult = XPR_TRUE; } } XPR_SAFE_DELETE_ARRAY(sBuffer); } return sResult; }
// @pymethod (<o PyNETRESOURCE>, str)|win32wnet|WNetGetResourceInformation|Finds the type and provider of a network resource // @rdesc Returns a NETRESOURCE and a string containing the trailing part of the remote path PyObject * PyWNetGetResourceInformation(PyObject *self, PyObject *args) { PyObject *NRT, *ret=NULL; //object placeholder for incoming NETRESOURCE object NETRESOURCE *nrin, *nrout=NULL; // buffer holds a NETRESOURCE struct and all its string members DWORD bufsize=sizeof(NETRESOURCE)+256; DWORD err; LPTSTR szFilePath = NULL; #ifdef Py_DEBUG bufsize=sizeof(NETRESOURCE); // make sure it loops thru again in debug mode #endif if (!PyArg_ParseTuple(args, "O!", &PyNETRESOURCEType, &NRT)) // @pyparm <o PyNETRESOURCE>|NetResource||Describes a network resource. lpRemoteName is required, dwType and lpProvider can be supplied if known return NULL; if (!PyWinObject_AsNETRESOURCE(NRT, &nrin, FALSE)) return NULL; while (1){ // function will not take NULL to return the buffer size, always pass in a valid buffer if (nrout) free(nrout); nrout=(NETRESOURCE *)malloc(bufsize); if (nrout==NULL) return PyErr_Format(PyExc_MemoryError,"Unable to allocate %d bytes", bufsize); Py_BEGIN_ALLOW_THREADS err = WNetGetResourceInformation(nrin, nrout, &bufsize, &szFilePath); Py_END_ALLOW_THREADS if (err == NO_ERROR){ ret=Py_BuildValue("NN", PyWinObject_FromNETRESOURCE(nrout), PyWinObject_FromTCHAR(szFilePath)); break; } else if (err!=ERROR_MORE_DATA){ ReturnNetError("WNetGetResourceInformation", err); break; } } if (nrout) free(nrout); return ret; }
xpr_bool_t WnetMgr::checkServer(const xpr_tchar_t *aServer) { if (XPR_IS_NULL(aServer)) return XPR_FALSE; xpr_tchar_t sServer[XPR_MAX_URL_LENGTH + 1] = {0}; _tcscpy(sServer, aServer); NETRESOURCE sNetResource = {0}; sNetResource.dwScope = RESOURCE_GLOBALNET; sNetResource.dwType = RESOURCETYPE_ANY; //sNetResource.dwDisplayType = RESOURCEDISPLAYTYPE_SERVER; //sNetResource.dwUsage = RESOURCEUSAGE_CONTAINER; sNetResource.lpRemoteName = sServer; DWORD sBufferLen = 16384; // 16kb xpr_byte_t *sBuffer = new xpr_byte_t[sBufferLen]; if (sBuffer == XPR_NULL) return XPR_FALSE; DWORD sWNetResult = NO_ERROR; try { xpr_tchar_t *sSystem = XPR_NULL; while ((sWNetResult = WNetGetResourceInformation(&sNetResource, sBuffer, &sBufferLen, &sSystem)) == ERROR_MORE_DATA) { XPR_SAFE_DELETE_ARRAY(sBuffer); sBuffer = new xpr_byte_t[sBufferLen]; } } catch (...) { } XPR_SAFE_DELETE_ARRAY(sBuffer); return (sWNetResult == NO_ERROR); }