吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 439|回复: 3
收起左侧

[求助] 以下delphi生成的dll编译不报错用AHK调用也不显示,何故?

[复制链接]
冥界3大法王 发表于 2025-3-7 09:25
本帖最后由 冥界3大法王 于 2025-3-7 09:27 编辑

最终生成的文件夹如下:
常用1======》*.txt
常用2======》*.txt
常用3======》*.txt
MenuDLL.dll
test.ahk

Delphi的DLL部分:
[Delphi] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
library MenuDLL;
 
uses
  System.Types,
  System.IOUtils,
  System.SysUtils,
  System.Classes,
  Vcl.Forms,
  Vcl.Menus,
  Vcl.Graphics,
  Vcl.Clipbrd,
  Winapi.Windows;
 
type
  TForm4 = class(TForm)
    PopupMenu1: TPopupMenu;
    procedure MenuDrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect; Selected: Boolean);
    procedure MenuMeasureItem(Sender: TObject; ACanvas: TCanvas; var Width, Height: Integer);
    procedure TextFileClick(Sender: TObject);
  private
    procedure BuildMenuStructure;
  public
    constructor Create(AOwner: TComponent); override;
  end;
 
var
  Form4: TForm4;
 
constructor TForm4.Create(AOwner: TComponent);
begin
  inherited CreateNew(AOwner);
  PopupMenu1 := TPopupMenu.Create(Self);
  BuildMenuStructure;
end;
 
procedure TForm4.BuildMenuStructure;
var
  DllPath: string;
  DirList: TStringList;
  Dirs: TStringDynArray;  // 添加动态数组作为中间变量
  i, j: Integer;
  MainItem, SubItem: TMenuItem;
  Files: TStringList;
begin
  DllPath := ExtractFilePath(GetModuleName(0));
  DirList := TStringList.Create;
  try
    // 修改为两步获取目录
    Dirs := TDirectory.GetDirectories(DllPath, '*', TSearchOption.soTopDirectoryOnly);
    DirList.AddStrings(Dirs);
    DirList.Sort;
    for i := 0 to DirList.Count - 1 do
    begin
      
    end;
  finally
    DirList.Free;
  end;
end;
 
procedure TForm4.MenuDrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect; Selected: Boolean);
begin
  ACanvas.FillRect(ARect);
  ACanvas.Font.Size := 12;
  ACanvas.Font.Name := '微软雅黑';
  ACanvas.TextOut(ARect.Left, ARect.Top, (Sender as TMenuItem).Caption);
end;
 
procedure TForm4.MenuMeasureItem(Sender: TObject; ACanvas: TCanvas; var Width, Height: Integer);
begin
  Height := 40;
  Width := 750;
end;
 
procedure TForm4.TextFileClick(Sender: TObject);
var
  FilePath: PChar;
  Content: string;
begin
  FilePath := PChar((Sender as TMenuItem).Tag);
  if FileExists(FilePath) then
  begin
    Content := TFile.ReadAllText(FilePath);
    Clipboard.AsText := Content;
  end;
end;
 
procedure ShowPopupMenu; stdcall;
var
  P: TPoint;
begin
  try
    // 必须创建Application实例
    Application.Initialize;
    Application.MainFormOnTaskbar := False;
 
    GetCursorPos(P);
    Form4 := TForm4.Create(nil);
    try
      Form4.PopupMenu1.Popup(P.X, P.Y);
      // 需要处理消息循环
      Application.Run;
    finally
      Form4.Free;
    end;
  except
    on E: Exception do
      MessageBox(0, PChar(E.Message), 'DLL Error', MB_ICONERROR);
  end;
end;
 
exports
  ShowPopupMenu;
 
begin
end.



AutoHotkey 1.1 调用部分: (大部分都是垃圾代码(用于测试))
[Asm] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
MsgBox % A_PtrSize == 4 ? "32位AHK" : "64位AHK"
 
^1::
hModule := DllCall("LoadLibrary", "Str", "MenuDLL.dll", "Ptr")
if (hModule == 0)
{
    MsgBox 无法加载DLL! 错误代码: %A_LastError%
    return
}
 
success := DllCall("MenuDLL\ShowPopupMenu", "CDecl Int")
if (success == 0)
{
    MsgBox 函数调用失败! 错误代码: %A_LastError%
}
DllCall("FreeLibrary", "Ptr", hModule)
return
 
 
^2::
result := DllCall("MenuDLL\TestDLL", "CDecl Str")
MsgBox 测试结果:%result%
return
 
^3::
; 使用ANSI版本路径
DllPath := "MenuDLL.dll"
hModule := DllCall("LoadLibrary", "Str", DllPath)
if (hModule == 0)
{
    MsgBox 错误代码: %A_LastError%
    return
}
 
; 显式指定调用约定
DllCall("ShowPopupMenu", "Ptr", hModule, "CDecl")
DllCall("FreeLibrary", "Ptr", hModule)
return

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

kenxy 发表于 2025-3-7 09:59
问题会不会出现在这个地方呢?
[Delphi] 纯文本查看 复制代码
1
2
3
finally
      Form4.Free;
end;
dig2000 发表于 2025-3-7 13:20
Form4没有和Application产生关系,所以Application.run的时候没有任何窗口显示,可以增加Application.CreateForm(TForm4, Form4); 其实程序可以更简化一些,没有必要用Application。
 楼主| 冥界3大法王 发表于 2025-3-7 14:21
dig2000 发表于 2025-3-7 13:20
Form4没有和Application产生关系,所以Application.run的时候没有任何窗口显示,可以增加Application.Creat ...

那给完整的表演下吧。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-7-31 06:53

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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