简体   繁体   English

将Windows设备文件名转换为驱动器号

[英]Convert windows device filename to drive letter

I am trying to get the filename associated with a process handle in C, and since my code needs to run on Windows XP I'm using GetProcessImageFileName (rather than QueryFullProcessImageName ). 我试图获取与C中的进程句柄相关联的文件名,并且因为我的代码需要在Windows XP上运行,所以我使用的是GetProcessImageFileName (而不是QueryFullProcessImageName )。

However, GetProcessImageFileName returns the path in device form, eg \\device\\harddiskvolume0\\ - how can I convert this to a drive letter? 但是, GetProcessImageFileName以设备形式返回路径,例如\\ device \\ harddiskvolume0 \\ - 如何将其转换为驱动器号?

I was going to suggest GetModuleFileNameEx like Luke did in comments. 我打算在评论中建议像Luke那样的GetModuleFileNameEx

QueryDosDevice() on all drive letters (you can find all the drive letters with GetLogicalDrives() ) would be another bet, though it's theoretically possible that you could get a path without a drive letter, or symbolic links could screw up a straightforward string compare. 所有驱动器号上的QueryDosDevice() (你可以找到带有GetLogicalDrives()所有驱动器号)都是另一个赌注,虽然理论上你可以得到一个没有驱动器号的路径,或者符号链接可能会搞砸一个简单的字符串比较。

But.. How about this... You should be able to prefix the NT path with \\??\\GLOBALROOT (this is from memory, it might not be exactly that) and then use it in functions like CreateFileW() . 但是..这个怎么样......你应该能够在NT路径前加上\\??\\GLOBALROOT (这是来自内存,它可能不是那个),然后在CreateFileW()等函数中使用它。 (AFAIK it has to be the Unicode versions of the file APIs..) (AFAIK它必须是文件API的Unicode版本..)

You can try convert the drive letter form to device form instead, and here is what I did, hope it helps: 您可以尝试将驱动器盘符表单转换为设备表单,这是我所做的,希望它有所帮助:

    TCHAR szTemp[MAX_PATH] = {0};
    _tcsncpy(szTemp, lpszImageFile, 2);
    QueryDosDevice(szTemp, szImageFile, MAX_PATH);

    _tcsncat(szImageFile, lpszImageFile+2, _tcslen(lpszImageFile) - 2);

In this code, lpszImageFile is the full path name of the process, eg c:\\program files\\test.exe. 在此代码中,lpszImageFile是进程的完整路径名,例如c:\\ program files \\ test.exe。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM