【问题标题】:How to call function CreateProcess in Delphi Prism?如何在 Delphi Prism 中调用函数 CreateProcess?
【发布时间】:2010-04-25 14:22:45
【问题描述】:

我写的

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' 之后; 你能帮我加载和调用一个函数吗?

【问题讨论】:

    标签: delphi dll createprocess delphi-prism oxygene


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

    @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.
    

    查看论文链接了解更多信息

    【讨论】:

      猜你喜欢
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多