HANDLE m_hIDComDev = CreateFile( _T("COM3:"),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL 
);

解决方案 »

  1.   

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/createfile.asp
    自己看去吧
      

  2.   

    The CreateFile function creates or opens the following objects and returns a handle that can be used to access the object: Consoles 
    Communications resources 
    Directories (open only) 
    Disk devices (Windows NT/2000 only) 
    Files 
    Mailslots 
    Pipes 
    HANDLE CreateFile(
      LPCTSTR lpFileName,                         // file name
      DWORD dwDesiredAccess,                      // access mode
      DWORD dwShareMode,                          // share mode
      LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
      DWORD dwCreationDisposition,                // how to create
      DWORD dwFlagsAndAttributes,                 // file attributes
      HANDLE hTemplateFile                        // handle to template file
    );
    Parameters
    lpFileName 
    [in] Pointer to a null-terminated string that specifies the name of the object to create or open. 
    Windows NT/2000: In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to nearly 32,000 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see File Name Conventions. Windows 95/98: This string must not exceed MAX_PATH characters. dwDesiredAccess 
    [in] Specifies the type of access to the object. An application can obtain read access, write access, read/write access, or device query access. This parameter can be any combination of the following values. 
    dwShareMode 
    [in] Specifies how the object can be shared. If dwShareMode is 0, the object cannot be shared. Subsequent open operations on the object will fail, until the handle is closed. 
    To share the object, use a combination of one or more of the following values. 
    lpSecurityAttributes 
    [in] Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpSecurityAttributes is NULL, the handle cannot be inherited. 
    Windows NT/2000: The lpSecurityDescriptor member of the structure specifies a security descriptor for the object. If lpSecurityAttributes is NULL, the object gets a default security descriptor. The target file system must support security on files and directories for this parameter to have an effect on files. dwCreationDisposition 
    [in] Specifies which action to take on files that exist, and which action to take when files do not exist. For more information about this parameter, see the Res section. This parameter must be one of the following values. dwFlagsAndAttributes 
    [in] Specifies the file attributes and flags for the file. 
    Any combination of the following attributes is acceptable for the dwFlagsAndAttributes parameter, except all other file attributes override FILE_ATTRIBUTE_NORMAL.
    hTemplateFile 
    [in] Specifies a handle with GENERIC_READ access to a template file. The template file supplies file attributes and extended attributes for the file being created. 
    Windows 95: The hTemplateFile parameter must be NULL. If you supply a handle, the call fails and GetLastError returns ERROR_NOT_SUPPORTED. Return Values
    If the function succeeds, the return value is an open handle to the specified file. If the specified file exists before the function call and dwCreationDisposition is CREATE_ALWAYS or OPEN_ALWAYS, a call to GetLastError returns ERROR_ALREADY_EXISTS (even though the function has succeeded). If the file does not exist before the call, GetLastError returns zero. 
      

  3.   


    If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError. Res
    Use the CloseHandle function to close an object handle returned by CreateFile. As noted above, specifying zero for dwDesiredAccess allows an application to query device attributes without actually accessing the device. This type of querying is useful, for example, if an application wants to determine the size of a floppy disk drive and the formats it supports without having a floppy in the drive. MAPI: For more information, see Syntax and Limitations for Win32 Functions Useful in MAPI Development.Files
    If you are attempting to create a file on a floppy drive that does not have a floppy disk or a CD-ROM drive that does not have a CD, the system displays a message box asking the user to insert a disk or a CD, respectively. To prevent the system from displaying this message box, call the SetErrorMode function with SEM_FAILCRITICALERRORS.When creating a new file, the CreateFile function performs the following actions: Combines the file attributes and flags specified by dwFlagsAndAttributes with FILE_ATTRIBUTE_ARCHIVE. 
    Sets the file length to zero. 
    Copies the extended attributes supplied by the template file to the new file if the hTemplateFile parameter is specified. 
    When opening an existing file, CreateFile performs the following actions: Combines the file flags specified by dwFlagsAndAttributes with existing file attributes. CreateFile ignores the file attributes specified by dwFlagsAndAttributes. 
    Sets the file length according to the value of dwCreationDisposition. 
    Ignores the hTemplateFile parameter. 
    Ignores the lpSecurityDescriptor member of the SECURITY_ATTRIBUTES structure if the lpSecurityAttributes parameter is not NULL. The other structure members are used. The bInheritHandle member is the only way to indicate whether the file handle can be inherited. 
    Windows NT/2000: If you rename or delete a file, then restore it shortly thereafter, Windows NT searches the cache for file information to restore. Cached information includes its short/long name pair and creation time. Windows NT/2000: Some file systems, such as NTFS, support compression or encryption for individual files and directories. On volumes formatted for such a file system, a new file inherits the compression and encryption attributes of its directory.You cannot use the CreateFile function to set a file's compression state. Use the DeviceIoControl function to set a file's compression state. Pipes
    If CreateFile opens the client end of a named pipe, the function uses any instance of the named pipe that is in the listening state. The opening process can duplicate the handle as many times as required but, once opened, the named pipe instance cannot be opened by another client. The access specified when a pipe is opened must be compatible with the access specified in the dwOpenMode parameter of the CreateNamedPipe function. For more information about pipes, see Pipes. Mailslots
    If CreateFile opens the client end of a mailslot, the function returns INVALID_HANDLE_VALUE if the mailslot client attempts to open a local mailslot before the mailslot server has created it with the CreateMailSlot function. For more information about mailslots, see Mailslots. Communications Resources
    The CreateFile function can create a handle to a communications resource, such as the serial port COM1. For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING, and the hTemplate parameter must be NULL. Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O. For more information about communications, see Communications. Disk Devices
    Volume handles may be opened as noncached at the discretion of the file system, even when the noncached option is not specified with CreateFile. You should assume that all Microsoft file systems open volume handles as noncached. The restrictions on noncached I/O for files apply to volumes as well.A file system may or may not require buffer alignment even though the data is noncached. However, if the noncached option is specified when opening a volume, buffer alignment is enforced regardless of the file system on the volume. It is recommended on all file systems that you open volume handles as noncached and follow the noncached I/O restrictions.Windows NT/2000: You can use the CreateFile function to open a disk drive or a partition on a disk drive. The function returns a handle to the disk device; that handle can be used with the DeviceIOControl function. The following requirements must be met in order for such a call to succeed: The caller must have administrative privileges for the operation to succeed on a hard disk drive. 
      

  4.   

    HANDLE m_hIDComDev = CreateFile( _T("COM3:"),//打开COM3口
    GENERIC_READ | GENERIC_WRITE,//可读可写
    0,// 不共享
    NULL, //安全设置没有
    OPEN_EXISTING, //打开已经存在的,如果不存在返回错误
    0, // 文件的属性,0是默认的
    NULL // 不提供模板文件属性 );如果要详细的参考,那么可以看看:
    你的MSDN或者http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/createfile.asp