std::wstring CDeskBand::ConvertToUNC(std::wstring sPath) { WCHAR temp; DWORD bufsize = 0; std::wstring sRet = sPath; //Call WNetGetUniversalName using UNIVERSAL_NAME_INFO_LEVEL option if (WNetGetUniversalName(sPath.c_str(), UNIVERSAL_NAME_INFO_LEVEL, (LPVOID) &temp, &bufsize) == ERROR_MORE_DATA) { // now we have the size required to hold the UNC path WCHAR * buf = new WCHAR[bufsize+1]; UNIVERSAL_NAME_INFO * puni = (UNIVERSAL_NAME_INFO *)buf; if (WNetGetUniversalName(sPath.c_str(), UNIVERSAL_NAME_INFO_LEVEL, (LPVOID) puni, &bufsize) == NO_ERROR) { sRet = std::wstring(puni->lpUniversalName); } delete [] buf; } return sRet; }
void Explorerplusplus::OnTreeViewCopyUniversalPaths(void) { HTREEITEM hItem; LPITEMIDLIST pidl; TCHAR szFullFileName[MAX_PATH]; UNIVERSAL_NAME_INFO uni; DWORD dwBufferSize; DWORD dwRet; hItem = TreeView_GetSelection(m_hTreeView); if(hItem != NULL) { pidl = m_pMyTreeView->BuildPath(hItem); GetDisplayName(pidl,szFullFileName,SIZEOF_ARRAY(szFullFileName),SHGDN_FORPARSING); dwBufferSize = sizeof(uni); dwRet = WNetGetUniversalName(szFullFileName,UNIVERSAL_NAME_INFO_LEVEL, (void **)&uni,&dwBufferSize); if(dwRet == NO_ERROR) CopyTextToClipboard(uni.lpUniversalName); else CopyTextToClipboard(szFullFileName); CoTaskMemFree(pidl); } }
string ConvertNameToUNC(string_view const Object) { auto strFileName = ConvertNameToFull(Object); // Посмотрим на тип файловой системы string strFileSystemName; os::fs::GetVolumeInformation(GetPathRoot(strFileName), nullptr, nullptr, nullptr, nullptr, &strFileSystemName); DWORD uniSize = 1024; block_ptr<UNIVERSAL_NAME_INFO> uni(uniSize); // применяем WNetGetUniversalName для чего угодно, только не для Novell`а if (!equal_icase(strFileSystemName, L"NWFS"sv)) { switch (WNetGetUniversalName(strFileName.c_str(), UNIVERSAL_NAME_INFO_LEVEL, uni.get(), &uniSize)) { case NO_ERROR: strFileName = uni->lpUniversalName; break; case ERROR_MORE_DATA: uni.reset(uniSize); if (WNetGetUniversalName(strFileName.c_str(),UNIVERSAL_NAME_INFO_LEVEL,uni.get(),&uniSize)==NO_ERROR) strFileName = uni->lpUniversalName; break; } } else if (strFileName.size() > 1 && strFileName[1] == L':') { // BugZ#449 - Неверная работа CtrlAltF с ресурсами Novell DS // Здесь, если не получилось получить UniversalName и если это // мапленный диск - получаем как для меню выбора дисков string strTemp; if (DriveLocalToRemoteName(DRIVE_UNKNOWN,strFileName[0],strTemp)) { const auto SlashPos = FindSlash(strFileName); if (SlashPos != string::npos) path::append(strTemp, string_view(strFileName).substr(SlashPos + 1)); strFileName = strTemp; } } return ConvertNameToReal(strFileName); }
// @pymethod string/tuple|win32wnet|WNetGetUniversalName|Takes a drive-based path for a network resource and returns an information structure that contains a more universal form of the name. static PyObject * PyWNetGetUniversalName(PyObject *self, PyObject *args) { int level = UNIVERSAL_NAME_INFO_LEVEL; TCHAR *szLocalPath = NULL; PyObject *obLocalPath; void *buf = NULL; DWORD length = 0; PyObject *ret = NULL; DWORD errcode; if (!PyArg_ParseTuple(args, "O|i:WNetGetUniversalName", &obLocalPath, &level)) return NULL; if (!PyWinObject_AsTCHAR(obLocalPath, &szLocalPath, FALSE)) return NULL; // @pyparm string|localPath||A string that is a drive-based path for a network resource. // <nl>For example, if drive H has been mapped to a network drive share, and the network // resource of interest is a file named SAMPLE.DOC in the directory \WIN32\EXAMPLES on // that share, the drive-based path is H:\WIN32\EXAMPLES\SAMPLE.DOC. // @pyparm int|infoLevel|UNIVERSAL_NAME_INFO_LEVEL|Specifies the type of structure that the function stores in the buffer pointed to by the lpBuffer parameter. // This parameter can be one of the following values. // @flagh Value|Meaning // @flag UNIVERSAL_NAME_INFO_LEVEL (=1)|The function returns a simple string with the UNC name. // @flag REMOTE_NAME_INFO_LEVEL (=2)|The function returns a tuple based in the Win32 REMOTE_NAME_INFO data structure. // @rdesc If the infoLevel parameter is REMOTE_NAME_INFO_LEVEL, the result is a tuple of 3 strings: (UNCName, connectionName, remainingPath) // First get the buffer size. { Py_BEGIN_ALLOW_THREADS char temp_buf[] = ""; // doesnt appear to like NULL!! errcode = WNetGetUniversalName( szLocalPath, level, &temp_buf, &length); Py_END_ALLOW_THREADS } if (errcode != ERROR_MORE_DATA || length == 0) { ReturnNetError("WNetGetUniversalName (for buffer size)", errcode); goto done; } buf = malloc(length); if (buf==NULL) goto done; errcode = WNetGetUniversalName( szLocalPath, level, buf, &length); if (errcode != 0) { ReturnNetError("WNetGetUniversalName", errcode); goto done; } switch (level) { case UNIVERSAL_NAME_INFO_LEVEL: ret = PyWinObject_FromTCHAR( ((UNIVERSAL_NAME_INFO *)buf)->lpUniversalName ); break; case REMOTE_NAME_INFO_LEVEL: { REMOTE_NAME_INFO *r = (REMOTE_NAME_INFO *)buf; ret = PyTuple_New(3); if (ret==NULL) goto done; PyTuple_SET_ITEM(ret, 0, PyWinObject_FromTCHAR( r->lpUniversalName) ); PyTuple_SET_ITEM(ret, 1, PyWinObject_FromTCHAR( r->lpConnectionName) ); PyTuple_SET_ITEM(ret, 2, PyWinObject_FromTCHAR( r->lpRemainingPath) ); break; } default: PyErr_SetString(PyExc_TypeError, "Unsupported infoLevel"); } done: PyWinObject_FreeTCHAR(szLocalPath); if (buf) free(buf); return ret; }