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

沒有留言:

張貼留言

Why I Choose Delphi! (CHT)

扣除使用 C++ Builder 的時間,我實際使用 Delphi 的時間點是 2012 年,它帶我看到了不一樣的世界,見識到世界各地的高手使用 Delphi 創造出他們引以為豪的產品,並且無私的分享他們的學習知識,使我的技術能力得以跟隨他們的能力提升。 最最讓我喜歡的...