文号
t79978
阅读
2644
回复
0
收藏
1
在Windows中为文件夹分配盘符(模拟subst)
2016/05/19
IP:四川
subst命令在Windows中可以动态分配盘符。它内部实现为DefineDosDevice。但这个函数有个问题,如果以前分配过的话,不会失败,而会把原来的定义覆盖掉并保存起来。因此在调用DefineDosDevice之前,要调用QueryDosDevice以查询是否已经存在盘符的定义。

注意:NT路径所需缓冲区是32768,而不是Win32路径的260。QueryDosDevice可能会返回两个NT路径,因此所需缓冲区大小是65536。
#include <stdio.h>
#include <string.h>
#include <windows.h>
     
int wmain(int argc, wchar_t *argv[])
{
    wchar_t drive[] = L"Z:";
    wchar_t srcpath[65536] = L""; // 两个32768
    if (argc < 2) return 0;
    for (drive[0] = L'Z'; drive[0] >= L'A'; drive[0]--)
    {
        memset(srcpath, 0, 65536 * sizeof (wchar_t));
        QueryDosDeviceW(drive, srcpath, 65536);
     
        if (lstrcmpW(argv[1], L"clean") == 0)
        {
            DefineDosDeviceW(DDD_REMOVE_DEFINITION, drive, NULL);
        }
        else
        {
            if (lstrlenW(srcpath) == 0)
            {
                DefineDosDeviceW(0, drive, argv[1]);
                break;
            }
        }
    }
}</windows.h></string.h></stdio.h>
[ 修改于10年7个月前 - 2016/05/19 23:40:12 ]
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的