Access Violation in function CreateProcess in Delphi 2009

后端 未结 2 1028
温柔的废话
温柔的废话 2020-12-14 11:08

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,         


        
相关标签:
2条回答
  • 2020-12-14 11:50

    Here an explanation why Unicode Delphi requires a different way to call CreateProcess: http://edn.embarcadero.com/article/38693

    0 讨论(0)
  • 2020-12-14 11:59

    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.

    0 讨论(0)
提交回复
热议问题