---
title: "Delphi CreateFile 用法"
author: "cfeers"
date: "2011-03-22T22:07:38Z"
tags: []
categories: ["编程语言"]
slug: "delphi-createfile"
draft: false
lastmod: "2011-03-22T22:07:55Z"
id: 971098
url: "https://cfeers.iteye.com/blog/971098"
---

HANDLE CreateFile(  
　　LPCTSTR lpFileName, // pointer to name of the file  
　　DWORD dwDesiredAccess, // access (read-write) mode  
　　DWORD dwShareMode, // share mode  
　　LPSECURITY\_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes  
　　DWORD dwCreationDistribution, // how to create  
　　DWORD dwFlagsAndAttributes, // file attributes  
　　HANDLE hTemplateFile // handle to file with attributes to copy  
　　);  
　　lpFileName: 指明串口制备，例：COM1，COM2  
　　dwDesiredAccess: 指明串口存取方式，例：GENERIC\_READ|GENERIC\_WRITE  
　　dwShareMode: 指明串口共享方式  
　　lpSecurityAttributes: 指明串口的安全属性结构,NULL为缺省安全属性  
　　dwCreateionDistribution: 必须为OPEN\_EXISTIN  
　　dwFlagAndAttributes: 对串口唯一有意义的是FILE\_FLAG\_OVERLAPPED  
　　hTemplateFile: 必须为NULL

例子：

m\_hComm := CreateFile(                    //创建文件（串口），取得操作句柄  
PChar(sCommPort),                        //文件名称  
//'\\\\.\\COM10',  
GENERIC\_READ or GENERIC\_WRITE,         //操作方式(读或写)  
dwShareMode,                           //共享方式  
nil,                                   //SD  
OPEN\_EXISTING,                         //文件创建方法  
FILE\_ATTRIBUTE\_NORMAL,                 //文件属性  
0);                                    //handle to template file

if( m\_hComm = INVALID\_HANDLE\_VALUE ) then     //串口打开失败  
begin  
;  
end;