从 BPL 函数返回字符串

发布于 2024-08-10 06:54:20 字数 1414 浏览 3 评论 0原文

有一个函数(如下简化)从 BPL 导出,该 BPL

function DoA(amount: currency; var Info: string): Currency; stdcall;
 begin
  result := amount * 19;
  Info:= 'Some Text about the result';
 end;

是通过 LoadPackage 从主程序加载的,而 GetProcAddress 对于其他函数运行良好。 但是这个在调用时会带来很多错误;

BPL 与(简化)一起使用

  bplhandle: HModule;
  BPLDoA: function (amount: currency; var Info: string): Currency; stdcall;
  intoStr : string;

 begin
  bplhandle:=LoadPackage('test.bpl');
   if bplhandle <> 0 then
    begin
     @BPLDoA:=GetProcAddress(bplhandle,'DoA');
       if assigned(BPLDoA) then
       result := BPLDoA(123, intoStr);
    end;
 end;

似乎在程序结束时发生的异常, 但更正后的文本返回到 intoStr (用断点查看)

该错误是否与 Info 参数是 var 和/或字符串有关?

错误消息是

项目 Project1.exe 引发异常类 EInvalidPointer 并显示消息“无效指针操作”

感谢

更多信息> 同一 bpl/单元中的另一个函数工作正常

function DoB(amount: currency): Currency; stdcall;
  result := amount * 19;
 end;

Mad except>

异常类:EInvalidPointer 异常消息:无效的指针操作。

主线程($1b7c): 0040276f +013 Project1.exe 系统@FreeMem 00404650 +01c Project1.exe 系统@LStrClr 00483814 +15c Project1.exe Unit1 97 +11 TForm1.Button3Click 00462430 +064 Project1.exe 控件 TControl.Click 0045a870 +01c Project1.exe StdCtrls TButton.Click

have a function, simplified below, that is exported from a BPL

function DoA(amount: currency; var Info: string): Currency; stdcall;
 begin
  result := amount * 19;
  Info:= 'Some Text about the result';
 end;

its loaded from the main program with LoadPackage, and GetProcAddress which works fine for the other functions.
but this one brings up many errors when its called;

BPL Is used with (simplified)

  bplhandle: HModule;
  BPLDoA: function (amount: currency; var Info: string): Currency; stdcall;
  intoStr : string;

.

 begin
  bplhandle:=LoadPackage('test.bpl');
   if bplhandle <> 0 then
    begin
     @BPLDoA:=GetProcAddress(bplhandle,'DoA');
       if assigned(BPLDoA) then
       result := BPLDoA(123, intoStr);
    end;
 end;

the exception that seems to happen at the end of the Procedure,
but the corrected text is returned into intoStr (viewed with a break point)

would the error have anything to do with the Info param being a var and/or a string?

The Error message is

Project Project1.exe raised exception class EInvalidPointer with message 'Invalid pointer operation'

thanks

more info>
another function from the same bpl/unit works fine

function DoB(amount: currency): Currency; stdcall;
  result := amount * 19;
 end;

Mad Except>

exception class : EInvalidPointer
exception message : Invalid pointer operation.

main thread ($1b7c):
0040276f +013 Project1.exe System @FreeMem
00404650 +01c Project1.exe System @LStrClr
00483814 +15c Project1.exe Unit1 97 +11 TForm1.Button3Click
00462430 +064 Project1.exe Controls TControl.Click
0045a870 +01c Project1.exe StdCtrls TButton.Click

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

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

发布评论

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

评论(3

冰雪梦之恋 2024-08-17 06:54:20

您尚未将 EXE 项目配置为“使用运行时包构建”。在项目选项的“包”部分中找到它。 (文档

EInvalidPointer 当内存管理器尝试释放未分配的内容时,会出现异常。这表明您有两个不同的内存管理器处于活动状态。您的 BPL 正在使用 RTL 包中的 BPL,该包出现在包的“需求”列表中。另一方面,您的 EXE 使用编译到 EXE 模块中的内存管理器。

通过告诉您的 EXE 使用运行时包来解决这个问题,然后确保 RTL 包位于所需包的列表中。

You haven't configured your EXE project to "build with run-time packages." Find that in the "packages" section of your project options. (Documentation)

The EInvalidPointer exception comes when a memory manager tries to free something that it didn't allocate. That suggests you have two different memory managers active. Your BPL is using the one from the RTL package, which appears on your package's "requires" list. Your EXE, on the other hand, is using the memory manager compiled into the EXE module.

Fix that by telling your EXE to use run-time packages, and then make sure the RTL package is on the list of required packages.

記柔刀 2024-08-17 06:54:20

您的导入声明与导出函数的签名完全匹配吗?

一定是这样的:

DoAProc: function (amount: currency; var Info: string): Currency; stdcall;

Does your import declaration exactly match the exported function's signature?

Must be like this:

DoAProc: function (amount: currency; var Info: string): Currency; stdcall;
贪了杯 2024-08-17 06:54:20

如果您不想被要求提供额外的 BPL(现在您的主 exe 正在使用运行时 BPL),另一个选择是在您的项目中包含 ShareMem 单元。查看 Delphi 帮助文件中的“共享内存”主题。

ms-help://embarcadero.rs2010/rad/Sharing_Memory.html

Another option if you don't want to be required to ship additional BPLs (which you will now that your main exe is using runtime BPLs), is to include the ShareMem unit in your project. Check out the "Sharing Memory" topic in the Delphi help file.

ms-help://embarcadero.rs2010/rad/Sharing_Memory.html

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