Delphi 2009中函数CreateProcess中的访问冲突 [英] Access Violation in function CreateProcess in Delphi 2009

查看:294
本文介绍了Delphi 2009中函数CreateProcess中的访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我有以下代码:

In my program I've the following code:

//Code
 if not CreateProcess(nil, NonConstCmd, nil, nil, True, NORMAL_PRIORITY_CLASS or
    CREATE_NEW_PROCESS_GROUP, nil, PCh, SI, P) then
//Code

我不断获得访问冲突错误。
顺便说一下,在Delphi7中,相同的代码完美地工作。
我读过MSDN,发现Delphi中的CreateProcess函数可以修改第二个参数。
最初是const,这就是为什么我创建一个具有相同值的新变量。
但它不起作用。

And I keep getting Access violation error. By the way, in Delphi7 the same code works perfectly. I've read MSDN and found that CreateProcess function in Delphi can modify the second argument. Inititally It was const, that's why I create a new variable with the same value. But it takes no effect.

问题是:为什么这个代码不工作?

The question is: why doesn't this code work?

推荐答案

问题出在 lpCommandLine 参数中。我怀疑你在做这样的事情:

The problem is in the lpCommandLine parameter. I suspect you are doing something like this:

var
  CmdLine: string;
...
CmdLine := 'notepad.exe';
CreateProcess(nil, PChar(CmdLine), ...)

访问冲突,因为 CmdLine 不可写内存。该字符串是存储在只读存储器中的常量字符串。

This results in an access violation because CmdLine is not writeable memory. The string is a constant string stored in read-only memory.

相反,您可以执行以下操作:

Instead you can do this:

CmdLine := 'notepad.exe';
UniqueString(CmdLine);
CreateProcess(nil, PChar(CmdLine), ...)

使 CmdLine 由可写内存支持。

只要使变量保存字符串非 - const,你需要使记录字符串也可写回。当您将字符串字面值分配给字符串变量时,字符串变量指向只读存储器。

It is not enough just to make the variable holding the string non-const, you need to make the memory that backs the string writeable too. When you assign a string literal to a string variable, the string variable points at read-only memory.

这篇关于Delphi 2009中函数CreateProcess中的访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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