Example #1
0
File: mode.c Project: GYGit/reactos
int ShowParallelStatus(int nPortNum)
{
    WCHAR buffer[250];
    WCHAR szPortName[MAX_PORTNAME_LEN];

    swprintf(szPortName, L"LPT%d", nPortNum);
    wprintf(L"\nStatus for device LPT%d:\n", nPortNum);
    wprintf(L"-----------------------\n");
    if (QueryDosDeviceW(szPortName, buffer, ARRAYSIZE(buffer)))
    {
        WCHAR* ptr = wcsrchr(buffer, L'\\');
        if (ptr != NULL)
        {
            if (0 == wcscmp(szPortName, ++ptr))
            {
                wprintf(L"    Printer output is not being rerouted.\n");
            }
            else
            {
                wprintf(L"    Printer output is being rerouted to serial port %s\n", ptr);
            }
            return 0;
        }
        else
        {
            wprintf(L"    QueryDosDeviceW(%s) returned unrecognised form %s.\n", szPortName, buffer);
        }
    }
    else
    {
        wprintf(L"    ERROR: QueryDosDeviceW(%s) failed: 0x%lx\n", szPortName, GetLastError());
    }
    return 1;
}
Example #2
0
static WCHAR FspNpGetDriveLetter(PDWORD PLogicalDrives, PWSTR VolumeName)
{
    WCHAR VolumeNameBuf[MAX_PATH];
    WCHAR LocalNameBuf[3];
    WCHAR Drive;

    if (0 == *PLogicalDrives)
        return 0;

    LocalNameBuf[1] = L':';
    LocalNameBuf[2] = L'\0';

    for (Drive = 'Z'; 'A' <= Drive; Drive--)
        if (0 != (*PLogicalDrives & (1 << (Drive - 'A'))))
        {
            LocalNameBuf[0] = Drive;
            if (QueryDosDeviceW(LocalNameBuf, VolumeNameBuf, sizeof VolumeNameBuf / sizeof(WCHAR)))
            {
                if (0 == lstrcmpW(VolumeNameBuf, VolumeName))
                {
                    *PLogicalDrives &= ~(1 << (Drive - 'A'));
                    return Drive;
                }
            }
        }

    return 0;
}
	void ProcessManager::InitDriveMappings()
	{
		wchar_t volumeName[100];
		wchar_t deviceName[150];
		wchar_t pathNames[100];

		HANDLE fileHandle = FindFirstVolumeW(volumeName, sizeof(volumeName));
		if (fileHandle == INVALID_HANDLE_VALUE)
			return;
		do
		{
			auto index = wcslen(volumeName) - 1;
			volumeName[index] = L'\0';
			auto charCount = QueryDosDeviceW(&volumeName[4], deviceName, sizeof(deviceName));
			volumeName[index] = L'\\';
			DWORD returned;
			if (GetVolumePathNamesForVolumeNameW(volumeName, pathNames, sizeof(pathNames), &returned))
			{
				DriveMappings[wxString(deviceName)] = wxString(pathNames);
			}


		} while (FindNextVolumeW(fileHandle, volumeName, sizeof(volumeName)));
		FindClose(fileHandle);
	}
Example #4
0
static std::wstring
GetDeviceMapping(const std::wstring& aDosDeviceName)
{
  wchar_t buf[MAX_PATH] = { 0 };
  DWORD rv = QueryDosDeviceW(aDosDeviceName.c_str(), buf, MAX_PATH);
  if (rv == 0) {
    return std::wstring(L"");
  }
  return std::wstring(buf, buf + rv);
}
Example #5
0
/* real hardware */
static GList *
get_connected_drives (GVolumeMonitor *volume_monitor)
{
  GList *list = NULL;

#if 0
  HANDLE  find_handle;
  BOOL    found;
  wchar_t wc_name[MAX_PATH+1];
  
  find_handle = FindFirstVolumeW (wc_name, MAX_PATH);
  found = (find_handle != INVALID_HANDLE_VALUE);
  while (found)
    {
      /* I don't know what this code is supposed to do; clearly it now
       * does nothing, the returned GList is always NULL. But what was
       * this code supposed to be a start of? The volume names that
       * the FindFirstVolume/FindNextVolume loop iterates over returns
       * device names like
       *
       *   \Device\HarddiskVolume1
       *   \Device\HarddiskVolume2
       *   \Device\CdRom0
       *
       * No DOS devices there, so I don't see the point with the
       * QueryDosDevice call below. Probably this code is confusing volumes
       * with something else that does contain the mapping from DOS devices
       * to volumes.
       */
      wchar_t wc_dev_name[MAX_PATH+1];
      guint trailing = wcslen (wc_name) - 1;

      /* remove trailing backslash and leading \\?\\ */
      wc_name[trailing] = L'\0';
      if (QueryDosDeviceW (&wc_name[4], wc_dev_name, MAX_PATH))
        {
          gchar *name = g_utf16_to_utf8 (wc_dev_name, -1, NULL, NULL, NULL);
          g_print ("%s\n", name);
	  g_free (name);
	}

      found = FindNextVolumeW (find_handle, wc_name, MAX_PATH);
    }
  if (find_handle != INVALID_HANDLE_VALUE)
    FindVolumeClose (find_handle);
#endif

  return list;
}
Example #6
0
bool get_device_path( const wstring& volume_guid_path, wstring& volume_dev_path ) {
	const wstring c_prefix( L"\\\\?\\" );
	if ( volume_guid_path.size( ) < c_prefix.size( ) || volume_guid_path.substr( 0, c_prefix.size( ) ) != c_prefix ) {
		return false;
		}
	unsigned buf_size = MAX_PATH;
	unique_ptr<wchar_t[ ]> buffer( new wchar_t[ buf_size ] );
	SetLastError( NO_ERROR );
	DWORD len = QueryDosDeviceW( del_trailing_slash( volume_guid_path ).substr( 4 ).c_str( ), buffer.get( ), buf_size );
	if ( len == 0 || GetLastError( ) != NO_ERROR ) {
		return false;
		}
	volume_dev_path.assign( buffer.get( ) );
	return true;
	}
Example #7
0
static
BOOL
DCB_ValidPort(unsigned long nPort)
{
	BOOL bRet;
	DWORD dwErr;
	WCHAR szPort[3 + 10 + 1];

	dwErr = GetLastError();

	_snwprintf(szPort, sizeof(szPort) / sizeof(szPort[0]), L"COM%lu", nPort);

	bRet = QueryDosDeviceW(szPort, NULL, 0) == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER;

	if(!bRet)
		dwErr = ERROR_INVALID_PARAMETER;

	SetLastError(dwErr);
	return bRet;
}
bool
DriveToNtPath(const wchar_t aDriveLetter, nsAString& aNtPath)
{
  const wchar_t drvTpl[] = {aDriveLetter, L':', L'\0'};
  aNtPath.SetLength(MAX_PATH);
  DWORD pathLen;
  while (true) {
    pathLen = QueryDosDeviceW(drvTpl, aNtPath.BeginWriting(), aNtPath.Length());
    if (pathLen || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
      break;
    }
    aNtPath.SetLength(aNtPath.Length() * 2);
  }
  if (!pathLen) {
    return false;
  }
  // aNtPath contains embedded NULLs, so we need to figure out the real length
  // via wcslen.
  aNtPath.SetLength(wcslen(aNtPath.BeginReading()));
  return true;
}
Example #9
0
File: mode.c Project: GYGit/reactos
int QueryDevices()
{
    WCHAR buffer[20240];
    int len;
    WCHAR* ptr = buffer;

    *ptr = L'\0';
    if (QueryDosDeviceW(NULL, buffer, ARRAYSIZE(buffer)))
    {
        while (*ptr != L'\0')
        {
            len = wcslen(ptr);
            if (wcsstr(ptr, L"COM"))
            {
                wprintf(L"    Found serial device - %s\n", ptr);
            }
            else if (wcsstr(ptr, L"PRN"))
            {
                wprintf(L"    Found printer device - %s\n", ptr);
            }
            else if (wcsstr(ptr, L"LPT"))
            {
                wprintf(L"    Found parallel device - %s\n", ptr);
            }
            else
            {
                // wprintf(L"    Found other device - %s\n", ptr);
            }
            ptr += (len+1);
        }
    }
    else
    {
        wprintf(L"    ERROR: QueryDosDeviceW(...) failed: 0x%lx\n", GetLastError());
    }
    return 1;
}
Example #10
0
DWORD APIENTRY NPGetConnection(
    LPWSTR lpLocalName, LPWSTR lpRemoteName, LPDWORD lpnBufferLen)
{
    DWORD NpResult;
    NTSTATUS Result;
    WCHAR LocalNameBuf[3];
    WCHAR VolumeNameBuf[FSP_FSCTL_VOLUME_NAME_SIZEMAX / sizeof(WCHAR)];
    PWCHAR VolumeListBuf = 0, VolumeListBufEnd, VolumeName, P;
    SIZE_T VolumeListSize, VolumeNameSize;
    ULONG Backslashes;

    if (!FspNpCheckLocalName(lpLocalName))
        return WN_BAD_LOCALNAME;

    LocalNameBuf[0] = lpLocalName[0] & ~0x20; /* convert to uppercase */
    LocalNameBuf[1] = L':';
    LocalNameBuf[2] = L'\0';

    if (0 == QueryDosDeviceW(LocalNameBuf, VolumeNameBuf, sizeof VolumeNameBuf))
        return WN_NOT_CONNECTED;

    Result = FspNpGetVolumeList(&VolumeListBuf, &VolumeListSize);
    if (!NT_SUCCESS(Result))
        return WN_OUT_OF_MEMORY;

    NpResult = WN_NOT_CONNECTED;
    for (P = VolumeListBuf, VolumeListBufEnd = (PVOID)((PUINT8)P + VolumeListSize), VolumeName = P;
        VolumeListBufEnd > P; P++)
    {
        if (L'\0' == *P)
        {
            if (0 == lstrcmpW(VolumeNameBuf, VolumeName))
            {
                /*
                 * Looks like this is a WinFsp device. Extract the VolumePrefix from the VolumeName.
                 *
                 * The VolumeName will have the following syntax:
                 *     \Device\Volume{GUID}\Server\Share
                 *
                 * We want to extract the \Server\Share part. We will simply count backslashes and
                 * stop at the third one. Since we are about to break this loop, it is ok to mess
                 * with the loop variables.
                 */

                for (Backslashes = 0; VolumeName < P; VolumeName++)
                    if (L'\\' == *VolumeName)
                        if (3 == ++Backslashes)
                            break;

                if (3 == Backslashes)
                {
                    VolumeNameSize = lstrlenW(VolumeName) + 1/* term-0 */;
                    if (*lpnBufferLen >= 1/* lead-\ */ + VolumeNameSize)
                    {
                        *lpRemoteName = L'\\';
                        memcpy(lpRemoteName + 1, VolumeName, VolumeNameSize * sizeof(WCHAR));
                        NpResult = WN_SUCCESS;
                    }
                    else
                    {
                        *lpnBufferLen = (DWORD)(1/* lead-\ */ + VolumeNameSize);
                        NpResult = WN_MORE_DATA;
                    }
                }

                break;
            }
            else
                VolumeName = P + 1;
        }
    }

    MemFree(VolumeListBuf);

    return NpResult;
}
Example #11
0
File: query.c Project: jaykrell/j
int
__cdecl
wmain()
{
    DWORD Size1 = { 0 };
    DWORD Size2 = { 0 };
    PWSTR Buffer1 = { 0 };
    PWSTR Buffer2 = { 0 };
    PWSTR p;
    PWSTR q;

    Size2 = 32;

    while (TRUE)
    {
        free(Buffer1);
        Buffer1 = NULL;
        Buffer1 = (PWSTR) calloc(Size2, sizeof(WCHAR));
        if (Buffer1 == NULL)
            OutOfMemory();

        Size1 = QueryDosDeviceW(NULL, Buffer1, Size2);
        if (Size1 >= Size2)
        {
            Size2 = (Size1 + 1);
        }
        else if (Size1 == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            Size2 = ((Size2 * 2) + 1);
        }
        else
        {
            break;
        }
    }
    for (p = Buffer1; *p ; (p += wcslen(p) + 1))
    {
        Size2 = 32;
        while (TRUE)
        {
            free(Buffer2);
            Buffer2 = NULL;
            Buffer2 = (PWSTR) calloc(Size2, sizeof(WCHAR));
            if (Buffer2 == NULL)
                OutOfMemory();
            Size1 = QueryDosDeviceW(p, Buffer2, Size2);
            if (Size1 >= Size2)
            {
                Size2 = (Size1 + 1);
            }
            else if (Size1 == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
                Size2 = ((Size2 * 2) + 1);
            }
            else
            {
                break;
            }
        }
        for (q = Buffer2; *q ; (q += wcslen(q) + 1))
        {
            wprintf(L"%ls => %ls\n", p, q);
        }
    }
    free(Buffer1);
    free(Buffer2);
    return 0;
}
Example #12
0
int wmain(int argc, WCHAR *argv[])
{
	_NtQuerySystemInformation NtQuerySystemInformation =
		(_NtQuerySystemInformation)GetLibraryProcAddress("ntdll.dll", "NtQuerySystemInformation");
	_NtDuplicateObject NtDuplicateObject =
		(_NtDuplicateObject)GetLibraryProcAddress("ntdll.dll", "NtDuplicateObject");
	_NtQueryObject NtQueryObject =
		(_NtQueryObject)GetLibraryProcAddress("ntdll.dll", "NtQueryObject");
	NTSTATUS status;
	PSYSTEM_HANDLE_INFORMATION handleInfo;
	ULONG handleInfoSize = 0x10000;
	ULONG pid = 0;
	HANDLE processHandle;
	ULONG i;

	if (argc < 2)
	{
		printf("Usage: handles filepath [pid]\n");
		return 1;
	}

	if (argc > 2)
	{
		pid = _wtoi(argv[2]);
	}

	// convert C:\Windows\System32 to \Device\HarddiskVolume1\Windows\System32
	const WCHAR* filePath = argv[1];
	if (wcslen(filePath) < 2 || filePath[1] != L':')
	{
		printf("Can't process input path which is tool short or not contain local driver!\n");
		return 1;
	}
	PWSTR pDosDriveName = new TCHAR[MAX_PATH];
	TCHAR szDrive[3] = TEXT(" :");
	szDrive[0] = filePath[0];
	DWORD uiLen = QueryDosDeviceW(szDrive, pDosDriveName, MAX_PATH);
	if (0 == uiLen)
	{
		if (ERROR_INSUFFICIENT_BUFFER != GetLastError())
		{
			printf("QueryDosDeviceW failed: %d\n", GetLastError());
			return 1;
		}

		delete[]pDosDriveName;
		pDosDriveName = new TCHAR[uiLen + 1];
		uiLen = QueryDosDevice(szDrive, pDosDriveName, uiLen + 1);
		if (0 == uiLen)
		{
			printf("QueryDosDeviceW failed: %d\n", GetLastError());
			return 1;
		}
	}
	wcscat(pDosDriveName, &filePath[2]);

	handleInfo = (PSYSTEM_HANDLE_INFORMATION)malloc(handleInfoSize);
	/* NtQuerySystemInformation won't give us the correct buffer size,
	so we guess by doubling the buffer size. */
	while ((status = NtQuerySystemInformation(
		SystemHandleInformation,
		handleInfo,
		handleInfoSize,
		NULL
	)) == STATUS_INFO_LENGTH_MISMATCH)
		handleInfo = (PSYSTEM_HANDLE_INFORMATION)realloc(handleInfo, handleInfoSize *= 2);

	/* NtQuerySystemInformation stopped giving us STATUS_INFO_LENGTH_MISMATCH. */
	if (!NT_SUCCESS(status))
	{
		printf("NtQuerySystemInformation failed!\n");
		return 1;
	}

	for (i = 0; i < handleInfo->HandleCount; i++)
	{
		SYSTEM_HANDLE handle = handleInfo->Handles[i];
		HANDLE dupHandle = NULL;
		POBJECT_TYPE_INFORMATION objectTypeInfo;
		PVOID objectNameInfo;
		UNICODE_STRING objectName;
		ULONG returnLength;

		// Jump of no file
		/*if (handle.ObjectTypeNumber != 31)
		{
			continue;
		}*/

		/* Check if this handle belongs to the PID the user specified. */
		if (pid != 0 && handle.ProcessId != pid)
			continue;

		if (!(processHandle = OpenProcess(PROCESS_DUP_HANDLE, FALSE, handle.ProcessId)))
		{
			//printf("Could not open PID %d! (Don't try to open a system process.)\n", handle.ProcessId);
			continue;
		}

		/* Duplicate the handle so we can query it. */
		DWORD re = NtDuplicateObject(
			processHandle,
			(HANDLE)handle.Handle,
			GetCurrentProcess(),
			&dupHandle,
			0,
			0,
			0
		);
		if (ERROR_SUCCESS != re)
		{
			printf("[%#x] Error!\n", handle.Handle);
			continue;
		}

		/* Query the object type. */
		objectTypeInfo = (POBJECT_TYPE_INFORMATION)malloc(0x1000);
		if (!NT_SUCCESS(NtQueryObject(
			dupHandle,
			ObjectTypeInformation,
			objectTypeInfo,
			0x1000,
			NULL
		)))
		{
			printf("[%#x] Error!\n", handle.Handle);
			CloseHandle(dupHandle);
			continue;
		}

		/* Query the object name (unless it has an access of
		0x0012019f, on which NtQueryObject could hang. */
		if (handle.GrantedAccess == 0x0012019f)
		{
			/* We have the type, so display that. */
			printf(
				"[%#x] %.*S: (did not get name)\n",
				handle.Handle,
				objectTypeInfo->Name.Length / 2,
				objectTypeInfo->Name.Buffer
			);
			free(objectTypeInfo);
			CloseHandle(dupHandle);
			continue;
		}

		objectNameInfo = malloc(0x1000);
		if (!NT_SUCCESS(MyNtQueryObject(
			NtQueryObject,
			dupHandle,
			ObjectNameInformation,
			objectNameInfo,
			0x1000,
			&returnLength
		)))
		{
			/* Reallocate the buffer and try again. */
			objectNameInfo = realloc(objectNameInfo, returnLength);
			if (!NT_SUCCESS(MyNtQueryObject(
				NtQueryObject,
				dupHandle,
				ObjectNameInformation,
				objectNameInfo,
				returnLength,
				NULL
			)))
			{
				/* We have the type name, so just display that. */
				printf(
					"[%#x] %.*S: (could not get name)\n",
					handle.Handle,
					objectTypeInfo->Name.Length / 2,
					objectTypeInfo->Name.Buffer
				);
				free(objectTypeInfo);
				free(objectNameInfo);
				CloseHandle(dupHandle);
				continue;
			}
		}

		/* Cast our buffer into an UNICODE_STRING. */
		objectName = *(PUNICODE_STRING)objectNameInfo;

		/* Print the information! */
		if (objectName.Length)
		{
			/* The object has a name. */
			printf(
				"[%#x] %.*S: %.*S\n",
				handle.Handle,
				objectTypeInfo->Name.Length / 2,
				objectTypeInfo->Name.Buffer,
				objectName.Length / 2,
				objectName.Buffer
			);

			if (wcscmp(objectName.Buffer, pDosDriveName) == 0)
			{
				printf("opend by process: %d", handle.ProcessId);
				break;
			}
		}
		else
		{
			/* Print something else. */
			printf(
				"[%#x] %.*S: (unnamed)\n",
				handle.Handle,
				objectTypeInfo->Name.Length / 2,
				objectTypeInfo->Name.Buffer
			);
		}

		free(objectTypeInfo);
		free(objectNameInfo);
		CloseHandle(dupHandle);
	}

	free(handleInfo);
	CloseHandle(processHandle);

	return 0;
}
Example #13
0
/*
 * File system functions
 */
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_PosixFileSystemFunctions_listFileSystems(JNIEnv *env, jclass target, jobject info, jobject result) {
    wchar_t* volumeName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1));

    jclass info_class = env->GetObjectClass(info);
    jmethodID method = env->GetMethodID(info_class, "add", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V");

    HANDLE handle = FindFirstVolumeW(volumeName, MAX_PATH+1);
    if (handle == INVALID_HANDLE_VALUE) {
        free(volumeName);
        mark_failed_with_errno(env, "could not find first volume", result);
        return;
    }

    wchar_t* deviceName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1));
    wchar_t* pathNames = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1));
    wchar_t* fsName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1));

    while(true) {
        // Chop off the trailing '\'
        size_t len = wcslen(volumeName);
        if (len < 5) {
            mark_failed_with_message(env, "volume name is too short", result);
            break;
        }
        volumeName[len-1] = L'\0';

        if (QueryDosDeviceW(&volumeName[4], deviceName, MAX_PATH+1) == 0) {
            mark_failed_with_errno(env, "could not query dos device", result);
            break;
        }
        volumeName[len-1] = L'\\';

        DWORD used;
        if (GetVolumePathNamesForVolumeNameW(volumeName, pathNames, MAX_PATH+1, &used) == 0) {
            // TODO - try again if the buffer is too small
            mark_failed_with_errno(env, "could not query volume paths", result);
            break;
        }

        wchar_t* cur = pathNames;
        if (cur[0] != L'\0') {
            if(GetVolumeInformationW(cur, NULL, 0, NULL, NULL, NULL, fsName, MAX_PATH+1) == 0) {
                if (GetLastError() != ERROR_NOT_READY) {
                    mark_failed_with_errno(env, "could not query volume information", result);
                    break;
                }
                wcscpy(fsName, L"unknown");
            }
            for (;cur[0] != L'\0'; cur += wcslen(cur) + 1) {
                env->CallVoidMethod(info, method, env->NewString((jchar*)deviceName, wcslen(deviceName)),
                                    env->NewString((jchar*)fsName, wcslen(fsName)), env->NewString((jchar*)cur, wcslen(cur)), JNI_FALSE);
            }
        }

        if (FindNextVolumeW(handle, volumeName, MAX_PATH) == 0) {
            if (GetLastError() != ERROR_NO_MORE_FILES) {
                mark_failed_with_errno(env, "could find next volume", result);
            }
            break;
        }
    }
    free(volumeName);
    free(deviceName);
    free(pathNames);
    free(fsName);
    FindVolumeClose(handle);
}
Example #14
0
HRESULT 
Virt::AddApplToSB(
	__in PWCHAR dosAppPath,
	__in PWCHAR dosSBPath
)
{
	HRESULT hResult = E_FAIL;
	PWCHAR	nativeAppPath = NULL, nativeSBPath = NULL;
	WCHAR	nativeSBVolName [256];

	WCHAR	dosSBVolName []=L"C:";
	ULONG nativeAppPathSize = 0, nativeSBPathSize = 0, nativeSBVolNameLen = 0;

	__try
	{
		nativeAppPathSize = (ULONG)(wcslen(dosAppPath) + 256)*sizeof(WCHAR);
		nativeAppPath = (PWCHAR) new char [nativeAppPathSize];
		if (!nativeAppPath)
		{
			 hResult = E_FAIL;
			 __leave;
		}
		
		memset( nativeAppPath, 0,  nativeAppPathSize);

		nativeSBPathSize = (ULONG)(wcslen(dosSBPath) + 256)*sizeof(WCHAR);
		nativeSBPath = (PWCHAR) new char [nativeSBPathSize];
		if (!nativeSBPath)
		{
			hResult = E_FAIL;
			__leave;
		}
		
		memset( nativeAppPath, 0,  nativeAppPathSize );
		
		hResult = MKL_QueryFileNativePath( pClientContext, dosAppPath, nativeAppPath, &nativeAppPathSize );
		if ( !SUCCEEDED(hResult) )
		{
			__leave;
		}
		else
			memset((char*)nativeAppPath+nativeAppPathSize,0,  2);

		hResult = MKL_QueryFileNativePath( pClientContext, dosSBPath, nativeSBPath, &nativeSBPathSize );
		if ( !SUCCEEDED(hResult) )
		{
			__leave;
		}
		else
			memset( (char*)nativeSBPath+nativeSBPathSize,0,  2 );

		memset(dosSBVolName, 0, 3*sizeof(WCHAR));
		memset(nativeSBVolName, 0, 256*sizeof(WCHAR));
		memcpy(dosSBVolName, dosSBPath, 2*sizeof(WCHAR) );
		
		nativeSBVolNameLen = QueryDosDeviceW( dosSBVolName, nativeSBVolName, 255 );
				if (!nativeSBVolNameLen)
		{
			DWORD err = GetLastError();
			hResult = E_FAIL;
			__leave;
		}
		memset( nativeSBVolName+nativeSBVolNameLen, 0, 2 );

		if ( pClientContext )
			hResult = MKL_Virt_AddApplToSB (
			pClientContext,
			nativeAppPath,
			nativeSBPath,
			nativeSBVolName
			);

	}
	__finally
	{
		
		if ( nativeAppPath )
			delete [] nativeAppPath;

		if ( nativeSBPath )
			delete [] nativeSBPath;
		
	}
 	
	return hResult;
}
Example #15
0
void WmdmLister::GuessDriveLetter(DeviceInfo* info) {
  qLog(Debug) << "Guessing drive letter for" << info->name_;

  // Windows XP puts the drive letter in brackets at the end of the name
  QRegExp drive_letter("\\(([A-Z]:)\\)$");
  if (drive_letter.indexIn(info->name_) != -1) {
    qLog(Debug) << "Looks like an XP drive" << drive_letter.cap(1);
    CheckDriveLetter(info, drive_letter.cap(1));
    return;
  }

  // Windows 7 sometimes has the drive letter as the whole name
  drive_letter = QRegExp("^([A-Z]:)\\\\$");
  if (drive_letter.indexIn(info->name_) != -1) {
    qLog(Debug) << "Looks like a win7 drive" << drive_letter.cap(1);
    CheckDriveLetter(info, drive_letter.cap(1));
    return;
  }

  // Otherwise Windows 7 uses the drive's DOS label as its whole name.
  // Let's enumerate all the volumes on the system and find one with that
  // label, then get its drive letter.  Yay!
  wchar_t volume_name[MAX_PATH + 1];
  HANDLE handle = FindFirstVolumeW(volume_name, MAX_PATH);

  forever {
    // QueryDosDeviceW doesn't allow a trailing backslash, so remove it.
    int length = wcslen(volume_name);
    volume_name[length - 1] = L'\0';

    wchar_t device_name[MAX_PATH + 1];
    QueryDosDeviceW(&volume_name[4], device_name, MAX_PATH);

    volume_name[length - 1] = L'\\';

    // Don't do cd-roms or floppies
    if (QString::fromWCharArray(device_name).contains("HarddiskVolume")) {
      wchar_t volume_path[MAX_PATH + 1];
      DWORD volume_path_length = MAX_PATH;
      GetVolumePathNamesForVolumeNameW(
          volume_name, volume_path, volume_path_length, &volume_path_length);

      if (wcslen(volume_path) == 3) {
        ScopedWCharArray name(QString(MAX_PATH + 1, '\0'));
        ScopedWCharArray type(QString(MAX_PATH + 1, '\0'));
        DWORD serial = 0;

        if (!GetVolumeInformationW(volume_path, name, MAX_PATH,
            &serial, NULL, NULL, type, MAX_PATH)) {
          qLog(Warning) << "Error getting volume information for" <<
              QString::fromWCharArray(volume_path);
        } else {
          if (name.ToString() == info->name_ && name.characters() != 0) {
            // We found it!
            qLog(Debug) << "Looks like a win7 drive name" << QString::fromWCharArray(volume_path);
            if (CheckDriveLetter(info, QString::fromWCharArray(volume_path))) {
              info->device_name_ = QString::fromWCharArray(device_name);
              info->volume_name_ = QString::fromWCharArray(volume_name);
            }
            break;
          }
        }
      }
    }

    if (!FindNextVolumeW(handle, volume_name, MAX_PATH))
      break;
  }
  FindVolumeClose(handle);
}
Example #16
0
/*
 * @implemented
 */
DWORD
WINAPI
QueryDosDeviceA(
    LPCSTR lpDeviceName,
    LPSTR lpTargetPath,
    DWORD ucchMax
    )
{
  UNICODE_STRING DeviceNameU;
  UNICODE_STRING TargetPathU;
  ANSI_STRING TargetPathA;
  DWORD Length;
  DWORD CurrentLength;
  PWCHAR Buffer;

  if (lpDeviceName)
  {
    if (!RtlCreateUnicodeStringFromAsciiz (&DeviceNameU,
					   (LPSTR)lpDeviceName))
    {
      SetLastError (ERROR_NOT_ENOUGH_MEMORY);
      return 0;
    }
  }
  Buffer = RtlAllocateHeap (RtlGetProcessHeap (),
			    0,
			    ucchMax * sizeof(WCHAR));
  if (Buffer == NULL)
  {
    if (lpDeviceName)
    {
      RtlFreeHeap (RtlGetProcessHeap (),
	           0,
	           DeviceNameU.Buffer);
    }
    SetLastError (ERROR_NOT_ENOUGH_MEMORY);
    return 0;
  }

  Length = QueryDosDeviceW (lpDeviceName ? DeviceNameU.Buffer : NULL,
			    Buffer,
			    ucchMax);
  if (Length != 0)
  {
    TargetPathA.Buffer = lpTargetPath;
    TargetPathU.Buffer = Buffer;
    ucchMax = Length;

    while (ucchMax)
    {
      CurrentLength = min (ucchMax, MAXUSHORT / 2);
      TargetPathU.MaximumLength = TargetPathU.Length = (USHORT)CurrentLength * sizeof(WCHAR);
     
      TargetPathA.Length = 0;
      TargetPathA.MaximumLength = (USHORT)CurrentLength;

      RtlUnicodeStringToAnsiString (&TargetPathA,
				    &TargetPathU,
				    FALSE);
      ucchMax -= CurrentLength;
      TargetPathA.Buffer += TargetPathA.Length;
      TargetPathU.Buffer += TargetPathU.Length / sizeof(WCHAR);
    }
  }

  RtlFreeHeap (RtlGetProcessHeap (),
	       0,
	       Buffer);
  if (lpDeviceName)
  {
    RtlFreeHeap (RtlGetProcessHeap (),
	         0,
	         DeviceNameU.Buffer);
  }
  return Length;
}
Example #17
0
BOOL GetIMAPIBurningDevices(CArray<LPWSTR>& aDevicePaths)
{
	CRegKey RegKey;
	if (RegKey.OpenKey(HKCR,"IMAPI.MSDiscMasterObj\\CLSID",CRegKey::defRead)!=ERROR_SUCCESS)
		return FALSE;

	WCHAR szCLSID[50];
	if (RegKey.QueryValue(L"",szCLSID,50)==0)
		return FALSE;

	CLSID clsid;
	if (CLSIDFromString(szCLSID,&clsid)!=NO_ERROR)
		return FALSE;

	
	HRESULT hRes;
	IDiscMaster* pdm;
	hRes=CoCreateInstance(clsid,NULL,CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER,IID_IDiscMaster,(void**)&pdm);
	if (FAILED(hRes))
		return FALSE;

	hRes=pdm->Open();
	if (FAILED(hRes))
	{
		pdm->Release();
		return FALSE;
	}

	IEnumDiscRecorders* pedr;
	hRes=pdm->EnumDiscRecorders(&pedr);
	if (SUCCEEDED(hRes))
	{
		IDiscRecorder* pdr;
		DWORD dwReturned;
		while ((hRes=pedr->Next(1,&pdr,&dwReturned))==S_OK)
		{
			BSTR bPath;
			hRes=pdr->GetPath(&bPath);
			if (SUCCEEDED(bPath))
			{
				if (bPath[0]=='\\')
				{
					WCHAR szName[MAX_PATH];
					WCHAR szTemp[MAX_PATH]=L"";
					WCHAR drive[]=L" :";
					GetLogicalDriveStringsW(MAX_PATH,szTemp);

					LPWSTR pPtr=szTemp;
					while (*pPtr!='\0')
					{
						*drive=*pPtr;
						if (QueryDosDeviceW(drive, szName,MAX_PATH))
						{
							if (wcscmp(szName,bPath)==0)
								aDevicePaths.Add(alloccopy(pPtr));
						}

						pPtr+=istrlenw(pPtr)+1;
					}
				}
				else
					aDevicePaths.Add(alloccopy(bPath));
			}

			pdr->Release();
		}
		pedr->Release();
	}


	pdm->Close();
	pdm->Release();

	return TRUE;
}
Example #18
0
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_PosixFileSystemFunctions_listFileSystems(JNIEnv *env, jclass target, jobject info, jobject result) {
    jclass info_class = env->GetObjectClass(info);
    jmethodID method = env->GetMethodID(info_class, "add", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZZ)V");

    DWORD required = GetLogicalDriveStringsW(0, NULL);
    if (required == 0) {
        mark_failed_with_errno(env, "could not determine logical drive buffer size", result);
        return;
    }

    wchar_t* buffer = (wchar_t*)malloc(sizeof(wchar_t) * (required + 1));
    wchar_t* deviceName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH + 1));
    wchar_t* fileSystemName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH + 1));

    if (GetLogicalDriveStringsW(required, buffer) == 0) {
        mark_failed_with_errno(env, "could not determine logical drives", result);
    } else {
        wchar_t* cur = buffer;
        for (;cur[0] != L'\0'; cur += wcslen(cur) + 1) {
            DWORD type = GetDriveTypeW(cur);
            jboolean remote = type == DRIVE_REMOTE;

            // chop off trailing '\'
            size_t len = wcslen(cur);
            cur[len-1] = L'\0';

            // create device name \\.\C:
            wchar_t devPath[7];
            swprintf(devPath, 7, L"\\\\.\\%s", cur);

            if (QueryDosDeviceW(cur, deviceName, MAX_PATH+1) == 0) {
                mark_failed_with_errno(env, "could not map device for logical drive", result);
                break;
            }
            cur[len-1] = L'\\';

            DWORD available = 1;
            if (!remote) {
                HANDLE hDevice = CreateFileW(devPath,         // like "\\.\E:"
                                             FILE_READ_ATTRIBUTES, // read access to the attributes
                                             FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, // share mode
                                             NULL, OPEN_EXISTING, 0, NULL);
                if (hDevice != INVALID_HANDLE_VALUE) {
                    DWORD cbBytesReturned;
                    DWORD bSuccess = DeviceIoControl (hDevice,                     // device to be queried
                                                IOCTL_STORAGE_CHECK_VERIFY2,
                                                NULL, 0,                     // no input buffer
                                                NULL, 0,                     // no output buffer
                                                &cbBytesReturned,            // # bytes returned
                                                (LPOVERLAPPED) NULL);        // synchronous I/O
                    if (!bSuccess) {
                        available = 0;
                    }
                    CloseHandle(hDevice);
                }
            }

            jboolean casePreserving = JNI_TRUE;
            if (available) {
                DWORD flags;
                if (GetVolumeInformationW(cur, NULL, 0, NULL, NULL, &flags, fileSystemName, MAX_PATH+1) == 0) {
                    mark_failed_with_errno(env, "could not get volume information", result);
                    break;
                }
                casePreserving = (flags & FILE_CASE_PRESERVED_NAMES) != 0;
            } else {
                if (type == DRIVE_CDROM) {
                    swprintf(fileSystemName, MAX_PATH+1, L"cdrom");
                } else {
                    swprintf(fileSystemName, MAX_PATH+1, L"unknown");
                }
            }

            env->CallVoidMethod(info, method,
                                wchar_to_java(env, cur, wcslen(cur), result),
                                wchar_to_java(env, fileSystemName, wcslen(fileSystemName), result),
                                wchar_to_java(env, deviceName, wcslen(deviceName), result),
                                remote, JNI_FALSE, casePreserving);
        }
    }

    free(buffer);
    free(deviceName);
    free(fileSystemName);
}