How to call function CreateProcess in Delphi Prism?

为君一笑 提交于 2019-12-12 01:29:53

问题


I wrote

function CreateProcess(
            lpApplicationName:String;
            lpCommandLine:String;
            lpProcessAttributes:IntPtr;
            lpThreadAttributes:IntPtr;
            bInheritHandles:Boolean;
            dwCreationFlags:Int32;
            lpEnvironment:IntPtr;
            lpCurrentDirectory:IntPtr;
            lpStartupInfo:STARTUPINFO;
            lpProcessInformation:ProcessInfo):Boolean;
         external 'kernel32.dll';

but VStudio said "Semicolon" expected - after external and " "end" expected" after 'kernel32.dll'; Can you help me to load and call a function please?


回答1:


Why don't you use the .NET Process Class .. it does not make a lot of sense to use interop in this case since you are already using Delphi Prism..

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx




回答2:


@Ilya, you are using a wrong syntax for call an external function. You need to use the DllImport keyword to get Windows interop working.

you must rewrite your function to

[DllImport("kernel32.dll")]
class function CreateProcess(
            lpApplicationName:String;
            lpCommandLine:String;
            lpProcessAttributes:IntPtr;
            lpThreadAttributes:IntPtr;
            bInheritHandles:Boolean;
            dwCreationFlags:Int32;
            lpEnvironment:IntPtr;
            lpCurrentDirectory:IntPtr;
            lpStartupInfo:STARTUPINFO;
            lpProcessInformation:ProcessInfo):Boolean; external;

check this working sample

namespace ConsoleApplication20;

interface
uses
    System.Diagnostics,
    System.Runtime.InteropServices;


type
PROCESS_INFORMATION =record
    hProcess    : IntPtr;
    hThread     : IntPtr;
    dwProcessId : UInt32;
    dwThreadId  : UInt32;
end;



STARTUPINFO =record
     cb       : UInt32;
    lpReserved: String;
    lpDesktop : String;
    lpTitle   : String;
    dwX       : UInt32;
    dwY       : UInt32;
    dwXSize   : UInt32;
    dYSize    : UInt32;
    dwXCountChars   : UInt32;
    dwYCountChars   : UInt32;
    dwFillAttribute : UInt32;
    dwFlags         : UInt32;
    wShowWindow : ShortInt;
    cbReserved2 : ShortInt;
    lpReserved2 : IntPtr;
    hStdInput   : IntPtr;
    hStdOutput  : IntPtr;
    hStdError   : IntPtr;
end;

  ConsoleApp = class
  private
    [DllImport("kernel32.dll")]
    class method CreateProcess( lpApplicationName: string;  lpCommandLine:string;  lpProcessAttributes:IntPtr; lpThreadAttributes:IntPtr;
                        bInheritHandles:Boolean;dwCreationFlags: UInt32;  lpEnvironment:IntPtr;
                        lpCurrentDirectory:string;var lpStartupInfo:STARTUPINFO;out lpProcessInformation:PROCESS_INFORMATION) : boolean; external;
  public
    class method Main;
  end;

implementation

class method ConsoleApp.Main;
var
lpStartupInfo        : STARTUPINFO;
lpProcessInformation : PROCESS_INFORMATION;
begin
        lpStartupInfo := new STARTUPINFO();
        lpProcessInformation := new PROCESS_INFORMATION();
        Console.WriteLine('Creating Process');
        CreateProcess('C:\WINDOWS\SYSTEM32\notepad.exe', nil, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, nil, var lpStartupInfo, out lpProcessInformation);
        Console.ReadLine();
end;

end.

Check theses link for mmore info

  • Platform Invoke Tutorial
  • A Closer Look at Platform Invoke


来源:https://stackoverflow.com/questions/2708520/how-to-call-function-createprocess-in-delphi-prism

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!