1

我是 Delphi 的初学者,现在我想学习这门语言,但我遇到了错误,我不知道问题出在哪里以及如何解决它。这个例子我把它从书本带到了delphi。

错误

[Pascal 错误] Engine.pas(41): E2250 没有可以使用这些参数调用的“ShellExecute”的重载版本

所有代码:

unit Engine;
interface
uses Windows, Classes, SysUtils;
type
  TTemplate = array of String;
  TEngine = class
private
  FFileName : String;
  FFileLines : TStringList;
protected
  procedure Execute(Path : String); virtual;
public
  Pattern : TTemplate;
  Replace : TTemplate;
  procedure Parse;
  constructor Create(FileName : String);
  destructor Destroy; override;
end;
implementation
{ TEngine }
uses ShellAPI; // włączenie modułu ShellAPI
constructor TEngine.Create(FileName : String);
begin
  FFileName := FileName; // przypisanie wartości parametru do
  FFileLines := TStringList.Create; // utworzenie typu TStringList
  FFileLines.LoadFromFile(FileName); // załadowanie zawartości
  inherited Create;
end;
destructor TEngine.Destroy;
begin
  FFileLines.Free; // zwolnienie typu
  { zwolnienie tablic }
  Pattern := nil;
  Replace := nil;
  DeleteFile('temporary.html'); // wykasowanie pliku tymczasowego
  inherited; // wywołanie destruktora klasy bazowej
end;
procedure TEngine.Execute(Path: String);
begin
  // otwarcie pliku w przeglądarce Internetowej
  ShellExecute(0, 'open', PChar(Path), nil, nil, SW_SHOW);
end;
procedure TEngine.Parse;
var
  i : Integer;
begin
  for I := Low(Pattern) to High(Pattern) do
  { zastąpienie określonych wartości w FFileLines }
  FFileLines.Text := StringReplace(FFileLines.Text, Pattern[i],
  Replace[i], [rfReplaceAll]);
  FFileLines.SaveToFile('temporary.html');
  Execute('temporary.html');
end;
end.

有错误的地方

ShellExecute(0, 'open', PChar(Path), nil, nil, SW_SHOW);

图片错误 在此处输入图像描述

Ctrl + 单击

[SuppressUnmanagedCodeSecurity, DllImport(shell32, CharSet = CharSet.Auto, SetLastError = True, EntryPoint = 'ShellExecute')]
function ShellExecute(hWnd: HWND; Operation, FileName, Parameters,
  Directory: string; ShowCmd: Integer): HINST; external;
[SuppressUnmanagedCodeSecurity, DllImport(shell32, CharSet = CharSet.Auto, SetLastError = True, EntryPoint = 'ShellExecute')]
4

1 回答 1

0

查看ShellExecute.net 实现中的声明ShellAPI,很清楚该怎么做。停止转换PChar并编写您的代码,如下所示:

ShellExecute(0, 'open', Path, '', '', SW_SHOW);

直到现在我才意识到这一点,但您从 Delphi.net 进行的 Windows API 调用似乎使用与DllImport其他 .net 语言相同的属性。我想这是有道理的,这些只是普通的 p/invoke 调用,就像在 C# 互操作代码中一样。

有趣的是,您报告尝试传递nil给这些字符串参数之一会导致编译器错误。这意味着没有简单的方法可以将空指针传递给需要 C 字符串的 API 函数。你需要做的是使用一个重载的外部声明,它接收到Pointer你想要传递nil给的参数。

顺便说一句,Embarcadero 开发商在他们的DllImport声明中犯了一个错误。他们设置SetLastError = True哪个不正确,ShellExecute哪个不设置线程最后一个错误值。

于 2013-04-23T09:43:15.113 回答