- 1 year ago
CreateProcess vs. CreateFile EXE
Comment
1 / 1
CreateProcess
and CreateFile
are two different Windows API functions with distinct purposes:
CreateProcess
:- Purpose:
CreateProcess
is used to create a new process and start the execution of a specified executable file (e.g., an.exe
, a batch script, etc.). - Parameters: It takes several parameters, including the path to the executable, command-line arguments, process attributes, and more.
- Return Value: If successful, it returns a handle to the newly created process and its primary thread.
- Purpose:
CreateFile
:- Purpose:
CreateFile
is used to create or open a file or I/O device (e.g., a disk drive, serial port, etc.) and obtain a handle to it. It is not used to execute or run executable files. - Parameters: It takes several parameters, including the file or device name, access mode, file attributes, and more.
- Return Value: If successful, it returns a handle to the opened file or device.
- Purpose:
Example of using CreateProcess
:
c#include <windows.h>
int main() {
STARTUPINFO si = {sizeof(STARTUPINFO)};
PROCESS_INFORMATION pi;
LPWSTR commandLine = L"notepad.exe";
if (CreateProcess(NULL, commandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
// Process created successfully
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
} else {
// Failed to create process
DWORD error = GetLastError();
// Handle error
}
return 0;
}
Example of using CreateFile
:
c#include <windows.h>
int main() {
HANDLE hFile = CreateFile(L"C:\\example.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
// File opened successfully
CloseHandle(hFile);
} else {
// Failed to open file
DWORD error = GetLastError();
// Handle error
}
return 0;
}
In summary, use CreateProcess
to launch an executable process, and use CreateFile
to open or create a file or I/O device.