尝试使用CreateProcess();没有编译器错误,但主要保持崩溃 [英] Trying to use CreateProcess() ; no compiler errors but main keeps crashing

查看:108
本文介绍了尝试使用CreateProcess();没有编译器错误,但主要保持崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数,在firefox中打开一个独立于主进程的PDF。我相信我有createProcess的参数的麻烦...任何帮助是非常感谢

I'm trying to create a function which opens a PDF in firefox separate from the main process. I believe I am having trouble with the parameters for createProcess... any help is greatly appreciated

编辑:批处理文件正在创建,我已经测试了几次,并解释一下:
批处理文件是因为我真的不知道我在做什么,我是计算机科学的学生,这是一个侧面项目,以帮助我在我的工作。我在一家法律办公室工作,并在电子邮件中写入邮件。我想制作一个简单的程序,循环通过扫描目录,显示扫描并提示用户有关文档的信息。因此,我需要能够动态构建文件路径。最初我使用系统打开firefox并显示文档。经过一番尝试,我得到它使用一个批处理文件。然后我学会了系统是一个阻塞命令,我需要启动一个单独的线程。这是我碰到createprocess的地方。我只是继续使用我的旧系统想法的批处理文件...而我想到的更多,我不记得哪个教授建议批处理文件或为什么...

the batch file is being created, I have tested it several times, and to explain a bit: The batch file is because I really don't know what I am doing, I am a student in computer science and this is a side project to help me at my job. I work at a law office and file the mail electronically as it comes in. I wanted to make a simple program that would loop through the scans directory, display the scan and prompt the user for information about the document. Therefore I need to be able to build a file path dynamically. Originally I was using "system" to open firefox and display the document. After a bit of trying I got it to work with a batch file. I then learned that system is a blocking command and that I would need to start a separate thread. This is where I ran into createprocess. I simply continued to use the batch file from my old system idea... And the more I think about it I can't remember which professor suggested the batch file or why...

void openPDF(char scansQueue[][MAX_NAME], int index)
{
// build bat file
fstream outfile;
outfile.open("C:\\firefox.bat");    
if(outfile.good())cout<<"outfile good!!!!"<<endl;
outfile<<"\"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe\" \"C:\\Scans\\"   <<scansQueue[index]<<"\"";

STARTUPINFOW si; 
PROCESS_INFORMATION pi; 
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if(!CreateProcess(NULL, L"C:\\firefox", NULL, NULL, false, 0, NULL, NULL, &si, &pi))cout<<"PROCESS FAILED TO EXECUTE!!!";
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

}


推荐答案

这个代码的几个问题。一些已经在评论中指出(关闭失败的可能无效句柄,无法创建批处理文件的可能性,以及相当有问题的命令行)。这里有几个问题。

There are several problems with this code. A few has already been pointed out in the comments (closing potentially invalid handles on failure, possibility that the batch file can't be created, and the rather questionable command line). Here's a few more issues.

首先,这种方式不能运行批处理文件。

First, you can't run a batch file this way.

文档 for CreateProcess 清楚地说明:

The documentation for CreateProcess clearly states:


要运行批处理文件,必须启动命令解释器;将 lpApplicationName 设置为cmd.exe并将 lpCommandLine 设置为以下参数:/ c加上批处理文件的名称。

To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the following arguments: /c plus the name of the batch file.

其次,你为 lpCommandLine 传递一个字符串文字,这也是文档明确禁止的:

Second, you are passing a string literal for lpCommandLine, something that's also explicitly outlawed by the documentation:


lpCommandLine [in,out,可选]

lpCommandLine [in, out, optional]

...

此函数的Unicode版本 CreateProcessW 可以修改此字符串的内容。因此,此参数不能是指向只读存储器的指针(例如 const 变量或文字字符串)。如果这个参数是一个常量字符串,该函数可能会导致访问冲突。

The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.

最后,为什么要创建一个临时批处理文件运行单个命令?您可以轻松地编写 CreateProcess 调用直接启动Firefox。

Finally, why are you creating a temporary batch file to run a single command? You could easily have written the CreateProcess call to start Firefox directly.

这篇关于尝试使用CreateProcess();没有编译器错误,但主要保持崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆