I'm working on Windows, using Visual studio 2019 community. I'm writing an MFC application in c++ and I'm trying to pass the following command line txt2pdf -i\".\textfile.txt\" -o\".\pdfout.pdf\"
to the cmd.
For this pupose, I'm using CreateProcess()
function, the documentation is here and this is an example of usage. My code is given below:
void CMFCApplication4Dlg::OnBnClickedButton1()
{
PROCESS_INFORMATION ProcessInfo; //This is what we get as an [out] parameter
STARTUPINFO StartupInfo; //This is an [in] parameter
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo; //Only compulsory field
LPWSTR cmd_args = (LPWSTR)"txt2pdf.exe - i\".textfile.txt\" -o\".pdfout.pdf\"";
if (!CreateProcess(NULL, cmd_args, NULL, NULL, FALSE, 0, NULL,
NULL, &StartupInfo, &ProcessInfo))
{
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
MessageBox(L"Success");
}
else
{
MessageBox(L"The process could not be started...");
}
}
I've seen many solutions and examples on how to call CreateProcess()
function, I've listed them below, but for some reason, none of them works, I always get the same error message:
Exception thrown at 0x759FF4BB (KernelBase.dll) in MFCApplication4.exe: 0xC0000005: Access violation writing location 0x00257960.
and when I look it up online, I don't find anything that is related to my code.
Other solutions I've tried:
How can I fix my code?