独家开卷

Exclusive volume opening

我可以通过将 dwShareMode 设置为 0 使用 CreateFile 打开卷 "exclusively":

#include <windows.h>
int main() {
  HANDLE ki = CreateFile("\\.\F:", GENERIC_READ | GENERIC_WRITE, 0,
    NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
}

我可以用 fopen 在 "shared mode" 中打开一个卷:

#include <stdio.h>
int main() {
  FILE* ki = fopen("\\.\F:", "r+b");
}

我可以打开文件 "exclusively" 打开:

#include <stdio.h>
#include <fcntl.h>
int main() {
  int ju = open("lima.txt", O_RDWR | O_EXCL);
  FILE* ki = fdopen(ju, "r+b");
}

但是,如果我尝试使用 open 打开卷,它将失败:

#include <stdio.h>
#include <fcntl.h>
int main() {
  int ju = open("\\.\F:", O_RDWR | O_EXCL);
  FILE* ki = fdopen(ju, "r+b");
}

经过测试,无论是否使用 O_EXCL 标志,都会发生这种情况。是独家卷 打开只能用 CreateFile 完成的东西,或者我错过了 什么东西?

根据the standard

result is undefined if O_RDWR is applied to a FIFO

在这种情况下,卷似乎被识别为 FIFO。修复:

open("\\.\F:", O_RDONLY);

或:

open("\\.\F:", O_WRONLY);

或:

open("\\.\F:", O_RDONLY | O_WRONLY);