/** * Wrap QueryDosDevice to allocate memory */ static BOOL QueryDosDeviceWithAlloc(LPCTSTR lpDeviceName, LPTSTR *lppTargetPath, DWORD *pcchSize) { LPTSTR buffer = NULL; DWORD cchSize = 1024, cchRet; assert(lppTargetPath && pcchSize); do { buffer = HeapAlloc(GetProcessHeap(), 0, cchSize * sizeof(TCHAR)); if (!buffer) { print_winerr(_T("HeapAlloc")); return FALSE; } cchRet = QueryDosDevice(lpDeviceName, buffer, cchSize); if (cchRet) { assert(cchRet <= cchSize); } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { cchSize *= 2; HeapFree(GetProcessHeap(), 0, buffer); buffer = NULL; } else { print_winerr(_T("QueryDosDevice")); HeapFree(GetProcessHeap(), 0, buffer); return FALSE; } } while (!cchRet); *pcchSize = cchRet; *lppTargetPath = buffer; return TRUE; }
void KDeviceNameMgr::Refresh() { m_DeviceNameMap.clear(); TCHAR szTemp[BUFSIZE] = {0}; if (GetLogicalDriveStrings(BUFSIZE-1, szTemp)) { TCHAR szName[MAX_PATH]; TCHAR szDrive[3] = TEXT(" :"); BOOL bFound = FALSE; TCHAR* p = szTemp; do { // Copy the drive letter to the template string *szDrive = *p; // Look up each device name if (QueryDosDevice(szDrive, szName, BUFSIZE)) { UINT uNameLen = _tcslen(szName); wstring strDeviceName; strDeviceName = szName; wstring strDosName; strDosName = szDrive; m_DeviceNameMap[strDeviceName] = strDosName; } while (*p++); } while (!bFound && *p); // end of string } }
int _CRTAPI1 main(int argc, char *argv[]) { DWORD dwT; LPTSTR strDriveLetters = (LPTSTR) malloc(sizeof(char) * BUFSIZE); LPTSTR strNext; if(strDriveLetters == NULL) { printf("Error allocating buffer (%d)\n", GetLastError()); exit(-1); } dwT = QueryDosDevice(NULL, strDriveLetters, BUFSIZE); if(dwT == 0) { printf("Error getting device letters (%d)\n", GetLastError()); exit(-2); } strNext = strDriveLetters; while(1) { processDevice(strNext); // get the address of the next address strNext += strlen(strNext) + 1; // if the next string is the NUL string, then return NULL if(*strNext == '\0') break; } free(strDriveLetters); return 0; }
WCHAR WINAPI ImScsiGetDriveLetterForVolumeName( IN LPCWSTR lpszVolumeName) { WCHAR vol_target[MAX_PATH]; wcsncpy(vol_target, lpszVolumeName + 4, 44); vol_target[44] = 0; if (!QueryDosDevice(vol_target, vol_target, _countof(vol_target))) { return FALSE; } WHeapMem<WCHAR> dosdevs(UNICODE_STRING_MAX_BYTES, HEAP_GENERATE_EXCEPTIONS); if (!QueryDosDevice(NULL, dosdevs, (DWORD)dosdevs.Count())) { return FALSE; } WCHAR dev_target[MAX_PATH]; SIZE_T length; for (LPCWSTR ptr = dosdevs; (length = wcslen(ptr)) != 0; ptr += length + 1) { if ((length != 2) || (ptr[1] != L':') || (!QueryDosDevice(ptr, dev_target, _countof(dev_target))) || (_wcsicmp(dev_target, vol_target) != 0)) { continue; } return *ptr; } return 0; }
//ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/memory/base/obtaining_a_file_name_from_a_file_handle.htm BOOL TinyFile::GetFileNameFromHandle(HANDLE hFile, LPTSTR pszFilename) { BOOL bSuccess = FALSE; HANDLE hFileMap; DWORD dwFileSizeHi = 0; DWORD dwFileSizeLo = ::GetFileSize(hFile, &dwFileSizeHi); if (dwFileSizeLo == 0 && dwFileSizeHi == 0) return FALSE; hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, MAX_PATH, NULL); if (hFileMap) { void* pMapView = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1); if (pMapView) { if (GetMappedFileName(GetCurrentProcess(), pMapView, pszFilename, MAX_PATH)) { TCHAR szTemp[1024]; szTemp[0] = '\0'; if (GetLogicalDriveStrings(1024 - 1, szTemp)) { TCHAR szName[MAX_PATH]; TCHAR szDrive[3] = TEXT(" :"); BOOL bFound = FALSE; TCHAR* p = szTemp; do { *szDrive = *p; if (QueryDosDevice(szDrive, szName, 1024)) { UINT iNameSize = _tcslen(szName); if (iNameSize < MAX_PATH) { bFound = _tcsnicmp(pszFilename, szName, iNameSize) == 0; if (bFound) { TCHAR szTempFile[MAX_PATH]; StringCchPrintf(szTempFile, MAX_PATH, TEXT("%s%s"), szDrive, pszFilename + iNameSize); StringCchCopyN(pszFilename, MAX_PATH + 1, szTempFile, _tcslen(szTempFile)); } } } while (*p++); } while (!bFound && *p); } } bSuccess = TRUE; UnmapViewOfFile(pMapView); } CloseHandle(hFileMap); } return(bSuccess); }
string TagstoreConfig::GetDosDevicename(string path) { DWORD r = 0; char target[1024]; target[0] = '\0'; r = QueryDosDevice(path.c_str(), target, 1024); return string(target); }
// From DOS file name to device file name BOOL SystemInfoUtils::GetDeviceFileName( LPCTSTR lpFsFileName, CString& deviceFileName ) { BOOL rc = FALSE; TCHAR lpDrive[3]; // Get the drive letter // unfortunetaly it works only with DOS file names _tcsncpy( lpDrive, lpFsFileName, 2 ); lpDrive[2] = _T('\0'); TCHAR lpDeviceName[0x1000]; // Query the device for the drive letter if ( QueryDosDevice( lpDrive, lpDeviceName, 0x1000 ) != 0 ) { // Subst drive? if ( _tcsnicmp( _T("\\??\\"), lpDeviceName, 4 ) == 0 ) { deviceFileName = lpDeviceName + 4; deviceFileName += lpFsFileName + 2; return TRUE; } else // Network drive? if ( _tcsnicmp( _T("\\Device\\LanmanRedirector\\"), lpDeviceName, 25 ) == 0 ) { //Mapped network drive char cDriveLetter; DWORD dwParam; TCHAR lpSharedName[0x1000]; if ( _stscanf( lpDeviceName, _T("\\Device\\LanmanRedirector\\;%c:%d\\%s"), &cDriveLetter, &dwParam, lpSharedName ) != 3 ) return FALSE; _tcscpy( lpDeviceName, _T("\\Device\\LanmanRedirector\\") ); _tcscat( lpDeviceName, lpSharedName ); } _tcscat( lpDeviceName, lpFsFileName + 2 ); deviceFileName = lpDeviceName; rc = TRUE; } return rc; }
VOID FindMountPoint(PDOKAN_CONTROL Control) { WCHAR physical[65536]; WCHAR logical[65536]; QueryDosDevice(NULL, physical, sizeof(physical)); for (WCHAR *pos = physical; *pos; pos += wcslen(pos) + 1) { QueryDosDevice(pos, logical, sizeof(logical)); if (wcsstr(logical, Control->DeviceName) != NULL && wcsstr(logical, pos) == NULL) { wcscpy_s(Control->MountPoint, sizeof(Control->MountPoint) / sizeof(WCHAR), pos); DbgPrintW(L"DokanControl MountPoint found '%s'\n", Control->MountPoint); break; } } }
//-------------------------------------------------------------------------- // get path+name from a mapped file in the debugged process bool win32_debmod_t::get_mapped_filename(HANDLE process_handle, ea_t imagebase, char *buf, size_t bufsize) { if ( _GetMappedFileName != NULL ) { TCHAR name[QMAXPATH]; name[0] = '\0'; if ( !can_access(imagebase) ) imagebase += MEMORY_PAGE_SIZE; if ( _GetMappedFileName(process_handle, (LPVOID)imagebase, name, qnumber(name)) ) { // translate path with device name to drive letters. // based on http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/obtaining_a_file_name_from_a_file_handle.asp TCHAR szTemp[MAX_PATH]; szTemp[0] = '\0'; if ( GetLogicalDriveStrings(sizeof(szTemp), szTemp) ) { char szName[MAX_PATH]; char szDrive[3] = " :"; bool bFound = FALSE; char *p = szTemp; do { // Copy the drive letter to the template string szDrive[0] = *p; // Look up each device name if ( QueryDosDevice(szDrive, szName, MAX_PATH) ) { size_t uNameLen = strlen(szName); if ( uNameLen < MAX_PATH ) { bFound = strnicmp(name, szName, uNameLen) == 0; if ( bFound ) { // Reconstruct pszFilename using szTemp // Replace device path with DOS path qstrncpy(name, szDrive, sizeof(name)); qstrncat(name, name+uNameLen, sizeof(name)); } } } // Go to the next NULL character. while ( *p++ ); } while ( !bFound && *p ); // end of string } wcstr(buf, name, bufsize); return true; } } return false; }
bool TranslateDeviceName(std::wstring& path) { if (!StartsWith(path, L"\\Device\\")) return false; size_t pos = path.find('\\', 8); if (pos == std::wstring::npos) return false; std::wstring device_name = path.substr(0, pos); path = path.substr(pos); const int drive_letters_size = 1024; WCHAR drive_letters[drive_letters_size] = {'\0'}; if (!GetLogicalDriveStrings(drive_letters_size - 1, drive_letters)) return false; WCHAR* p = drive_letters; WCHAR device[MAX_PATH]; WCHAR logical_drive[3] = L" :"; std::wstring drive_letter; do { *logical_drive = *p; if (QueryDosDevice(logical_drive, device, MAX_PATH)) { if (device_name == device) { drive_letter = logical_drive; } else { const std::wstring network_prefix = L"\\Device\\LanmanRedirector"; std::wstring location = device; if (StartsWith(location, network_prefix)) { location.erase(0, network_prefix.size()); if (StartsWith(location, L"\\;")) { pos = location.find('\\', 1); if (pos != std::wstring::npos) location.erase(0, pos); } if (StartsWith(path, location)) { drive_letter = logical_drive; path.erase(0, location.size()); } } } } while (*p++); } while (drive_letter.empty() && *p); if (drive_letter.empty()) drive_letter = L"\\"; // assuming that it's a remote drive path = drive_letter + path; return true; }
// From DOS file name to device file name 将DOS文件名转换为设备文件名 DWORD CFileControlTool::GetDeviceFileName(LPCTSTR pszFsFileName,LPTSTR pszDeviceFileName,DWORD dwMaxLen) { DWORD dwRet = 0; TCHAR lpDrive[3]; // 获取驱动器盘符 // 遗憾的是它仅适用于DOS文件名 _tcsncpy(lpDrive, pszFsFileName,2); lpDrive[2] = _T('\0'); TCHAR szDeviceName[MAX_NAME_DRIVER_LEN+1]; // Query the device for the drive letter if ( QueryDosDevice(lpDrive,szDeviceName,MAX_NAME_DRIVER_LEN) != 0 ) { // Subst drive? 监测是否为虚拟驱动器 if (_tcsnicmp( _T("\\??\\"), szDeviceName, 4 ) == 0) { _tcscpy_s(pszDeviceFileName,dwMaxLen,szDeviceName + 4); _tcscat_s(pszDeviceFileName,dwMaxLen,pszFsFileName + 2); //pszDeviceFileName = szDeviceName + 4; //pszDeviceFileName += pszFsFileName + 2; return _tcslen(pszDeviceFileName); } else // Network drive? 监测是否为网络驱动器 if (_tcsnicmp(_T("\\Device\\LanmanRedirector\\"),szDeviceName,25) == 0) { //Mapped network drive 映射网络驱动器 char cDriveLetter; DWORD dwParam; TCHAR lpSharedName[0x1000]; if (_stscanf(szDeviceName,_T("\\Device\\LanmanRedirector\\;%c:%d\\%s"),&cDriveLetter,&dwParam,lpSharedName ) != 3) return FALSE; _tcscpy(szDeviceName,_T("\\Device\\LanmanRedirector\\")); _tcscat(szDeviceName,lpSharedName); } _tcscat( szDeviceName,pszFsFileName + 2); _tcscpy_s(pszDeviceFileName,dwMaxLen,szDeviceName); dwRet = _tcslen(pszDeviceFileName); } return dwRet; }
bool isRemovableMedia(const QString & vol) { char szDosDeviceName[MAX_PATH]; QString volume = vol; UINT driveType = GetDriveType(volume.replace("/", "\\").toLatin1()); if (driveType != DRIVE_REMOVABLE) return false; QueryDosDevice(volume.replace("/", "").toLatin1(), szDosDeviceName, MAX_PATH); if (strstr(szDosDeviceName, "\\Floppy") != NULL) { // it's a floppy return false; } return true; }
// From device file name to DOS filename BOOL SystemInfoUtils::GetFsFileName( LPCTSTR lpDeviceFileName, CString& fsFileName ) { BOOL rc = FALSE; TCHAR lpDeviceName[0x1000]; TCHAR lpDrive[3] = _T("A:"); // Iterating through the drive letters for ( TCHAR actDrive = _T('A'); actDrive <= _T('Z'); actDrive++ ) { lpDrive[0] = actDrive; // Query the device for the drive letter if ( QueryDosDevice( lpDrive, lpDeviceName, 0x1000 ) != 0 ) { // Network drive? if ( _tcsnicmp( _T("\\Device\\LanmanRedirector\\"), lpDeviceName, 25 ) == 0 ) { //Mapped network drive char cDriveLetter; DWORD dwParam; TCHAR lpSharedName[0x1000]; if ( _stscanf( lpDeviceName, _T("\\Device\\LanmanRedirector\\;%c:%d\\%s"), &cDriveLetter, &dwParam, lpSharedName ) != 3 ) continue; _tcscpy( lpDeviceName, _T("\\Device\\LanmanRedirector\\") ); _tcscat( lpDeviceName, lpSharedName ); } // Is this the drive letter we are looking for? if ( _tcsnicmp( lpDeviceName, lpDeviceFileName, _tcslen( lpDeviceName ) ) == 0 ) { fsFileName = lpDrive; fsFileName += (LPCTSTR)( lpDeviceFileName + _tcslen( lpDeviceName ) ); rc = TRUE; break; } } } return rc; }
void ConnectDialog::showEvent(QShowEvent *event) { Q_UNUSED(event); ui->comboBox_COM_port->clear(); #ifdef __WINDOWS__ // Enumerate comports for ( int i=0; i<=255; i++ ) { WCHAR lpTargetPath[5000]; QString comport = QString("COM%1").arg(i); DWORD test = QueryDosDevice(comport.toStdWString().c_str(), (LPWSTR)lpTargetPath, 5000); if ( test != 0 ) { ui->comboBox_COM_port->addItem(comport); } } #else QDirIterator it("/dev/serial/by-id", QDirIterator::NoIteratorFlags); while ( it.hasNext() ) { it.next(); QString path = it.fileInfo().fileName(); if ( path == "." || path == ".." ) continue; if ( !it.fileInfo().isSymLink() ) continue; ui->comboBox_COM_port->addItem(it.fileInfo().symLinkTarget()); } #endif ui->comboBox_CAN_Bitrate->setCurrentIndex(getConfig("CAN_BITRATE_BOX").toInt()); ui->comboBox_COM_port->lineEdit()->setText(getConfig("COM_PORT_BOX")); ui->comboBox_Data_Bits->setCurrentIndex(getConfig("DATA_BIT_BOX").toInt()); ui->comboBox_Parity->setCurrentIndex(getConfig("PARITY_BOX").toInt()); ui->comboBox_speed->setCurrentIndex(getConfig("SPEED_BOX").toInt()); ui->comboBox_StopBits->setCurrentIndex(getConfig("STOP_BIT_BOX").toInt()); ui->lineEdit_SocketCAN_device->setText(getConfig("SOCKET_CAN_PORT")); ui->checkBox_LoopBack->setChecked(getConfig("LOOPBACK_BOX")=="yes"); }
SerialPort::Ptr WinPortFactory::create(const std::string& name) { bool isUsb = false; char szNtDeviceName[MAX_PATH]; if (QueryDosDevice(name.c_str(), szNtDeviceName, MAX_PATH)) { if (strncmp(szNtDeviceName, USB_DEVICE_NAME, sizeof(USB_DEVICE_NAME) - 1) == 0) { isUsb = true; } } return create(name, isUsb); }
// From device file name to DOS filename 将设备文件名转换为DOS文件名 DWORD CFileControlTool::GetFsFileName(LPCTSTR pszDeviceFileName,LPTSTR pszfsFileName,DWORD dwManLen) { DWORD dwRet = 0; TCHAR szDeviceName[MAX_NAME_DRIVER_LEN]; TCHAR lpDrive[3] = _T("A:"); // Iterating through the drive letters for ( TCHAR actDrive = _T('A'); actDrive <= _T('Z'); actDrive++ ) { lpDrive[0] = actDrive; // Query the device for the drive letter if ( QueryDosDevice(lpDrive, szDeviceName,MAX_NAME_DRIVER_LEN) != 0) { // Network drive? if (_tcsnicmp(_T("\\Device\\LanmanRedirector\\"),szDeviceName,25) == 0) { //Mapped network drive char cDriveLetter; DWORD dwParam; TCHAR lpSharedName[MAX_NAME_DRIVER_LEN]; if (_stscanf(szDeviceName,_T("\\Device\\LanmanRedirector\\;%c:%d\\%s"),&cDriveLetter,&dwParam,lpSharedName) != 3) continue; _tcscpy(szDeviceName,_T("\\Device\\LanmanRedirector\\")); _tcscat(szDeviceName,lpSharedName); } // Is this the drive letter we are looking for? if (_tcsnicmp(szDeviceName,pszDeviceFileName,_tcslen( szDeviceName )) == 0) { _tcscpy_s(pszfsFileName,dwManLen,lpDrive); _tcscat_s(pszfsFileName,dwManLen,(LPCTSTR)(pszDeviceFileName + _tcslen(szDeviceName))); dwManLen = TRUE; break; } } } return dwManLen; }
// // Recursively evaluate drivestr to find the final // dos drive letter to which the source is mapped. // static BOOL DriveSubstitution(char *drivestr, char *subststr, size_t substlen) { char device[MAX_PATH]; if ( QueryDosDevice(drivestr, device, MAX_PATH) ) { if ( device[0] == '\\' && device[1] == '?' && device[2] == '?' && device[3] == '\\' && isalpha(device[4]) && device[5] == ':') { device[0] = device[4]; device[1] = ':'; device[2] = '\0'; if ( DriveSubstitution(device, subststr, substlen) ) { return TRUE; } else { subststr[0] = device[0]; subststr[1] = ':'; subststr[2] = '\0'; return TRUE; } } else if ( device[0] == '\\' && device[1] == '?' && device[2] == '?' && device[3] == '\\' && device[4] == 'U' && device[5] == 'N' && device[6] == 'C' && device[7] == '\\') { subststr[0] = '\\'; strncpy(&subststr[1], &device[7], substlen-1); subststr[substlen-1] = '\0'; return TRUE; } } return FALSE; }
static int listDevices(ClientElem_t *pClient) { int res = 0; int size = 8192; TCHAR *pszDevices = 0; long dwChars = 0; TCHAR *ptr; while (res == 0 && dwChars == 0) { pszDevices = malloc(size); if (pszDevices != 0) { dwChars = QueryDosDevice(NULL, pszDevices, size/sizeof(TCHAR)); if (dwChars == 0) { DWORD err = GetLastError(); if (err == ERROR_INSUFFICIENT_BUFFER) { size *= 2; if (size > 1024*1024) { SEND("ERROR cannot list com ports, QueryDosDevice too hungry"); res = -1; } else { free(pszDevices); } } else { SEND("ERROR cannot list com ports, %i", err); res = -1; } } } else { SEND("ERROR cannot list com ports, out of memory"); res = -1; } } ptr = pszDevices; while (res >= 0 && dwChars > 0) { int port; TCHAR *pTmp; if (swscanf(ptr, TEXT("COM%i"), &port) == 1) { SEND("COM%i", port); } pTmp = wcschr(ptr, 0); dwChars -= (DWORD)((pTmp - ptr) / sizeof(TCHAR) + 1); ptr = pTmp + 1; } free(pszDevices); return res; }
static gboolean get_process_filename (HANDLE process, WCHAR * name, DWORD name_capacity) { gsize name_length; WCHAR drive_strings[DRIVE_STRINGS_MAX_LENGTH]; WCHAR *drive; if (GetProcessImageFileName (process, name, name_capacity) == 0) return FALSE; name_length = wcslen (name); drive_strings[0] = L'\0'; drive_strings[DRIVE_STRINGS_MAX_LENGTH - 1] = L'\0'; GetLogicalDriveStringsW (DRIVE_STRINGS_MAX_LENGTH - 1, drive_strings); for (drive = drive_strings; *drive != '\0'; drive += wcslen (drive) + 1) { WCHAR device_name[3]; WCHAR mapping_strings[MAX_PATH]; WCHAR *mapping; gsize mapping_length; wcsncpy_s (device_name, 3, drive, 2); mapping_strings[0] = '\0'; mapping_strings[MAX_PATH - 1] = '\0'; QueryDosDevice (device_name, mapping_strings, MAX_PATH - 1); for (mapping = mapping_strings; *mapping != '\0'; mapping += mapping_length + 1) { mapping_length = wcslen (mapping); if (mapping_length > name_length) continue; if (wcsncmp (name, mapping, mapping_length) == 0) { wcscpy_s (name, 3, device_name); memmove (name + 2, name + mapping_length, (name_length - mapping_length + 1) * sizeof (WCHAR)); return TRUE; } } } return FALSE; }
int digilent::find_digilent_device_name(char* device) { char* sz; char* p; int len = 0xFFFF; int rc = -1; u32 vendor, product; sz = (char*)malloc(len); if (sz == NULL) return -1; // Find all DOS names if (QueryDosDevice(NULL, sz, len) <= 0) return -1; p = sz; while (*p) { if (sscanf(p, "USB#Vid_%x&Pid_%x#", &vendor, &product) == 2) { if (vendor == USB_VENDOR_ID && product == USB_PRODUCT_ID) // Found break; } p += strlen(p) + 1; } if (*p == 0) // Not found goto cleanup; // Copy result to target buffer strncpy(device, p, MAX_PATH); rc = 0; cleanup: if (sz) free(sz); return rc; }
void displayDevice(const std::wstring &in) { wchar_t * targetBuffer = NULL; DWORD currentSize = 0; DWORD error = 0; const DWORD incrementSize = 2048; //Display device path logger << in; do { delete [] targetBuffer; currentSize += incrementSize; targetBuffer = new wchar_t[currentSize]; error = QueryDosDevice(in.c_str(), targetBuffer, currentSize); } while (error == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER); if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { logger << L"\r\n"; delete [] targetBuffer; return; } logger << L" = " << targetBuffer; delete [] targetBuffer; UINT result = GetDriveType(in.c_str()); switch (result) { case DRIVE_REMOVABLE: logger << L" [Removable]"; break; case DRIVE_FIXED: logger << L" [Fixed]"; break; case DRIVE_REMOTE: logger << L" [Remote]"; break; case DRIVE_CDROM: logger << L" [CDRom]"; break; case DRIVE_RAMDISK: logger << L" [RAMDisk]"; break; } logger << L"\r\n"; }
UINT fileManage::getVolume(WCHAR* volumePathName, UINT num) const { HANDLE findHandle = NULL; DWORD charCount = MAX_PATH+1; WCHAR volumeName[MAX_PATH+1]; WCHAR deviceName[MAX_PATH+1]; DWORD index = 0; UINT result = 0; UINT count = 0; findHandle = FindFirstVolume(volumeName, ARRAYSIZE(volumeName)); if(INVALID_HANDLE_VALUE==findHandle) return 0; while(TRUE) { index = wcslen(volumeName)-1; volumeName[index] = L'\0'; result = QueryDosDevice(&volumeName[4], deviceName, ARRAYSIZE(deviceName)); volumeName[index] = L'\\'; if(ERROR_INSUFFICIENT_BUFFER==result || 0==result) { result = 0; break; } result = retrieveDevName(deviceName); if(!result) { size_t temp = wcslen(volumePathName); if(volumePathName[temp]==L'\0') result=1; break; } result = GetVolumePathNamesForVolumeName(volumeName, volumePathName, charCount, &charCount); if(result) { if(count++==num) { size_t temp = wcslen(volumePathName); if(volumePathName[temp]==L'\0') result=1; break; } result = FindNextVolume(findHandle, volumeName, ARRAYSIZE(volumeName)); } } FindVolumeClose(findHandle); findHandle = INVALID_HANDLE_VALUE; return result; }
bool GetComPort(int * comPort, const int PORT_SCAN_LIMIT) { const DWORD ucchMax = 1000; TCHAR lpTargetPath[ucchMax]; // buffer to store the path of the COMPORTS *comPort = -1; for (int i = 0; i<=PORT_SCAN_LIMIT; i++) { std::ostringstream ossComPort; ossComPort << i; std::string strComPort(ossComPort.str()); std::string comPortName = std::string("COM") + ossComPort.str(); if(QueryDosDevice((LPCTSTR)comPortName.c_str(), (LPTSTR)lpTargetPath, ucchMax)) { *comPort = i; return true; } } return false; }
CStdString NetworkDriveHelper::ResolveSubstDriveInPath(const CStdString& sPathIn) { int iLength = sPathIn.GetLength(); if(iLength < 3 || sPathIn[1] != _T(':') || sPathIn[2] != _T('\\')) return sPathIn; CStdString sPathExpanded; TCHAR* szBuffer = sPathExpanded.GetBuffer(_MAX_PATH); int iCharCount = QueryDosDevice(sPathIn.Left(2), szBuffer, 1024); sPathExpanded.ReleaseBuffer(); if(iCharCount == 0 || sPathExpanded.Left(4) != _T("\\??\\")) return sPathIn; sPathExpanded.Delete(0, 4); if(sPathExpanded[1] != _T(':')) return sPathIn; return sPathExpanded + sPathIn.Right(iLength - 2); }
BOOL GetPorts(Port p[], int &count, BOOL fGetCountOnly) { BOOL fRet = FALSE; TCHAR buf[65535]; unsigned long dwChars = QueryDosDevice(NULL, buf, sizeof(buf)); int x = 0; if ( dwChars == 0 ) { TRACE(_T("Error with querydosdevice: (%ld)\n"), GetLastError()); fRet = FALSE; } else { TCHAR *ptr = buf; int port; while (dwChars) { if ( _stscanf_s(ptr, "COM%d", &port) == 1 ) { if ( ! fGetCountOnly ) { p[x].iPort = port; p[x].fHasRA = FALSE; } x++; } TCHAR *temp_ptr = strchr(ptr,0); dwChars -= (DWORD)((temp_ptr-ptr)/sizeof(TCHAR)+1); ptr = temp_ptr+1; } // while fRet = TRUE; if ( fGetCountOnly ) { count = x; } } return fRet; }
DWORD APIENTRY NPAddConnection3( __in HWND WndOwner, __in LPNETRESOURCE NetResource, __in LPWSTR Password, __in LPWSTR UserName, __in DWORD Flags) { DWORD status; WCHAR temp[128]; WCHAR local[3]; UNREFERENCED_PARAMETER(WndOwner); UNREFERENCED_PARAMETER(Password); UNREFERENCED_PARAMETER(UserName); UNREFERENCED_PARAMETER(Flags); DbgPrintW(L"NPAddConnection3\n"); DbgPrintW(L" LocalName: %s\n", NetResource->lpLocalName); DbgPrintW(L" RemoteName: %s\n", NetResource->lpRemoteName); ZeroMemory(local, sizeof(local)); if (lstrlen(NetResource->lpLocalName) > 1 && NetResource->lpLocalName[1] == L':') { local[0] = (WCHAR)toupper(NetResource->lpLocalName[0]); local[1] = L':'; local[2] = L'\0'; } if (QueryDosDevice(local, temp, 128)) { DbgPrintW(L" WN_ALREADY_CONNECTED"); status = WN_ALREADY_CONNECTED; } else { DbgPrintW(L" WN_BAD_NETNAME"); status = WN_BAD_NETNAME; } return status; }
port_list serial_port::available_ports() { port_list available; #ifdef _WIN32 // Needed for QueryDosDevice char path[128]; #endif int n = 0; // List the available serial ports for (int p=RS232_MIN_PORT_INDEX; p<=RS232_MAX_PORT_INDEX; p++) { #ifdef _WIN32 n = QueryDosDevice(comports[p], path, sizeof(path)); #else n = access(comports[p], F_OK) + 1; #endif if (n>0) { available.push_back(comports[p]); } }//end for(p) return available; }
static bool mapDeviceToDriveLetter(QString *s) { enum { bufSize = 512 }; // Retrieve drive letters and get their device names. // Do not cache as it may change due to removable/network drives. TCHAR driveLetters[bufSize]; if (!GetLogicalDriveStrings(bufSize-1, driveLetters)) return false; TCHAR driveName[MAX_PATH]; TCHAR szDrive[3] = TEXT(" :"); for (const TCHAR *driveLetter = driveLetters; *driveLetter; driveLetter++) { szDrive[0] = *driveLetter; // Look up each device name if (QueryDosDevice(szDrive, driveName, MAX_PATH)) { const QString deviceName = QString::fromWCharArray(driveName); if (s->startsWith(deviceName)) { s->replace(0, deviceName.size(), QString::fromWCharArray(szDrive)); return true; } } } return false; }
BOOL CEnumerateSerial::UsingQueryDosDevice(CSimpleArray<UINT>& ports) #endif { //What will be the return value from this function (assume the worst) BOOL bSuccess = FALSE; //Make sure we clear out any elements which may already be in the array ports.RemoveAll(); //Determine what OS we are running on OSVERSIONINFO osvi; osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); BOOL bGetVer = GetVersionEx(&osvi); //On NT use the QueryDosDevice API if (bGetVer && (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)) { //Use QueryDosDevice to look for all devices of the form COMx. Since QueryDosDevice does //not consitently report the required size of buffer, lets start with a reasonable buffer size //of 4096 characters and go from there int nChars = 4096; BOOL bWantStop = FALSE; while (nChars && !bWantStop) { ATL::CHeapPtr<TCHAR> szDevices; if (szDevices.Allocate(nChars)) { DWORD dwChars = QueryDosDevice(NULL, szDevices, nChars); if (dwChars == 0) { DWORD dwError = GetLastError(); if (dwError == ERROR_INSUFFICIENT_BUFFER) { //Expand the buffer and loop around again nChars *= 2; } else bWantStop = TRUE; } else { bSuccess = TRUE; bWantStop = TRUE; size_t i=0; while (szDevices[i] != _T('\0')) { //Get the current device name TCHAR* pszCurrentDevice = &szDevices[i]; //If it looks like "COMX" then //add it to the array which will be returned size_t nLen = _tcslen(pszCurrentDevice); if (nLen > 3) { if ((_tcsnicmp(pszCurrentDevice, _T("COM"), 3) == 0) && IsNumeric(&pszCurrentDevice[3], FALSE)) { //Work out the port number int nPort = _ttoi(&pszCurrentDevice[3]); ports.Add(nPort); } } //Go to next device name i += (nLen + 1); } } } else { bWantStop = TRUE; SetLastError(ERROR_OUTOFMEMORY); } } } else SetLastError(ERROR_CALL_NOT_IMPLEMENTED); return bSuccess; }
BOOL GetFileNameFromHandle(HANDLE hFile) { BOOL bSuccess = FALSE; TCHAR pszFilename[MAX_PATH+1]; HANDLE hFileMap; // Get the file size. DWORD dwFileSizeHi = 0; DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi); if( dwFileSizeLo == 0 && dwFileSizeHi == 0 ) { _tprintf(TEXT("Cannot map a file with a length of zero.\n")); return FALSE; } // Create a file mapping object. hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 1, NULL); if (hFileMap) { // Create a file mapping to get the file name. void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1); if (pMem) { if (GetMappedFileName (GetCurrentProcess(), pMem, pszFilename, MAX_PATH)) { // Translate path with device name to drive letters. TCHAR szTemp[BUFSIZE]; szTemp[0] = '\0'; if (GetLogicalDriveStrings(BUFSIZE-1, szTemp)) { TCHAR szName[MAX_PATH]; TCHAR szDrive[3] = TEXT(" :"); BOOL bFound = FALSE; TCHAR* p = szTemp; do { // Copy the drive letter to the template string *szDrive = *p; // Look up each device name if (QueryDosDevice(szDrive, szName, MAX_PATH)) { size_t uNameLen = _tcslen(szName); if (uNameLen < MAX_PATH) { bFound = _tcsnicmp(pszFilename, szName, uNameLen) == 0 && *(pszFilename + uNameLen) == _T('\\'); if (bFound) { // Reconstruct pszFilename using szTempFile // Replace device path with DOS path TCHAR szTempFile[MAX_PATH]; StringCchPrintf(szTempFile, MAX_PATH, TEXT("%s%s"), szDrive, pszFilename+uNameLen); StringCchCopyN(pszFilename, MAX_PATH+1, szTempFile, _tcslen(szTempFile)); } } } // Go to the next NULL character. while (*p++); } while (!bFound && *p); // end of string } } bSuccess = TRUE; UnmapViewOfFile(pMem); } CloseHandle(hFileMap); } _tprintf(TEXT("File name is %s\n"), pszFilename); return(bSuccess); }