Dos路徑轉換爲文件路徑--TitanEngine

//Dos路徑轉換爲文件路徑
VOID*  TranslateNativeNameW(wchar_t* NativeName)
{
	//提交保留物理頁,外部會對它進行釋放
	void*   TranslatedName = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, PAGE_READWRITE); //pointer is returned
	wchar_t DeviceName[3] = L"A:";
	wchar_t DeviceCOMName[5] = L"COM0";
	int		CurrentDeviceLength;

	while (DeviceName[0] <= 0x5A)//小於等於字符Z
	{
		RtlZeroMemory(TranslatedName, 0x1000);
		//查詢字符對應的Dos路徑
		if (QueryDosDeviceW(DeviceName, (LPWSTR)TranslatedName, MAX_PATH * 2) > NULL)
		{
			/*
			0x00290000  5c 00 44 00 65 00 76 00 69 00 63 00 65  \.D.e.v.i.c.e
			0x0029000D  00 5c 00 48 00 61 00 72 00 64 00 64 00  .\.H.a.r.d.d.
			0x0029001A  69 00 73 00 6b 00 56 00 6f 00 6c 00 75  i.s.k.V.o.l.u
			0x00290027  00 6d 00 65 00 38 00 00 00 00 00 00 00  .m.e.8.
			*/
			CurrentDeviceLength = lstrlenW((LPWSTR)TranslatedName);
			//越過DosDevice名稱之後,加上完整路徑
			lstrcatW((LPWSTR)TranslatedName, (LPCWSTR)(NativeName + CurrentDeviceLength));
			if (lstrcmpiW((LPCWSTR)TranslatedName, NativeName) == NULL)
			{
				//如果字符串相等,清空地址
				RtlZeroMemory(TranslatedName, 0x1000);
				//設置文件路徑名
				lstrcatW((LPWSTR)TranslatedName, DeviceName);
				lstrcatW((LPWSTR)TranslatedName, (LPWSTR)(NativeName + CurrentDeviceLength));
				//返回轉換結果
				/*
				0x00290000  43 00 3a 00 5c 00 57 00 69 00 6e 00 64  C.:.\.W.i.n.d
				0x0029000D  00 6f 00 77 00 73 00 5c 00 53 00 79 00  .o.w.s.\.S.y.
				0x0029001A  73 00 74 00 65 00 6d 00 33 00 32 00 5c  s.t.e.m.3.2.\
				0x00290027  00 73 00 69 00 68 00 6f 00 73 00 74 00  .s.i.h.o.s.t.
				0x00290034  2e 00 65 00 78 00 65 00 00 00 00 00 00  ..e.x.e.
				*/
				return TranslatedName;
			}
		}
		DeviceName[0]++;
	}
	//沒有走到過這裏面
	while (DeviceCOMName[3] <= 0x39)//COM0到COM9
	{
		RtlZeroMemory(TranslatedName, 0x1000);
		if (QueryDosDeviceW(DeviceCOMName, (LPWSTR)TranslatedName, MAX_PATH * 2) > NULL)
		{
			CurrentDeviceLength = lstrlenW((LPWSTR)TranslatedName);
			lstrcatW((LPWSTR)TranslatedName, (LPCWSTR)(NativeName + CurrentDeviceLength));
			if (lstrcmpiW((LPCWSTR)TranslatedName, NativeName) == NULL)
			{
				RtlZeroMemory(TranslatedName, 0x1000);
				lstrcatW((LPWSTR)TranslatedName, DeviceCOMName);
				lstrcatW((LPWSTR)TranslatedName, (LPWSTR)(NativeName + CurrentDeviceLength));
				return TranslatedName;
			}
		}
		DeviceCOMName[3]++;
	}

	VirtualFree(TranslatedName, NULL, MEM_RELEASE);

	return NULL;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章