int DetectFreeDiskSymbol() { int i = 0, iRet = 0; DWORD dwLength = 20, dwRet = 0; char szAllLocalDisk[12][3]={"O:","P:","Q:","R:","S:","T:","U:","V:", "W:","X:","Y:","Z:"}; char szRemoteMachine[20] = {0}; for(i = 0;i < 12;i++) { dwRet = WNetGetConnection(szAllLocalDisk[i],szRemoteMachine,&dwLength); if(!((dwRet == 0 )||(dwRet == 2250))) { iRet = -1; break; } if(stricmp(szRemoteMachine,"") == 0) { iRet = i+1; break; } dwLength = 20; strcpy(szRemoteMachine,""); } return iRet; }
static void playlist_loaddir_get_driveinfos(void) { DWORD drivesbits,i,vlen; loaddir_clear_driveinfos(0); drivesbits=GetLogicalDrives(); if(!drivesbits) return; i=LOADDIR_FIRSTDRIVE; drivesbits>>=LOADDIR_FIRSTDRIVE; do{ if(drivesbits&1){ mpxplay_drivemap_info_t *di=&drivemap_infos[i]; di->type=pds_chkdrive(i); loaddir_build_drivename(i,di->drivename); if((i>=2) || (di->type!=DRIVE_TYPE_FLOPPY)){ if(di->type==DRIVE_TYPE_NETWORK){ vlen=LOADDIR_DRIVE_VOLUMENAME_LEN; WNetGetConnection(di->drivename,&di->volumename[0],&vlen); } if((di->type!=DRIVE_TYPE_NETWORK) || !di->volumename[0]) pds_drive_getvolumelabel(i,&di->volumename[0],LOADDIR_DRIVE_VOLUMENAME_LEN); } if(loaddir_lastvaliddrive<i) loaddir_lastvaliddrive=i; } drivesbits>>=1; i++; }while(i<LOADDIR_MAX_LOCAL_DRIVES); loaddir_diskdrives_mount_localdisks(); }
// @pymethod string|win32wnet|WNetGetConnection|Retrieves the name of the network resource associated with a local device. static PyObject * PyWNetGetConnection(PyObject *self, PyObject *args) { PyObject *ret = NULL; PyObject *obConnection = Py_None; DWORD length = 0; DWORD errcode; TCHAR *szConnection = NULL; TCHAR *buf = NULL; // @pyparm string|connection|None|A string that is a drive-based path for a network resource. // 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. if (!PyArg_ParseTuple(args, "|O", &obConnection)) return NULL; if (!PyWinObject_AsTCHAR(obConnection, &szConnection, TRUE)) goto done; // get the buffer size { Py_BEGIN_ALLOW_THREADS errcode=WNetGetConnection(szConnection, NULL, &length); Py_END_ALLOW_THREADS } if (length==0) { ReturnNetError("WNetGetConnection", errcode); goto done; } buf = (TCHAR *)malloc( sizeof( TCHAR) * length); if (buf == NULL){ PyErr_Format(PyExc_MemoryError, "Unable to allocate %d bytes", sizeof(TCHAR)*length); goto done; } Py_BEGIN_ALLOW_THREADS errcode = WNetGetConnection(szConnection, buf, &length); Py_END_ALLOW_THREADS if (0 != errcode) { ReturnNetError("WNetGetConnection", errcode); goto done; } // length includes the NULL - drop it (safely!) ret = PyWinObject_FromTCHAR(buf, (length > 0) ? length-1 : 0); done: PyWinObject_FreeTCHAR(szConnection); if (buf) free(buf); return ret; }
bool CVolumeDescriptionEnumeratorThread::GetDrive(const wxChar* pDrive, const int len) { wxChar* pVolume = new wxChar[len + 1]; wxStrcpy(pVolume, pDrive); if (pVolume[len - 1] == '\\') pVolume[len - 1] = 0; if (!*pVolume) { delete [] pVolume; return false; } // Check if it is a network share wxChar *share_name = new wxChar[512]; DWORD dwSize = 511; if (!WNetGetConnection(pVolume, share_name, &dwSize)) { m_crit_section.Enter(); t_VolumeInfoInternal volumeInfo; volumeInfo.pVolume = pVolume; volumeInfo.pVolumeName = share_name; m_volumeInfo.push_back(volumeInfo); m_crit_section.Leave(); pDrive += len + 1; return true; } else delete [] share_name; // Get the label of the drive wxChar* pVolumeName = new wxChar[501]; int oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS); BOOL res = GetVolumeInformation(pDrive, pVolumeName, 500, 0, 0, 0, 0, 0); SetErrorMode(oldErrorMode); if (res && pVolumeName[0]) { m_crit_section.Enter(); t_VolumeInfoInternal volumeInfo; volumeInfo.pVolume = pVolume; volumeInfo.pVolumeName = pVolumeName; m_volumeInfo.push_back(volumeInfo); m_crit_section.Leave(); return true; } delete [] pVolumeName; delete [] pVolume; return false; }
static HB_BOOL hb_IsNetShared( const char * szLocalDevice ) { TCHAR lpRemoteDevice[ HB_PATH_MAX ]; LPCTSTR lpLocalDevice; LPTSTR lpFree; DWORD dwLen = HB_SIZEOFARRAY( lpRemoteDevice ); DWORD dwResult; lpLocalDevice = HB_FSNAMECONV( szLocalDevice, &lpFree ); hb_vmUnlock(); dwResult = WNetGetConnection( lpLocalDevice, lpRemoteDevice, &dwLen ); hb_vmLock(); if( lpFree ) hb_xfree( lpFree ); return dwResult == NO_ERROR; }
BOOL CSubmountsDlg::OnInitDialog() { CDialog::OnInitDialog(); if (m_bAddOnlyMode) { TCHAR szRemoteName[MAX_PATH]; ULONG nBufSize = sizeof(szRemoteName); if (WNetGetConnection(m_strAddOnlyPath.Left(2), szRemoteName, &nBufSize) == ERROR_SUCCESS) { CString strAfsShare(szRemoteName); int nSlashPos = strAfsShare.ReverseFind('\\'); if (nSlashPos > -1) { strAfsShare = strAfsShare.Mid(nSlashPos + 1); // Get the submount info for this share name CSubmountInfo *pInfo = ReadSubmtInfo(strAfsShare); CString strSharePath; if (pInfo != 0) { ASSERT_VALID(pInfo); strSharePath = pInfo->GetPathName(); } m_strAddOnlyPath = strSharePath + m_strAddOnlyPath.Mid(2); } } OnAdd(); OnOk(); return TRUE; } if (!FillSubmtList()) { // ShowMessageBox(IDS_GET_CELL_LIST_ERROR); // EndDialog(0); // return TRUE; } return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
HRESULT PathConvertMappedDriveToUNC(PCWSTR pszPath, PWSTR *ppszUNC) { *ppszUNC = NULL; HRESULT hr = E_FAIL; #if 0 // build simple root "C:" (with no slash) since WNetGetConnection does not deal with these WCHAR szDrive[3] = {pszPath[0], pszPath[1], 0}; if (DRIVE_REMOTE == GetDriveType(szDrive)) { WCHAR szRemoteName[MAX_PATH]; DWORD dwLen = ARRAYSIZE(szRemoteName); if (NO_ERROR == WNetGetConnection(szDrive, szRemoteName, &dwLen)) { // +3 to skip to "\foo" in "C:\foo" WCHAR szUNC[MAX_PATH]; PathCombine(szUNC, szRemoteName, pszPath + 3); hr = SHStrDup(szUNC, ppszUNC); } } #else // alternate implementation struct { UNIVERSAL_NAME_INFO uni; // a pointer WCHAR szBuf[MAX_PATH]; // buffer for path, make bigger in the future } uniBuffer; DWORD cbBuffer = sizeof(uniBuffer); // test this with disconnected drive letters, might fail for them hr = HRESULT_FROM_WIN32(WNetGetUniversalNameW(pszPath, UNIVERSAL_NAME_INFO_LEVEL, &uniBuffer, &cbBuffer)); if (SUCCEEDED(hr)) { PathRemoveBackslash(uniBuffer.uni.lpUniversalName); // some cases return trailing slash hr = SHStrDup(uniBuffer.uni.lpUniversalName, ppszUNC); } #endif return hr; }
bool WinReviveNetworkResource(uint16 * _wfileName) { bool result = false; HWND windowHandle = null; NETRESOURCE nr = { 0 }; nr.dwType = RESOURCETYPE_DISK; nr.lpRemoteName = _wfileName; if(_wfileName[0] != '\\' || _wfileName[1] == '\\') { uint16 volumePathName[MAX_LOCATION]; if(GetVolumePathName(_wfileName, volumePathName, MAX_LOCATION)) { uint16 remoteName[MAX_LOCATION]; DWORD size = MAX_LOCATION; volumePathName[wcslen(volumePathName)-1] = 0; if(WNetGetConnection(volumePathName, remoteName, &size) == ERROR_CONNECTION_UNAVAIL) { nr.lpRemoteName = remoteName; nr.lpLocalName = volumePathName; } else return false; } else return false; } EnumThreadWindows(GetCurrentThreadId(), EnumThreadWindowsProc, (LPARAM)&windowHandle); if(!windowHandle) { EnumWindows(EnumThreadWindowsProc, (LPARAM)&windowHandle); } if(WNetAddConnection3(windowHandle, &nr, null, null, CONNECT_INTERACTIVE|CONNECT_PROMPT) == NO_ERROR) result = true; return result; }
BOOL GetDriveInfo( LPDRIVE_INFO di ) /*++ Routine Description: Fills in a given DRIVE_INFO object based on the DriveLetter member. Arguments: Required: di.DriveLetter - the root directory of the drive (e.g. c:\) Return Value: LPDRIVE_INFO - Returns a pointer to a DRIVE_INFO object with information about the supplied drive completed. --*/ { UINT OldErrorMode; DWORD SizeOfRemoteNameBuffer; HCURSOR hSaveCursor; hSaveCursor = SetCursor ( LoadCursor ( NULL, IDC_WAIT ) ); DbgHandleAssert( hSaveCursor ); // // Get the type of this drive. // di->DriveType = GetDriveType( di->DriveLetter ); // // If this is a network drive, get the share its connected to. // if( di->DriveType == DRIVE_REMOTE ) { DWORD WNetError; // // WinNet APIs want the drive name w/o a trailing slash so use the // remove it temporarily // di->DriveLetter[ 2 ] = TEXT( '\0' ); SizeOfRemoteNameBuffer = sizeof( di->RemoteNameBuffer ); WNetError = WNetGetConnection( di->DriveLetter, di->RemoteNameBuffer, &SizeOfRemoteNameBuffer ); di->DriveLetter[ 2 ] = TEXT( '\\' ); DbgAssert( ( WNetError == NO_ERROR ) || ( WNetError == ERROR_VC_DISCONNECTED )); } // // Disable pop-ups (especially if there is no media in the // removable drives.) // OldErrorMode = SetErrorMode( SEM_FAILCRITICALERRORS ); // // Get the space statistics for this drive. // di->ValidDetails = GetDiskFreeSpace( di->DriveLetter, &di->SectorsPerCluster, &di->BytesPerSector, &di->FreeClusters, &di->Clusters ); if( di->ValidDetails == FALSE ) { // // Reenable pop-ups. // SetErrorMode( OldErrorMode ); SetCursor ( hSaveCursor ); return FALSE; } // // Get information about the volume for this drive. // di->ValidDetails = GetVolumeInformation( di->DriveLetter, di->VolumeNameBuffer, sizeof( di->VolumeNameBuffer ), &di->VolumeSerialNumber, &di->MaximumComponentLength, &di->FileSystemFlags, di->FileSystemNameBuffer, sizeof( di->FileSystemNameBuffer ) ); SetErrorMode (OldErrorMode); SetCursor ( hSaveCursor ); return di->ValidDetails; }
BOOL FillTreeViewWithDrives( HWND hWnd, HWND hTreeView, int nStyle ) /*++ Routine Description: FillTreeViewWithDrives - fills tree view with list of drives Arguments: hWnd - Handle of Dialog hTreeView - Handle of TreeView nStyle - either IDC_RADIO_TYPES or IDC_RADIO_LETTERS Return Value: BOOL - TRUE if tree is built successfully, FALSE otherwise. --*/ { TCHAR LogicalDrives[ MAX_PATH ]; LPTSTR Drive; DWORD Chars; HTREEITEM hParent; HTREEITEM hMyComputer; TV_INSERTSTRUCT tvis; HCURSOR hSaveCursor; UINT OldErrorMode; static UINT nDisplayStyle; // // Set the style iff we get a valid style // if ((nStyle == IDC_PUSH_DRIVE_TYPE) || (nStyle == IDC_PUSH_DRIVE_LETTER) ){ nDisplayStyle = nStyle; } // // Set the pointer to an hourglass - this could take a while // hSaveCursor = SetCursor ( LoadCursor ( NULL, IDC_WAIT ) ) ; DbgHandleAssert( hSaveCursor ) ; //initialize the basic TV structures tvis.hParent = TVI_ROOT; tvis.hInsertAfter = TVI_LAST; tvis.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM; // create the ROOT entry tvis.item.pszText = (LPTSTR) GetString ( IDS_MY_COMPUTER ); tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_MY_COMPUTER; tvis.item.lParam = 0; //clear the treeview TreeView_DeleteAllItems( hTreeView ); //add a root entry hMyComputer = AddTreeViewItem(hTreeView, &tvis); // // Retrieve the logical drive strings from the system. // Chars = GetLogicalDriveStrings( sizeof( LogicalDrives ), LogicalDrives ); DbgAssert(( Chars != 0 ) && ( Chars <= sizeof( LogicalDrives ))); Drive = LogicalDrives; // // Disable pop-ups (especially if there is no media in the // removable drives.) // OldErrorMode = SetErrorMode( SEM_FAILCRITICALERRORS ); // // For each logical drive, create a DRIVE_INFO object and display // its name and type. // while( *Drive ) { LPDRIVE_INFO DriveInfo; TCHAR VolumeNameBuffer[512]; TCHAR FileSystemNameBuffer[512]; TCHAR DriveLetter[3]; DriveLetter[ 0 ] = Drive [ 0 ]; DriveLetter[ 1 ] = Drive [ 1 ]; DriveLetter[ 2 ] = TEXT( '\0'); switch( GetDriveType( Drive ) ) { case 1: tvis.item.pszText =(LPTSTR) GetString ( IDS_DRIVE_NO_ROOT_DIR ); tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_FOLDER_OPEN; break; case DRIVE_REMOVABLE: tvis.item.pszText =(LPTSTR) GetString ( IDS_DRIVE_REMOVABLE ); tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_DRIVE_REMOVABLE; break; case DRIVE_FIXED: tvis.item.pszText =(LPTSTR) GetString ( IDS_DRIVE_FIXED ); tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_DRIVE_HARD; break; case DRIVE_REMOTE: tvis.item.pszText =(LPTSTR) GetString ( IDS_DRIVE_REMOTE ); tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_NET_CARD; break; case DRIVE_CDROM: tvis.item.pszText =(LPTSTR) GetString ( IDS_DRIVE_CDROM ); tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_DRIVE_CDROM; break; case DRIVE_RAMDISK: tvis.item.pszText =(LPTSTR) GetString ( IDS_DRIVE_RAMDISK ); tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_DRIVE_RAM; break; case 0: tvis.item.pszText =(LPTSTR) GetString ( IDS_DRIVE_UNKNOWN ); tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_FOLDER_OPEN; break; } tvis.hParent = hMyComputer; tvis.hInsertAfter = TVI_LAST; if ( nDisplayStyle == IDC_PUSH_DRIVE_TYPE ) { tvis.item.lParam = 0; hParent = FindOrAddTreeViewItem ( hTreeView, &tvis); } else { hParent = hMyComputer; } tvis.hParent = hParent; tvis.item.pszText = DriveLetter; tvis.item.lParam = (DWORD) CreateDriveInfoStructure(); ((LPDRIVE_INFO) tvis.item.lParam)->DriveLetter[0]= Drive[0]; ((LPDRIVE_INFO) tvis.item.lParam)->DriveLetter[1]= Drive[1]; ((LPDRIVE_INFO) tvis.item.lParam)->DriveLetter[2]= Drive[2]; switch( GetDriveType( Drive ) ) { case 1: tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_DRIVE_HARD; break; case DRIVE_REMOVABLE: tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_DRIVE_REMOVABLE; break; case DRIVE_FIXED: tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_DRIVE_HARD; break; case DRIVE_REMOTE: { // Get the remote name DWORD WNetError; TCHAR RemoteNameBuffer[512]; DWORD SizeOfRemoteNameBuffer; NETRESOURCE NetResource; TCHAR ResourceInformationBuffer[ 1024 ]; DWORD cchResourceInformationBuffer; TCHAR SystemBuffer[ MAX_PATH ]; TCHAR DisplayName[ MAX_PATH ]; SizeOfRemoteNameBuffer = sizeof( RemoteNameBuffer ); WNetError = WNetGetConnection( DriveLetter, RemoteNameBuffer, &SizeOfRemoteNameBuffer ); wsprintf(DisplayName, L"%s %s", tvis.item.pszText, RemoteNameBuffer); tvis.item.pszText = DisplayName; // // Call code to determine which tiny icon to display // /* NetResource.dwType = RESOURCETYPE_DISK; NetResource.lpProvider = NULL; WNetGetResourceInformation( &NetResource, (void *) &ResourceInformationBuffer, &cchResourceInformationBuffer, &SystemBuffer); */ // BUGBUG: subst hits this error //if (WNetError == NO_ERROR) { tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_DRIVE_NET; //} //else //{ // tvis.item.iImage = // tvis.item.iSelectedImage = IMAGE_DRIVE_NET_X; // } break; } case DRIVE_CDROM: tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_DRIVE_CDROM; break; case DRIVE_RAMDISK: tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_DRIVE_RAM; break; case 0: tvis.item.iImage = tvis.item.iSelectedImage = IMAGE_DRIVE_HARD; break; } AddTreeViewItem (hTreeView, &tvis); // // Examine the next logical drive. // Drive += _tcslen( Drive ) + 1; } TreeView_Expand(GetDlgItem(hWnd, IDC_TV_DRIVE_LIST), hMyComputer, TVE_EXPAND); // // Lengthy operation completed. Restore Cursor, and error mode // SetCursor ( hSaveCursor ) ; SetErrorMode ( OldErrorMode ) ; return(TRUE); }
static void fillComboBox(HWND hWnd) { HWND hWndCB = GetDlgItem(hWnd, ID_COMBO_DIR); HWND hWndTempLB = GetDlgItem(hWnd, ID_LISTTEMP_DIR); if(hWndCB == NULL) return; ComboBox_ResetContent(hWndCB); ListBox_ResetContent(hWndTempLB); ListBox_Dir(hWndTempLB, DDL_DRIVES|DDL_EXCLUSIVE, (LPSTR)"*"); int iDriveCount = ListBox_GetCount(hWndTempLB); int iCurDrive=_getdrive() - 1; char szDrive[16]; char szItem[80]; for (int i = 0; i < iDriveCount; i++) { ListBox_GetText(hWndTempLB, i, szDrive); CharLower(szDrive); int iDrive = szDrive[2] - 'a'; char szRoot[16]; sprintf(szRoot, "%c:\\", szDrive[2]); int iType = DriveType(iDrive); if(iType < 2) continue; //Start the item string with the drive letter, colon, and two spaces sprintf(szItem, "%c%s", szDrive[2], ": "); if((iType == DRIVE_FIXED) || (iType == DRIVE_RAMDISK)) { // get volume ID char szVolumeID[80]; DWORD dwMaxLength; DWORD dwSysFlags; GetVolumeInformation(szRoot, // address of root directory of the file system szVolumeID, // address of name of the volume sizeof(szVolumeID), // length of lpVolumeNameBuffer NULL, // address of volume serial number &dwMaxLength, // address of system's maximum filename length &dwSysFlags, // address of file system flags NULL, // address of name of file system NULL); // length of lpFileSystemNameBuffer CharLower(szVolumeID); lstrcat(szItem, szVolumeID); } //For network drives, go grab the \\server\share for it. if(DRIVE_REMOTE == iType) { char szNet[64]; szNet[0] = '\0'; DWORD dwSizeOfszNet = sizeof(szNet); sprintf(szDrive, "%c:", szDrive[2]); CharUpper(szDrive); WNetGetConnection(szDrive, szNet, &dwSizeOfszNet); CharLower(szNet); lstrcat(szItem, szNet); } int index = ComboBox_AddString(hWndCB, szItem); ComboBox_SetItemData(hWndCB, index, MAKELONG(iDrive, iType)); if(iDrive == iCurDrive) ComboBox_SetCurSel(hWndCB, index); if(szUNCRoot[0] != '\0') ComboBox_SetCurSel(hWndCB, -1); } }
BOOL GetDriveSubmount (TCHAR chDrive, LPTSTR pszSubmountNow) { BOOL isWinNT = IsWindowsNT(); TCHAR szDrive[] = TEXT("*:"); szDrive[0] = chDrive; TCHAR szMapping[ _MAX_PATH ] = TEXT(""); if (isWinNT && !QueryDosDevice (szDrive, szMapping, MAX_PATH)) return FALSE; LPTSTR pszSubmount = szMapping; TCHAR szNetBiosName[32]; memset(szNetBiosName, '\0', sizeof(szNetBiosName)); GetClientNetbiosName(szNetBiosName); _tcscat(szNetBiosName, TEXT("\\")); if (isWinNT) { // Now if this is an AFS network drive mapping, {szMapping} will be: // // \Device\LanmanRedirector\<Drive>:\<netbiosname>\submount // // on Windows NT. On Windows 2000, it will be: // // \Device\LanmanRedirector\;<Drive>:0\<netbiosname>\submount // // (This is presumably to support multiple drive mappings with // Terminal Server). // // on Windows XP and 2003, it will be : // \Device\LanmanRedirector\;<Drive>:<AuthID>\<netbiosname>\submount // // where : <Drive> : DOS drive letter // <AuthID>: Authentication ID, 16 char hex. // <netbiosname>: Netbios name of server // BOOL b; #ifndef AFSIFS b = _tcsnicmp(szMapping, cszLANMANDEVICE, _tcslen(cszLANMANDEVICE)); #else const TCHAR ker_sub_path[] = "\\Device\\afsrdr\\"; b = _tcsnicmp(szMapping, ker_sub_path, _tcslen(ker_sub_path)); #endif if (b) return FALSE; #ifndef AFSIFS pszSubmount = &szMapping[ _tcslen(cszLANMANDEVICE) ]; #else pszSubmount = &szMapping[ _tcslen(ker_sub_path) ]; #endif #ifdef AFSIFS if (*(pszSubmount) < '0' || *(pszSubmount) > '9') return FALSE; ++pszSubmount; #else if (IsWindows2000()) { if (*(pszSubmount) != TEXT(';')) return FALSE; } else --pszSubmount; if (toupper(*(++pszSubmount)) != chDrive) return FALSE; if (*(++pszSubmount) != TEXT(':')) return FALSE; #ifdef COMMENT // No longer a safe assumption on XP if (IsWindows2000()) if (*(++pszSubmount) != TEXT('0')) return FALSE; #endif // scan for next "\" while (*(++pszSubmount) != TEXT('\\')) { if (*pszSubmount==0) return FALSE; } // note that szNetBiosName has a '\\' tagged in the end earlier for (++pszSubmount; *pszSubmount && (*pszSubmount != TEXT('\\')); ++pszSubmount) if (!_tcsncicmp(pszSubmount, szNetBiosName, _tcslen(szNetBiosName))) break; if ((!*pszSubmount) || (*pszSubmount == TEXT('\\'))) return FALSE; pszSubmount += _tcslen(szNetBiosName); #endif } else // (!IsWindowsNT()) { DWORD dwSize = MAX_PATH; if (WNetGetConnection (szDrive, szMapping, &dwSize) != NO_ERROR) return FALSE; if (*(pszSubmount++) != TEXT('\\')) return FALSE; if (*(pszSubmount++) != TEXT('\\')) return FALSE; for ( ; *pszSubmount && (*pszSubmount != TEXT('\\')); ++pszSubmount) if (!lstrncmpi (pszSubmount, szNetBiosName, lstrlen(szNetBiosName))) break; if ((!*pszSubmount) || (*pszSubmount == TEXT('\\'))) return FALSE; pszSubmount += lstrlen(szNetBiosName); } if (!pszSubmount || !*pszSubmount) return FALSE; #ifndef AFSIFS lstrcpy (pszSubmountNow, pszSubmount); #else lstrcpy (pszSubmountNow, "\\afs"); lstrcat (pszSubmountNow, pszSubmount); #endif return TRUE; }
static PyObject * netuse_usage_report_orig(PyObject *self, PyObject *args) { char * servername = NULL; PyObject *retvalue = NULL; DWORD bitmasks; char chdrive = '@'; char drivepath[] = { 'A', ':', '\\', 0 }; char drivename[] = { 'A', ':', 0 }; ULARGE_INTEGER lFreeBytesAvailable; ULARGE_INTEGER lTotalNumberOfBytes; /* ULARGE_INTEGER lTotalNumberOfFreeBytes; */ char szRemoteName[MAX_PATH]; DWORD dwResult, cchBuff = MAX_PATH; DWORD serverlen = 0; if (! PyArg_ParseTuple(args, "|s", &servername)) { return NULL; } if (servername) serverlen = strlen(servername); bitmasks = GetLogicalDrives(); if (bitmasks == 0) { PyErr_Format(PyExc_RuntimeError, "A system error has occurred in GetLogicalDrives: %ld", GetLastError() ); return NULL; } retvalue = PyList_New(0); if (retvalue == NULL) return NULL; while (bitmasks) { ++ chdrive; drivepath[0] = chdrive; drivename[0] = chdrive; if ((bitmasks & 1L) == 0) { bitmasks >>= 1; continue; } bitmasks >>= 1; switch (GetDriveType(drivepath)) { case DRIVE_REMOTE: break; case DRIVE_FIXED: continue; default: continue; } /* If the network connection was made using the Microsoft LAN * Manager network, and the calling application is running in a * different logon session than the application that made the * connection, a call to the WNetGetConnection function for the * associated local device will fail. The function fails with * ERROR_NOT_CONNECTED or ERROR_CONNECTION_UNAVAIL. This is * because a connection made using Microsoft LAN Manager is * visible only to applications running in the same logon session * as the application that made the connection. (To prevent the * call to WNetGetConnection from failing it is not sufficient for * the application to be running in the user account that created * the connection.) * * Refer to http://msdn.microsoft.com/en-us/library/windows/desktop/aa385453(v=vs.85).aspx * */ dwResult = WNetGetConnection(drivename, szRemoteName, &cchBuff ); if (dwResult == NO_ERROR || dwResult == ERROR_CONNECTION_UNAVAIL \ || dwResult == ERROR_NOT_CONNECTED) { if (serverlen) { if ((cchBuff < serverlen + 3) || (strncmp(servername, szRemoteName+2, serverlen) != 0) || (szRemoteName[serverlen + 2] != '\\') ) continue; } } else { PyErr_Format(PyExc_RuntimeError, "A system error has occurred in WNetGetConnection: %ld", GetLastError() ); Py_XDECREF(retvalue); return NULL; } switch (check_duplicate_shared_folder(retvalue, szRemoteName)) { case -1: Py_XDECREF(retvalue); return NULL; case 1: continue; default: break; } if (GetDiskFreeSpaceEx(drivepath, &lFreeBytesAvailable, &lTotalNumberOfBytes, NULL )) { PyObject *pobj = Py_BuildValue("ssLL", drivename, szRemoteName, lFreeBytesAvailable, lTotalNumberOfBytes ); if (PyList_Append(retvalue, pobj) == -1) { Py_XDECREF(retvalue); return NULL; } } else if (dwResult == ERROR_CONNECTION_UNAVAIL || dwResult == ERROR_NOT_CONNECTED) { PyObject *pobj = Py_BuildValue("ssLL", drivename, szRemoteName, 0L, 0L ); if (PyList_Append(retvalue, pobj) == -1) { Py_XDECREF(retvalue); return NULL; } } else { PyErr_Format(PyExc_RuntimeError, "A system error has occurred in GetDiskFreeSpaceEx(%s): %ld", drivepath, GetLastError() ); Py_XDECREF(retvalue); return NULL; } }
static PyObject * netuse_list_drive(PyObject *self, PyObject *args) { PyObject *retvalue = NULL; PyObject *pobj = NULL; char *servername = NULL; char *username = NULL; char *password = NULL; char chdrive = 'A'; char drivepath[] = { 'A', ':', '\\', 0 }; char drivename[] = { 'A', ':', 0 }; char szRemoteName[MAX_PATH]; DWORD dwResult; DWORD cchBuff = MAX_PATH; char szUserName[MAX_PATH] = {0}; if (! PyArg_ParseTuple(args, "|s", &servername)) { return NULL; } retvalue = PyList_New(0); if (retvalue == NULL) return NULL; while (chdrive <= 'Z') { drivepath[0] = chdrive; drivename[0] = chdrive; dwResult = WNetGetConnection(drivename, szRemoteName, &cchBuff ); if (dwResult == NO_ERROR) { dwResult = WNetGetUser(drivename, (LPSTR) szUserName, &cchBuff); if (dwResult != NO_ERROR) snprintf(szUserName, MAX_PATH, "%s", "Unknown User"); pobj = Py_BuildValue("ssss", drivename, szRemoteName, "OK", szUserName ); if (PyList_Append(retvalue, pobj) == -1) { Py_XDECREF(retvalue); return NULL; } } else if (dwResult == ERROR_CONNECTION_UNAVAIL) { } else if (dwResult == ERROR_NOT_CONNECTED) { } else if (dwResult == ERROR_BAD_DEVICE) { } else if (dwResult == ERROR_NO_NET_OR_BAD_PATH) { } else { PyErr_Format(PyExc_RuntimeError, "A system error has occurred in WNetGetConnection: %ld", GetLastError() ); Py_XDECREF(retvalue); return NULL; } ++ chdrive; } return retvalue; }