static void test_define_dos_deviceA(void) { char drivestr[3]; char buf[MAX_PATH]; DWORD ret; /* Find an unused drive letter */ drivestr[1] = ':'; drivestr[2] = 0; for (drivestr[0] = 'a'; drivestr[0] <= 'z'; drivestr[0]++) { ret = QueryDosDeviceA( drivestr, buf, sizeof(buf)); if (!ret) break; } if (drivestr[0] > 'z') { skip("can't test creating a dos drive, none available\n"); return; } /* Map it to point to the current directory */ ret = GetCurrentDirectoryA(sizeof(buf), buf); ok(ret, "GetCurrentDir\n"); ret = DefineDosDeviceA(0, drivestr, buf); todo_wine ok(ret, "Could not make drive %s point to %s!\n", drivestr, buf); if (!ret) { skip("can't test removing fake drive\n"); } else { ret = DefineDosDeviceA(DDD_REMOVE_DEFINITION, drivestr, NULL); ok(ret, "Could not remove fake drive %s!\n", drivestr); } }
void specialname_map_init(void) { char letter[3]; char buf[MAX_PATH]; char c; unsigned int idx = 0; unsigned int i, x; size_t len; letter[1] = ':'; letter[2] = '\0'; for (c = 'A'; c <= 'Z'; c++) { letter[0] = c; if (QueryDosDeviceA(letter, buf, MAX_PATH)) { g_specialnames_a[idx] = strdup(letter); g_targetnames_a[idx] = strdup(buf); idx++; } } GetWindowsDirectoryA(buf, MAX_PATH); g_targetnames_a[idx] = strdup("\\systemroot"); g_specialnames_a[idx] = strdup(buf); idx++; len = strlen(buf) + strlen("\\system32"); system32dir_a = calloc(1, len + 1); system32dir_w = calloc(1, (len + 1) * sizeof(wchar_t)); strcpy(system32dir_a, buf); strcat(system32dir_a, "\\system32"); for (x = 0; x < len - strlen("\\system32"); x++) system32dir_w[x] = (wchar_t)buf[x]; wcscat(system32dir_w, L"\\system32"); system32dir_len = (unsigned int)len; len = strlen(buf) + strlen("\\sysnative"); sysnativedir_a = calloc(1, len + 1); sysnativedir_w = calloc(1, (len + 1) * sizeof(wchar_t)); strcpy(sysnativedir_a, buf); strcat(sysnativedir_a, "\\sysnative"); for (x = 0; x < len - strlen("\\sysnative"); x++) sysnativedir_w[x] = (wchar_t)buf[x]; wcscat(sysnativedir_w, L"\\sysnative"); sysnativedir_len = (unsigned int)len; for (i = 0; i < idx; i++) { len = strlen(g_specialnames_a[i]) + 1; g_specialnames_w[i] = (wchar_t *)malloc(len * sizeof(wchar_t)); for (x = 0; x < len; x++) g_specialnames_w[i][x] = (wchar_t)g_specialnames_a[i][x]; len = strlen(g_targetnames_a[i]) + 1; g_targetnames_w[i] = (wchar_t *)malloc(len * sizeof(wchar_t)); for (x = 0; x < len; x++) g_targetnames_w[i][x] = (wchar_t)g_targetnames_a[i][x]; } g_num_specialnames = idx; }
static void test_query_dos_deviceA(void) { char drivestr[] = "a:"; char *p, *buffer, buffer2[2000]; DWORD ret, ret2, buflen=32768; BOOL found = FALSE; if (!pFindFirstVolumeA) { win_skip("On win9x, HARDDISK and RAMDISK not present\n"); return; } buffer = HeapAlloc( GetProcessHeap(), 0, buflen ); ret = QueryDosDeviceA( NULL, buffer, buflen ); ok(ret && GetLastError() != ERROR_INSUFFICIENT_BUFFER, "QueryDosDevice buffer too small\n"); if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { HeapFree( GetProcessHeap(), 0, buffer ); return; } ok(ret, "QueryDosDeviceA failed to return list, last error %u\n", GetLastError()); if (ret) { p = buffer; for (;;) { if (!strlen(p)) break; ret2 = QueryDosDeviceA( p, buffer2, sizeof(buffer2) ); ok(ret2, "QueryDosDeviceA failed to return current mapping for %s, last error %u\n", p, GetLastError()); p += strlen(p) + 1; if (ret <= (p-buffer)) break; } } for (;drivestr[0] <= 'z'; drivestr[0]++) { /* Older W2K fails with ERROR_INSUFFICIENT_BUFFER when buflen is > 32767 */ ret = QueryDosDeviceA( drivestr, buffer, buflen - 1); if(ret) { for (p = buffer; *p; p++) *p = toupper(*p); if (strstr(buffer, "HARDDISK") || strstr(buffer, "RAMDISK")) found = TRUE; } } ok(found, "expected at least one devicename to contain HARDDISK or RAMDISK\n"); HeapFree( GetProcessHeap(), 0, buffer ); }
static void test_query_dos_deviceA(void) { char drivestr[] = "a:"; char *p, *buffer, buffer2[2000]; DWORD ret, ret2, buflen=32768; BOOL found = FALSE; /* callers must guess the buffer size */ SetLastError(0xdeadbeef); ret = QueryDosDeviceA( NULL, NULL, 0 ); ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "QueryDosDeviceA(no buffer): returned %u, le=%u\n", ret, GetLastError()); buffer = HeapAlloc( GetProcessHeap(), 0, buflen ); SetLastError(0xdeadbeef); ret = QueryDosDeviceA( NULL, buffer, buflen ); ok((ret && GetLastError() != ERROR_INSUFFICIENT_BUFFER), "QueryDosDeviceA failed to return list, last error %u\n", GetLastError()); if (ret && GetLastError() != ERROR_INSUFFICIENT_BUFFER) { p = buffer; for (;;) { if (!strlen(p)) break; ret2 = QueryDosDeviceA( p, buffer2, sizeof(buffer2) ); ok(ret2, "QueryDosDeviceA failed to return current mapping for %s, last error %u\n", p, GetLastError()); p += strlen(p) + 1; if (ret <= (p-buffer)) break; } } for (;drivestr[0] <= 'z'; drivestr[0]++) { /* Older W2K fails with ERROR_INSUFFICIENT_BUFFER when buflen is > 32767 */ ret = QueryDosDeviceA( drivestr, buffer, buflen - 1); ok(ret || GetLastError() == ERROR_FILE_NOT_FOUND, "QueryDosDeviceA failed to return current mapping for %s, last error %u\n", drivestr, GetLastError()); if(ret) { for (p = buffer; *p; p++) *p = toupper(*p); if (strstr(buffer, "HARDDISK") || strstr(buffer, "RAMDISK")) found = TRUE; } } ok(found, "expected at least one devicename to contain HARDDISK or RAMDISK\n"); HeapFree( GetProcessHeap(), 0, buffer ); }
static void test_query_dos_deviceA(void) { char drivestr[] = "a:"; char *p, buffer[2000]; DWORD ret; BOOL found = FALSE; for (;drivestr[0] <= 'z'; drivestr[0]++) { ret = QueryDosDeviceA( drivestr, buffer, sizeof(buffer)); if(ret) { for (p = buffer; *p; p++) *p = toupper(*p); if (strstr(buffer, "HARDDISK") || strstr(buffer, "RAMDISK")) found = TRUE; } } todo_wine ok(found, "expected at least one devicename to contain HARDDISK or RAMDISK\n"); }
static void test_query_dos_deviceA(void) { char drivestr[] = "a:"; char *p, buffer[2000]; DWORD ret; for (;drivestr[0] <= 'z'; drivestr[0]++) { ret = QueryDosDeviceA( drivestr, buffer, sizeof(buffer)); if(ret) { for (p = buffer; *p; p++) *p = toupper(*p); todo_wine ok( strstr( buffer, CDROM) || strstr( buffer, FLOPPY) || strstr( buffer, HARDISK) || strstr( buffer, LANMAN) || strstr( buffer, RAMDISK), "expect the string %s contains %s,%s,%s,%s or %s\n", buffer, CDROM, FLOPPY, HARDISK, LANMAN, RAMDISK); } } }
void PrintRemapForSubstDrive(char driveLetter) { const int BUF_SIZE = 1024; char targetPath[BUF_SIZE]; char rootPath[8]; sprintf_s(rootPath, 8, "%c:", driveLetter); DWORD result = QueryDosDeviceA(rootPath, targetPath, BUF_SIZE); if (result == 0) { return; } else { if (targetPath[0] == '\\' && targetPath[1] == '?' && targetPath[2] == '?' && targetPath[3] == '\\') { // example path: \??\C:\jetbrains\idea NormalizeSlashes(targetPath, '/'); printf("%c:\n%s\n", driveLetter, targetPath + 4); } } }
/* * Mount partition #part_nr, residing on the same disk as drive_name to an available * drive letter. Returns the newly allocated drive string. * We need to do this because, for instance, EFI System partitions are not assigned * Volume GUIDs by the OS, and we need to have a letter assigned, for when we invoke * bcdtool for Windows To Go. All in all, the process looks like this: * 1. F: = \Device\HarddiskVolume9 (SINGLE LOOKUP) * 2. Harddisk5Partition1 = \Device\HarddiskVolume9 (FULL LOOKUP) * 3. Harddisk5Partition2 = \Device\HarddiskVolume10 (SINGLE LOOKUP) * 4. DefineDosDevice(letter, \Device\HarddiskVolume10) */ char* AltMountVolume(const char* drive_name, uint8_t part_nr) { static char mounted_drive[] = "?:"; const DWORD bufsize = 65536; char *buffer = NULL, *p, target[2][MAX_PATH], *ret = NULL; size_t i; mounted_drive[0] = GetUnusedDriveLetter(); if (mounted_drive[0] == 0) { uprintf("Could not find an unused drive letter"); goto out; } target[0][0] = 0; // Convert our drive letter to something like "\Device\HarddiskVolume9" if (!QueryDosDeviceA(drive_name, target[0], MAX_PATH) || (strlen(target[0]) == 0)) { uprintf("Could not get the DOS volume name for '%s': %s", drive_name, WindowsErrorString()); goto out; } // Now parse the whole DOS device list to find the 'Harddisk#Partition#' that matches the above // TODO: realloc if someone ever manages to burst through 64K of DOS devices buffer = malloc(bufsize); if (buffer == NULL) goto out; buffer[0] = 0; if (!QueryDosDeviceA(NULL, buffer, bufsize)) { uprintf("Could not get the DOS device list: %s", WindowsErrorString()); goto out; } p = buffer; while (strlen(p) != 0) { if ((strncmp("Harddisk", p, 8) == 0) && (strstr(&p[9], "Partition") != NULL)) { target[1][0] = 0; if (QueryDosDeviceA(p, target[1], MAX_PATH) && (strlen(target[1]) != 0)) if ((strcmp(target[1], target[0]) == 0) && (p[1] != ':')) break; } p += strlen(p) + 1; } i = strlen(p); if (i == 0) { uprintf("Could not find partition mapping for %s", target[0]); goto out; } while ((--i > 0) && (isdigit(p[i]))); p[++i] = '0' + part_nr; p[++i] = 0; target[0][0] = 0; if (!QueryDosDeviceA(p, target[0], MAX_PATH) || (strlen(target[0]) == 0)) { uprintf("Could not find the DOS volume name for partition '%s': %s", p, WindowsErrorString()); goto out; } if (!DefineDosDeviceA(DDD_RAW_TARGET_PATH | DDD_NO_BROADCAST_SYSTEM, mounted_drive, target[0])) { uprintf("Could not mount '%s' to '%s': %s", target[0], mounted_drive, WindowsErrorString()); goto out; } uprintf("Successfully mounted '%s' (USB partition %d) as '%s'", target[0], part_nr, mounted_drive); ret = mounted_drive; out: safe_free(buffer); return ret; }
/* * Return the first GUID volume name for the associated drive or NULL if not found * See http://msdn.microsoft.com/en-us/library/cc542456.aspx * The returned string is allocated and must be freed */ char* GetLogicalName(DWORD DriveIndex, BOOL bKeepTrailingBackslash, BOOL bSilent) { BOOL success = FALSE; char volume_name[MAX_PATH]; HANDLE hDrive = INVALID_HANDLE_VALUE, hVolume = INVALID_HANDLE_VALUE; size_t len; char path[MAX_PATH]; VOLUME_DISK_EXTENTS DiskExtents; DWORD size; UINT drive_type; int i, j; static const char* ignore_device[] = { "\\Device\\CdRom", "\\Device\\Floppy" }; static const char* volume_start = "\\\\?\\"; CheckDriveIndex(DriveIndex); for (i=0; hDrive == INVALID_HANDLE_VALUE; i++) { if (i == 0) { hVolume = FindFirstVolumeA(volume_name, sizeof(volume_name)); if (hVolume == INVALID_HANDLE_VALUE) { suprintf("Could not access first GUID volume: %s\n", WindowsErrorString()); goto out; } } else { if (!FindNextVolumeA(hVolume, volume_name, sizeof(volume_name))) { if (GetLastError() != ERROR_NO_MORE_FILES) { suprintf("Could not access next GUID volume: %s\n", WindowsErrorString()); } goto out; } } // Sanity checks len = safe_strlen(volume_name); if ((len <= 1) || (safe_strnicmp(volume_name, volume_start, 4) != 0) || (volume_name[len-1] != '\\')) { suprintf("'%s' is not a GUID volume name\n", volume_name); continue; } drive_type = GetDriveTypeA(volume_name); if ((drive_type != DRIVE_REMOVABLE) && (drive_type != DRIVE_FIXED)) continue; volume_name[len-1] = 0; if (QueryDosDeviceA(&volume_name[4], path, sizeof(path)) == 0) { suprintf("Failed to get device path for GUID volume '%s': %s\n", volume_name, WindowsErrorString()); continue; } for (j=0; (j<ARRAYSIZE(ignore_device)) && (_strnicmp(path, ignore_device[j], safe_strlen(ignore_device[j])) != 0); j++); if (j < ARRAYSIZE(ignore_device)) { suprintf("Skipping GUID volume for '%s'\n", path); continue; } // If we can't have FILE_SHARE_WRITE, forget it hDrive = CreateFileA(volume_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hDrive == INVALID_HANDLE_VALUE) { suprintf("Could not open GUID volume '%s': %s\n", volume_name, WindowsErrorString()); continue; } if ((!DeviceIoControl(hDrive, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0, &DiskExtents, sizeof(DiskExtents), &size, NULL)) || (size <= 0)) { suprintf("Could not get Disk Extents: %s\n", WindowsErrorString()); safe_closehandle(hDrive); continue; } safe_closehandle(hDrive); if ((DiskExtents.NumberOfDiskExtents >= 1) && (DiskExtents.Extents[0].DiskNumber == DriveIndex)) { if (bKeepTrailingBackslash) volume_name[len-1] = '\\'; success = TRUE; break; } } out: if (hVolume != INVALID_HANDLE_VALUE) FindVolumeClose(hVolume); return (success)?safe_strdup(volume_name):NULL; }
// Return a list of all serial ports wxArrayString Serial::port_list() { wxArrayString list; #if defined(LINUX) // This is ugly guessing, but Linux doesn't seem to provide anything else. // If there really is an API to discover serial devices on Linux, please // email [email protected] with the info. Please? // The really BAD aspect is all ports get DTR raised briefly, because linux // has no way to open the port without raising DTR, and there isn't any way // to tell if the device file really represents hardware without opening it. // maybe sysfs or udev provides a useful API?? DIR *dir; struct dirent *f; struct stat st; unsigned int i, len[NUM_DEVNAMES]; char s[512]; int fd, bits; termios mytios; dir = opendir("/dev/"); if (dir == NULL) return list; for (i=0; i<NUM_DEVNAMES; i++) len[i] = strlen(devnames[i]); // Read all the filenames from the /dev directory... while ((f = readdir(dir)) != NULL) { // ignore everything that doesn't begin with "tty" if (strncmp(f->d_name, "tty", 3)) continue; // ignore anything that's not a known serial device name for (i=0; i<NUM_DEVNAMES; i++) { if (!strncmp(f->d_name + 3, devnames[i], len[i])) break; } if (i >= NUM_DEVNAMES) continue; snprintf(s, sizeof(s), "/dev/%s", f->d_name); // check if it's a character type device (almost certainly is) if (stat(s, &st) != 0 || !(st.st_mode & S_IFCHR)) continue; // now see if we can open the file - if the device file is // populating /dev but doesn't actually represent a loaded // driver, this is where we will detect it. fd = open(s, O_RDONLY | O_NOCTTY | O_NONBLOCK); if (fd < 0) { // if permission denied, give benefit of the doubt // (otherwise the port will be invisible to the user // and we won't have a to alert them to the permssion // problem) if (errno == EACCES) list.Add(wxString(s,wxConvUTF8)); // any other error, assume it's not a real device continue; } // does it respond to termios requests? (probably will since // the name began with tty). Some devices where a single // driver exports multiple names will open but this is where // we can really tell if they work with real hardare. if (tcgetattr(fd, &mytios) != 0) { close(fd); continue; } // does it respond to reading the control signals? If it's // some sort of non-serial terminal (eg, pseudo terminals) // this is where we will detect it's not really a serial port if (ioctl(fd, TIOCMGET, &bits) < 0) { close(fd); continue; } // it passed all the tests, it's a serial port, or some sort // of "terminal" that looks exactly like a real serial port! close(fd); // unfortunately, Linux always raises DTR when open is called. // not nice! Every serial port is going to get DTR raised // and then lowered. I wish there were a way to prevent this, // but it seems impossible. list.Add(wxString(s,wxConvUTF8)); } closedir(dir); #elif defined(MACOSX) // adapted from SerialPortSample.c, by Apple // http://developer.apple.com/samplecode/SerialPortSample/listing2.html // and also testserial.c, by Keyspan // http://www.keyspan.com/downloads-files/developer/macosx/KesypanTestSerial.c // www.rxtx.org, src/SerialImp.c seems to be based on Keyspan's testserial.c // neither keyspan nor rxtx properly release memory allocated. // more documentation at: // http://developer.apple.com/documentation/DeviceDrivers/Conceptual/WorkingWSerial/WWSerial_SerialDevs/chapter_2_section_6.html mach_port_t masterPort; CFMutableDictionaryRef classesToMatch; io_iterator_t serialPortIterator; if (IOMasterPort(NULL, &masterPort) != KERN_SUCCESS) return list; // a usb-serial adaptor is usually considered a "modem", // especially when it implements the CDC class spec classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue); if (!classesToMatch) return list; CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDModemType)); if (IOServiceGetMatchingServices(masterPort, classesToMatch, &serialPortIterator) != KERN_SUCCESS) return list; macos_ports(&serialPortIterator, list); IOObjectRelease(serialPortIterator); // but it might be considered a "rs232 port", so repeat this // search for rs232 ports classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue); if (!classesToMatch) return list; CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDRS232Type)); if (IOServiceGetMatchingServices(masterPort, classesToMatch, &serialPortIterator) != KERN_SUCCESS) return list; macos_ports(&serialPortIterator, list); IOObjectRelease(serialPortIterator); #elif defined(WINDOWS) // http://msdn.microsoft.com/en-us/library/aa365461(VS.85).aspx // page with 7 ways - not all of them work! // http://www.naughter.com/enumser.html // may be possible to just query the windows registary // http://it.gps678.com/2/ca9c8631868fdd65.html // search in HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM // Vista has some special new way, vista-only // http://msdn2.microsoft.com/en-us/library/aa814070(VS.85).aspx char *buffer, *p; //DWORD size = QUERYDOSDEVICE_BUFFER_SIZE; DWORD ret; buffer = (char *)malloc(QUERYDOSDEVICE_BUFFER_SIZE); if (buffer == NULL) return list; memset(buffer, 0, QUERYDOSDEVICE_BUFFER_SIZE); ret = QueryDosDeviceA(NULL, buffer, QUERYDOSDEVICE_BUFFER_SIZE); if (ret) { printf("Detect Serial using QueryDosDeviceA: "); for (p = buffer; *p; p += strlen(p) + 1) { printf(": %s", p); if (strncmp(p, "COM", 3)) continue; list.Add(wxString(p) + ":"); } } else { char buf[1024]; win32_err(buf); printf("QueryDosDeviceA failed, error \"%s\"\n", buf); printf("Detect Serial using brute force GetDefaultCommConfig probing: "); for (int i=1; i<=32; i++) { printf("try %s", buf); COMMCONFIG cfg; DWORD len; snprintf(buf, sizeof(buf), "COM%d", i); if (GetDefaultCommConfig(buf, &cfg, &len)) { wxString name; name.Printf("COM%d:", i); list.Add(name); printf(": %s", buf); } } } free(buffer); #endif list.Sort(); return list; }
static void test_GetMappedFileName(void) { HMODULE hMod = GetModuleHandleA(NULL); char szMapPath[MAX_PATH], szModPath[MAX_PATH], *szMapBaseName; DWORD ret; char *base; char temp_path[MAX_PATH], file_name[MAX_PATH], map_name[MAX_PATH], device_name[MAX_PATH], drive[3]; WCHAR map_nameW[MAX_PATH], nt_map_name[MAX_PATH]; HANDLE hfile, hmap; SetLastError(0xdeadbeef); ret = pGetMappedFileNameA(NULL, hMod, szMapPath, sizeof(szMapPath)); ok(!ret, "GetMappedFileName should fail\n"); todo_wine ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError()); SetLastError(0xdeadbeef); ret = pGetMappedFileNameA(hpSR, hMod, szMapPath, sizeof(szMapPath)); ok(!ret, "GetMappedFileName should fail\n"); todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError()); SetLastError( 0xdeadbeef ); ret = pGetMappedFileNameA(hpQI, hMod, szMapPath, sizeof(szMapPath)); todo_wine ok( ret || broken(GetLastError() == ERROR_UNEXP_NET_ERR), /* win2k */ "GetMappedFileNameA failed with error %u\n", GetLastError() ); if (ret) { ok(ret == strlen(szMapPath), "szMapPath=\"%s\" ret=%d\n", szMapPath, ret); todo_wine ok(szMapPath[0] == '\\', "szMapPath=\"%s\"\n", szMapPath); szMapBaseName = strrchr(szMapPath, '\\'); /* That's close enough for us */ todo_wine ok(szMapBaseName && *szMapBaseName, "szMapPath=\"%s\"\n", szMapPath); if (szMapBaseName) { GetModuleFileNameA(NULL, szModPath, sizeof(szModPath)); ok(!strcmp(strrchr(szModPath, '\\'), szMapBaseName), "szModPath=\"%s\" szMapBaseName=\"%s\"\n", szModPath, szMapBaseName); } } GetTempPathA(MAX_PATH, temp_path); GetTempFileNameA(temp_path, "map", 0, file_name); drive[0] = file_name[0]; drive[1] = ':'; drive[2] = 0; SetLastError(0xdeadbeef); ret = QueryDosDeviceA(drive, device_name, sizeof(device_name)); ok(ret, "QueryDosDeviceA error %d\n", GetLastError()); trace("%s -> %s\n", drive, device_name); SetLastError(0xdeadbeef); hfile = CreateFileA(file_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0); ok(hfile != INVALID_HANDLE_VALUE, "CreateFileA(%s) error %d\n", file_name, GetLastError()); SetFilePointer(hfile, 0x4000, NULL, FILE_BEGIN); SetEndOfFile(hfile); SetLastError(0xdeadbeef); hmap = CreateFileMappingA(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL); ok(hmap != 0, "CreateFileMappingA error %d\n", GetLastError()); SetLastError(0xdeadbeef); base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0); ok(base != NULL, "MapViewOfFile error %d\n", GetLastError()); SetLastError(0xdeadbeef); ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, 0); ok(!ret, "GetMappedFileName should fail\n"); todo_wine ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "wrong error %d\n", GetLastError()); SetLastError(0xdeadbeef); ret = pGetMappedFileNameA(GetCurrentProcess(), base, 0, sizeof(map_name)); ok(!ret, "GetMappedFileName should fail\n"); todo_wine ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError()); SetLastError(0xdeadbeef); ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, 1); todo_wine ok(ret == 1, "GetMappedFileName error %d\n", GetLastError()); ok(!map_name[0] || broken(map_name[0] == device_name[0]) /* before win2k */, "expected 0, got %c\n", map_name[0]); SetLastError(0xdeadbeef); ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name)); todo_wine { ok(ret, "GetMappedFileName error %d\n", GetLastError()); ok(ret > strlen(device_name), "map_name should be longer than device_name\n"); ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name); } SetLastError(0xdeadbeef); ret = pGetMappedFileNameW(GetCurrentProcess(), base, map_nameW, sizeof(map_nameW)/sizeof(map_nameW[0])); todo_wine { ok(ret, "GetMappedFileNameW error %d\n", GetLastError()); ok(ret > strlen(device_name), "map_name should be longer than device_name\n"); } if (nt_get_mapped_file_name(GetCurrentProcess(), base, nt_map_name, sizeof(nt_map_name)/sizeof(nt_map_name[0]))) { ok(memcmp(map_nameW, nt_map_name, lstrlenW(map_nameW)) == 0, "map name does not start with a device name: %s\n", map_name); WideCharToMultiByte(CP_ACP, 0, map_nameW, -1, map_name, MAX_PATH, NULL, NULL); ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name); } SetLastError(0xdeadbeef); ret = pGetMappedFileNameA(GetCurrentProcess(), base + 0x2000, map_name, sizeof(map_name)); todo_wine { ok(ret, "GetMappedFileName error %d\n", GetLastError()); ok(ret > strlen(device_name), "map_name should be longer than device_name\n"); ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name); } SetLastError(0xdeadbeef); ret = pGetMappedFileNameA(GetCurrentProcess(), base + 0x4000, map_name, sizeof(map_name)); ok(!ret, "GetMappedFileName should fail\n"); todo_wine ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError()); SetLastError(0xdeadbeef); ret = pGetMappedFileNameA(GetCurrentProcess(), NULL, map_name, sizeof(map_name)); ok(!ret, "GetMappedFileName should fail\n"); todo_wine ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError()); SetLastError(0xdeadbeef); ret = pGetMappedFileNameA(0, base, map_name, sizeof(map_name)); ok(!ret, "GetMappedFileName should fail\n"); todo_wine ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError()); UnmapViewOfFile(base); CloseHandle(hmap); CloseHandle(hfile); DeleteFileA(file_name); SetLastError(0xdeadbeef); hmap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READONLY | SEC_COMMIT, 0, 4096, NULL); ok(hmap != 0, "CreateFileMappingA error %d\n", GetLastError()); SetLastError(0xdeadbeef); base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0); ok(base != NULL, "MapViewOfFile error %d\n", GetLastError()); SetLastError(0xdeadbeef); ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name)); ok(!ret, "GetMappedFileName should fail\n"); todo_wine ok(GetLastError() == ERROR_FILE_INVALID, "expected ERROR_FILE_INVALID, got %d\n", GetLastError()); UnmapViewOfFile(base); CloseHandle(hmap); }
static void test_GetVolumeNameForVolumeMountPointA(void) { BOOL ret; char volume[MAX_PATH], path[] = "c:\\"; DWORD len = sizeof(volume), reti; char temp_path[MAX_PATH]; /* not present before w2k */ if (!pGetVolumeNameForVolumeMountPointA) { skip("GetVolumeNameForVolumeMountPointA not found\n"); return; } reti = GetTempPathA(MAX_PATH, temp_path); ok(reti != 0, "GetTempPathA error %d\n", GetLastError()); ok(reti < MAX_PATH, "temp path should fit into MAX_PATH\n"); ret = pGetVolumeNameForVolumeMountPointA(path, volume, 0); ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n"); ok(GetLastError() == ERROR_FILENAME_EXCED_RANGE || GetLastError() == ERROR_INVALID_PARAMETER, /* Vista */ "wrong error, last=%d\n", GetLastError()); if (0) { /* these crash on XP */ ret = pGetVolumeNameForVolumeMountPointA(path, NULL, len); ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n"); ret = pGetVolumeNameForVolumeMountPointA(NULL, volume, len); ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n"); } ret = pGetVolumeNameForVolumeMountPointA(path, volume, len); ok(ret == TRUE, "GetVolumeNameForVolumeMountPointA failed\n"); ok(!strncmp( volume, "\\\\?\\Volume{", 11), "GetVolumeNameForVolumeMountPointA failed to return valid string <%s>\n", volume); /* test with too small buffer */ ret = pGetVolumeNameForVolumeMountPointA(path, volume, 10); ok(ret == FALSE && GetLastError() == ERROR_FILENAME_EXCED_RANGE, "GetVolumeNameForVolumeMountPointA failed, wrong error returned, was %d, should be ERROR_FILENAME_EXCED_RANGE\n", GetLastError()); /* Try on a arbitrary directory */ /* On FAT filesystems it seems that GetLastError() is set to ERROR_INVALID_FUNCTION. */ ret = pGetVolumeNameForVolumeMountPointA(temp_path, volume, len); ok(ret == FALSE && (GetLastError() == ERROR_NOT_A_REPARSE_POINT || GetLastError() == ERROR_INVALID_FUNCTION), "GetVolumeNameForVolumeMountPointA failed on %s, last=%d\n", temp_path, GetLastError()); /* Try on a nonexistent dos drive */ path[2] = 0; for (;path[0] <= 'z'; path[0]++) { ret = QueryDosDeviceA( path, volume, len); if(!ret) break; } if (path[0] <= 'z') { path[2] = '\\'; ret = pGetVolumeNameForVolumeMountPointA(path, volume, len); ok(ret == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND, "GetVolumeNameForVolumeMountPointA failed on %s, last=%d\n", path, GetLastError()); /* Try without trailing \ and on a nonexistent dos drive */ path[2] = 0; ret = pGetVolumeNameForVolumeMountPointA(path, volume, len); ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME, "GetVolumeNameForVolumeMountPointA failed on %s, last=%d\n", path, GetLastError()); } }
BOOL CProcessControl::Get( __in BOOL bCurrentProc, __in ULONG ulPid, __out LPSTR lpOutBuf, __in ULONG ulOutBufSizeCh ) { BOOL bRet = FALSE; HANDLE hProc = NULL; DWORD dwProcPathLenCh = 0; CHAR chProcPathDev[MAX_PATH] = {0}; CHAR chVolNameDev[MAX_PATH] = {0}; CHAR chVolName[MAX_PATH] = {0}; __try { if (!lpOutBuf || !ulOutBufSizeCh || (!bCurrentProc && !ulPid)) { printfPublic("input arguments error. %d %d 0x%p %d", bCurrentProc, ulPid, lpOutBuf, ulOutBufSizeCh); __leave; } ZeroMemory(lpOutBuf, ulOutBufSizeCh * sizeof(CHAR)); if (bCurrentProc) { if (!CModulePath::Get(NULL, lpOutBuf, ulOutBufSizeCh)) { printfPublic("Get failed"); __leave; } bRet = TRUE; __leave; } hProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, ulPid); if (!hProc) { printfPublic("OpenProcess failed. (%d)", GetLastError()); __leave; } if (m_QueryFullProcessImageNameA) { dwProcPathLenCh = ulOutBufSizeCh; if (!m_QueryFullProcessImageNameA(hProc, 0, lpOutBuf, &dwProcPathLenCh)) { printfPublic("QueryFullProcessImageName failed. (%d)", GetLastError()); __leave; } bRet = TRUE; __leave; } if (!GetProcessImageFileNameA(hProc, chProcPathDev, _countof(chProcPathDev))) { printfPublic("GetProcessImageFileName failed. (%d)", GetLastError()); __leave; } strcat_s(chVolName, _countof(chVolName), "A:"); for (; _T('Z') >= *chVolName; (*chVolName)++) { ZeroMemory(chVolNameDev, sizeof(chVolNameDev)); if (!QueryDosDeviceA(chVolName, chVolNameDev, _countof(chVolNameDev))) { if (2 == GetLastError()) continue; else { printfPublic("QueryDosDevice failed. (%d)", GetLastError()); __leave; } } if (0 == _strnicmp(chProcPathDev, chVolNameDev, strlen(chVolNameDev))) { bRet = TRUE; break; } } if (bRet) { strcat_s(lpOutBuf, ulOutBufSizeCh, chVolName); strcat_s(lpOutBuf, ulOutBufSizeCh, chProcPathDev + strlen(chVolNameDev)); } } __finally { if (hProc) { CloseHandle(hProc); hProc = NULL; } } return bRet; }