Chinaunix首页 | 论坛 | 博客
  • 博客访问: 60130
  • 博文数量: 14
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-23 10:08
个人简介

生活是一种态度!也是一种感悟!

文章分类

全部博文(14)

文章存档

2013年(14)

我的朋友

分类: C/C++

2013-04-11 11:38:03

CreateFile函数声明:
HANDLE WINAPI CreateFile(
  _In_      LPCTSTR lpFileName,
  _In_      DWORD dwDesiredAccess,
  _In_      DWORD dwShareMode,
  _In_opt_  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  _In_      DWORD dwCreationDisposition,
  _In_      DWORD dwFlagsAndAttributes,
  _In_opt_  HANDLE hTemplateFile
);
lpFileName 参数: 要打开的文件的名字
    使用 "COM1" ~ "COM9“,一切正常,然而使用 " COM12“ 即只要大于等于10,失败,GetLastError()返回值2(系统找不到指定的文件)。

使用 "" 字符串做为lpFileName, CreateFile() 打开成功,GetLastError()返回0值,
即,sprintf(NameBuf, "", PortNum);
“\\.\”前缀表示将访问Win32设备命名空间,而非Win32文件命名空间,这就是如何不经过文件系统,而能直接的访问物理磁盘和卷的。
COM1 ~ COM9 是 NT 命名空间保留使用的,因此可以打开成功。而COM12不是NT命名空间的名字,因此,必须使用“\\.\”前缀指定是设备的命名空间,同样的COM1 ~ COM9 也可以通过“\\.\”指定设备命名空间的方式使用CreateFile打开。

查看msdn :
lpFileName [in] 
   The name of the file or device to be created or opened. You may use either forward slashes (/) or backslashes (\) in this name.
   In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see .
     For information on special device names, see .
     To create a file stream, specify the name of the file, a colon, and then the name of the stream. For more information, see .

Note  File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\\?\" prefix as detailed in the following sections.

Win32 Device Namespaces

The "\\.\" prefix will access the Win32 device namespace instead of the Win32 file namespace. This is how access to physical disks and volumes is accomplished directly, without going through the file system, if the API supports this type of access. You can access many devices other than disks this way (using the and functions, for example).

For example, if you want to open the system's serial communications port 1, you can use "COM1" in the call to the function. This works because COM1–COM9 are part of the reserved names in the NT namespace, although using the "\\.\" prefix will also work with these device names. By comparison, if you have a 100 port serial expansion board installed and want to open COM56, you cannot open it using "COM56" because there is no predefined NT namespace for COM56. You will need to open it using "\\.\COM56" because "\\.\" goes directly to the device namespace without attempting to locate a predefined alias.

 

阅读(3478) | 评论(0) | 转发(0) |
0

上一篇:GCC编译器安装过程

下一篇:没有了

给主人留下些什么吧!~~