起步软件技术论坛-X3

 找回密码
 立即注册
搜索
查看: 342|回复: 7

【结帖】不支持指针,ShellExecuteEx函数不能调用怎么办**

[复制链接]
发表于 2008-1-11 23:19:27 | 显示全部楼层 |阅读模式
我们需要写一个启动外部程序并等待它结束的函数,查了一下发现,有两个Win32 API函数可以利用:ShellExecuteEx 或者CreateProcess。
function ExecAppWait(AppName, Params: string): Boolean;
var
  ShellExInfo: TShellExecuteInfo;
begin
  //FillChar(ShellExInfo, SizeOf(ShellExInfo), 0);
  with ShellExInfo do begin
    cbSize := SizeOf(ShellExInfo);
    fMask := see_Mask_NoCloseProcess;
    Wnd := Forms.Application.Handle;
    lpFile := PChar(AppName);
    lpParameters := PChar(Params);
    nShow := sw_ShowNormal;
  end;
  Result := ShellExecuteEx(@ShellExInfo);
  if Result then
    WaitForSingleObject(ShellExInfo.HProcess, INFINITE);
end;

Function WinExecEx(cmd:string)word;
var
  StartupInfo:Borland.Delphi.Windows.TStartupInfo;
  ProcessInfo:Borland.Delphi.Windows.TProcessInformation;
  i:integer;
begin
  //FillChar(StartupInfo, SizeOf(StartupInfo), 0);
  StartupInfo.cb:=SizeOf(StartupInfo);
  StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow:=SW_SHOWNORMAL;

  if not Borland.Delphi.Windows.CreateProcess('',cmd,nil,nil,False,Create_new_console
     or Normal_priority_class,nil,'', StartupInfo, ProcessInfo) then
  begin
    Result:=0;
    exit;
  end;
  WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
  Result:=ProcessInfo.dwProcessId;
  CloseHandle(ProcessInfo.hProcess);
  CloseHandle(ProcessInfo.hThread);
end;


现在写了两个函数,第一个有指针参数不能编译,第二个可以编译但始终不能运行程序
请问还有没有其它方法可以实现这个函数
回复

使用道具 举报

发表于 2008-1-12 08:44:38 | 显示全部楼层
平台对指针不太支持
可以考虑做成dll然后当作第三方控件导入到平台
具体请以Import在论坛搜索(需要 delphi6+Sp2)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-1-12 14:31:07 | 显示全部楼层
library CommonLib;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes,
  Windows,
  ShellAPI;

{$R *.res}
function ExecAppWait(hwnd:HWND; AppName, Params: string): Boolean;stdcall;
var
  ShellExInfo: TShellExecuteInfo;
begin
  FillChar(ShellExInfo, SizeOf(ShellExInfo), 0);
  with ShellExInfo do begin
    cbSize := SizeOf(ShellExInfo);
    fMask := see_Mask_NoCloseProcess;
    Wnd := hwnd;
    lpFile := PChar(AppName);
    lpParameters := PChar(Params);
    nShow := sw_ShowNormal;
  end;
  Result := ShellExecuteEx(@ShellExInfo);
  if Result then
    WaitForSingleObject(ShellExInfo.HProcess, INFINITE);
end;

exports
  ExecAppWait;

begin
end.


我用delphi 写了一个编译成DLL ,要怎么导入?看了一下手册

对于
没有对象信息的动态库,Delphi不能够自动生成类型声明信息,需要按照
Delphi的语法,自己手工的把输出的过程的声明写成.pas文件(一定要保
证文件在 Delphi中编译通过),其他的步骤不变。

不知道要怎么声明.pas文件
回复 支持 反对

使用道具 举报

发表于 2008-1-12 15:08:38 | 显示全部楼层
参考代码
const
  dllName = 'DLLName';

  function ExecAppWait(hwnd:HWND; AppName, Params: string): Boolean;stdcall;
implementation

  function ExecAppWait(hwnd:HWND; AppName, Params: string): Boolean;stdcall;export;external dllName;
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-1-12 16:19:50 | 显示全部楼层
我已经导入成功, 但在平台中调用出错,调用代码:
CommonLib.CommUnit.ExecAppWait(0,'calc.exe','');
运行时出错,现把资源文件和出错界面上传

dllforx3.zip

89.57 KB, 下载次数: 70

回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-1-14 16:22:42 | 显示全部楼层
版本2435  请给个例子
回复 支持 反对

使用道具 举报

发表于 2008-1-22 11:00:34 | 显示全部楼层
楼主,这是我做的,测试通过
ShellExecEX.ShellExecuteExPas.ExecAppWait('C:\WINDOWS\system32\calc.exe','');
  jsDialogs.ShowMsg('End','ok');

最后生成.rar

65.13 KB, 下载次数: 92

回复 支持 反对

使用道具 举报

发表于 2008-2-13 14:46:44 | 显示全部楼层
楼主,还有问题吗?
没问题就结帖了!!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Justep Inc.

GMT+8, 2025-7-7 00:07 , Processed in 0.045058 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表