delphi - 测试 Delphi DLL 导致 VB6 IDE 崩溃

标签 delphi dll vb6 typelib

我第一次尝试用 Delphi 编写 DLL。到目前为止,一切都很好。通过使用类型库,我能够毫无困难地在 DLL 之间传递 Widestring。

目前令人好奇的是,我使用 VB6 作为测试平台,每次我在 IDE 中运行测试时,程序都会运行,然后 IDE 进程突然从内存中消失 - 没有错误消息,什么也没有。如果我单步执行代码,一切正常,直到执行最后一行,然后 IDE 就会消失。

相比之下,当我将测试编译为 EXE 时,程序运行到最后,没有错误消息等。

以前有人遇到过这个问题吗?有没有一个明显的解决方案摆在我面前?

下面的源代码,以防万一:

--项目

library BOSLAD;

uses
  ShareMem,
  SysUtils,
  Classes,
  BOSLADCode in 'BOSLADCode.pas';

exports
  version,
  DMesg,
  foo;
{$R *.res}

begin
end.

--单位

unit BOSLADCode;

interface
  function version() : Double; stdcall;
  procedure DMesg(sText : WideString; sHead : WideString ); stdcall;
  function foo() : PWideString; stdcall;

implementation
  uses Windows;

  function version() : Double;
  var
    s : String;
  begin
    result := 0.001;
  end;

  procedure DMesg( sText : WideString; sHead : WideString);
  begin
    Windows.MessageBoxW(0, PWideChar(sText), PWideChar(sHead), 0);
  end;

  function foo() : PWideString;
  var s : WideString;
  begin
    s := 'My dog''s got fleas';
    result := PWideString(s);
  end;
end.

-- 类型库

 // This is the type library for BOSLAD.dll
      [
      // Use GUIDGEN.EXE to create the UUID that uniquely identifies
      // this library on the user's system. NOTE: This must be done!!
         uuid(0C55D7DA-0840-40c0-B77C-DC72BE9D109E),
      // This helpstring defines how the library will appear in the
      // References dialog of VB.
         helpstring("BOSLAD TypeLib"),
      // Assume standard English locale.
         lcid(0x0409),
      // Assign a version number to keep track of changes.
         version(1.0)
      ]
      library BOSLAD
      {

      // Now define the module that will "declare" your C functions.
      [
         helpstring("Functions in BOSLAD.DLL"),
         version(1.0),
      // Give the name of your DLL here.
         dllname("BOSLAD.dll")
      ]
      module BOSLADFunctions
      {
[helpstring("version"), entry("version")] void __stdcall version( [out,retval] double* res );
[helpstring("DMesg"), entry("DMesg")] void __stdcall DMesg( [in] BSTR msg, [in] BSTR head );
[helpstring("foo"), entry("foo")] void __stdcall foo( [out,retval] BSTR* msg );
      } // End of Module
      }; // End of Library
<小时/>

我将 WideString 的声明移到了声明它的函数之外,期望这会增加变量的生命周期,使其比 foo 函数的生命周期更长。没有任何区别。

同样,我在 VB6 中注释了对 foo 函数的调用。这也没有什么区别。无论我做什么,VB6 IDE 在执行最后一行代码后就会死掉。

原因除了指向局部变量的指针之外。但什么?

最佳答案

result := PWideString(s);

您将在此处返回指向局部变量的指针。它立即失效。

关于delphi - 测试 Delphi DLL 导致 VB6 IDE 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/193651/

相关文章:

delphi - 提示指令的语法规范

c++ - 将 DllMain winapi .dll 导入 Visual Studio 项目 C++

vb6 - 获取有关使用 FileSystemObject 的木马警告

.net - 丢失的 VB6 源代码;是否可以覆盖另一个对象中的方法?

delphi - 如何在运行时使用另一个过程中的过程在组合框上设置 onChange 事件?

delphi - 传递给 Delphi 控制台应用程序的命令行参数的最大长度

debugging - WinDbg 符号解析

windows - 适用于x64的SQLite3.dll

c# - 无法从 system.collections.generic.list 转换为字符串到 Vba.collections

mysql - Delphi 从 MySql 加载 Blob