Translate

2013/06/10

EAccessViolation in function CreateProcess in Delphi 2009

很有趣的,我也遇到了這件事情,明明 CreateProcess 這函式在Delphi 7時就正常,到了Delphi 2009就出怪手了。

原因就是出在Unicode……解法轉貼如下:

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), ...)


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), ...)

This is enough to make CmdLine be backed by writeable memory.

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.

參考來源:Access Violation in function CreateProcess in Delphi 2009

沒有留言:

張貼留言

DataSnap REST API Authorization with PHP

About Authentication with DataSet REST JavaScript client. In connection.js: function setCredentials(user, password) { connectionInf...