如何在Delphi Prism中调用函数CreateProcess?

发布于 2024-08-30 01:12:08 字数 559 浏览 3 评论 0原文

我写了

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';

,但 VStudio 说“预期分号”- 在外部之后,在“kernel32.dll”之后预期“结束”; 你能帮我加载和调用一个函数吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我还不会笑 2024-09-06 01:12:08

为什么不使用 .NET Process Class .. 在这种情况下使用互操作没有多大意义,因为您已经在使用 Delphi Prism ..

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

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

天赋异禀 2024-09-06 01:12:08

@Ilya,您使用了错误的语法来调用外部函数。您需要使用 DllImport 关键字才能使 Windows 互操作正常工作。

您必须重写您的函数来

[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;

检查此工作示例

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.

检查这些链接以获取更多信息

@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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文