当前位置 博文首页 > KOOKNUT的博客:Dos路径转换为文件路径--TitanEngine

    KOOKNUT的博客:Dos路径转换为文件路径--TitanEngine

    作者:[db:作者] 时间:2021-07-02 21:24

    //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;
    }
    
    cs