CreateFile

 

Description

This function creates or opens an object and returns a handle that can be used to access the object.

Syntax

HANDLE CreateFile( LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, SECURITY_ATTRIBUTES * lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile )

Parameters

lpFileName
[input] A fullpath file name
dwDesiredAccess
[input] Specifies the type of access to the object. This parameter can be 0, GENERIC_READ, GENERIC_WRITE
dwShareMode
[input] Specifies how the object can be shared. If dwShareMode is 0, and CreateFile succeeds, the object cannot be shared and cannot be opened again until the handle is closed.
lpSecurityAttributes
[input] Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. Use NULL if no need for the handle to be inherited.
dwCreationDisposition
[input] Specifies which action to take on files that exist, and which action to take when files do not exist. Can be CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING, OPEN_ALWAYS, TRUNCATE_EXISTING
dwFlagsAndAttributes
[input] Specifies the file attributes and flags for the file.
hTemplateFile
[input] 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.

Return

If the function succeeds, the return value is an open handle to the specified file.

If the function fails, the return value is INVALID_HANDLE_VALUE.

Examples

EX1

int CreateFile_ex1()
{
    string strNewFilePath = "c:\\temp.txt";
    ///Create new file or open an exist file.
    HANDLE hFile;
    hFile = CreateFile( strNewFilePath,
                        GENERIC_READ,
                        FILE_SHARE_READ,
                        NULL,
                        CREATE_NEW ,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);
    if (hFile == INVALID_HANDLE_VALUE) 
    { 
        out_str("Could not Create file.");   // process error
        return -1;
    } 

    // codes to read the file
    CloseHandle(hFile);// must close file after reading
    
     if(!test_read_file(strNewFilePath))
        return 0;
    return 1;
}
//opens an existing file for reading.
bool test_read_file(LPCSTR lpcszFilename)
{
    HANDLE hFile; 
    hFile = CreateFile(lpcszFilename, 
                    GENERIC_READ,              // open for reading 
                    FILE_SHARE_READ,           // share for reading 
                    NULL,                      // no security 
                    OPEN_EXISTING,             // existing file only 
                    FILE_ATTRIBUTE_NORMAL,     // normal file 
                    NULL);                     // no attr. template 
    if (hFile == INVALID_HANDLE_VALUE) 
    { 
        out_str("Could not open file.");   // process error
        return false;
    } 
    // codes to read the file
    CloseHandle(hFile);// must close file after reading
    return true;
}

Remark

See Also

CloseHandle

header to Include

origin.h

Reference