Delphi动态调用DLL问题,很诡异,求帮忙,快哭 了……

22fqwed 2013-03-17 08:15:21
自己编的一个程序,DLL也是我用delphi2010写的
静态调用dll时没问题,动态调用时出现错误,调用此dll里其它函数没有问题;
如果是函数内部使用了delphi自己的类型,那我传递出来了是shortstring类型啊

源代码:http://l5.yunpan.cn/lk/Q8biwcnFXEpgm
运行formtest.exe 可以看效果

...全文
499 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
budded 2013-03-18
  • 打赏
  • 举报
回复
你把函数定义统一下就可以了 还有楼上的楼上的,shortstring跟sharemem木有关系,请测试再来说话
budded 2013-03-18
  • 打赏
  • 举报
回复
服了你了 你看看你的dll函数定义:function GetIEUrl(x:integer):shortstring;stdcall; 你再看看你的函数调用的地方:type TFunc = function():ShortString; 这个是静态调用的地方:function GetIEUrl():shortstring; stdcall;external 'findie.dll'; 真能干啊,能写出三种样式来
sololie 2013-03-18
  • 打赏
  • 举报
回复


library findie;
uses
  ShareMem,   // 必须第一位,其他引用该dll的项目也一样
  Windows,
  Messages,
  SysUtils 
   ........
sololie 2013-03-18
  • 打赏
  • 举报
回复

unit4 做如下修改:
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, {sharemem}; // 去掉sharemem,这个不是放这里的

program formtest;

uses
  ShareMem,  // 加在这里
  Forms,
  dialogs,
  Unit4 in 'Unit4.pas' {Form4};

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm4, Form4);
  Application.Run;
end.

=====================


function geturlstringdynamic(): shortstring;
type
  TFunc = function(x:Integer): shortstring; stdcall; // 你DLL中函数原型必须匹配
var
  Th: Thandle;
  Tf: TFunc;
  Tp: TFarProc;
begin

  Th := LoadLibraryw('findie.dll');
  @Tf := GetProcAddress(Th, 'GetIEUrl');
  result := Tf(111);
  freelibrary(th);
end;

==================

findie.dll 做如下修改:

function GetIEUrl(x: integer): shortstring; stdcall;
var
  DDE: TDdeClientConv;
  bret: boolean;
  presult: pansichar;
  sresult: shortstring;
begin
  // dde := TDdeClientConv.Create(dde); 改为下面
  dde := TDdeClientConv.Create(nil);
  dde.OpenLink;
  bret := DDE.SetLink('Iexplore', 'WWW_GetWindowInfo');
  if bret then
  begin
    presult := dde.RequestData('0xFFFFFFFF,sURL, sTitle');

    sresult := shortstring(presult);
    result := shortstring(geturlstring(sresult));

    dde.CloseLink;
    dde.free;
    //result:='15s';
  end
  else
  begin
    result := 'no ie';
  end;
end;


最后重写build所有项目

22fqwed 2013-03-18
  • 打赏
  • 举报
回复
帖子要结扎了,
sololie说的很对,给出来的代码也能成功运行,就是更换网页地址后 form窗口多点几次按钮就死了
22fqwed 2013-03-18
  • 打赏
  • 举报
回复
引用 8 楼 budded 的回复:
你把函数定义统一下就可以了 还有楼上的楼上的,shortstring跟sharemem木有关系,请测试再来说话
我统一了,都是shortstring的,你看看代码,或者你看哪边没统一的,修改下运行试试 !
22fqwed 2013-03-18
  • 打赏
  • 举报
回复
引用 3 楼 jinghai1776 的回复:
问一下:你是否把 shortstring 作为 DLL 外部函数的参数或返回值传递了?
是的,有什么问题吗? 静态调用就可以的
sololie 2013-03-17
  • 打赏
  • 举报
回复
sharemem 单元要放在所有单元的最前面,即第一位。 dll,或引用到该dll的其他所有项目都必须把引用sharememe单元并放在第一位。 另外要注意使用shortstring时长度截断的问题,shortstring长度为255,不能存入太长的字串,应尽量用pcahr。
静_海 2013-03-17
  • 打赏
  • 举报
回复
问一下:你是否把 shortstring 作为 DLL 外部函数的参数或返回值传递了?
22fqwed 2013-03-17
  • 打赏
  • 举报
回复
不能下载的话联系我 miaozilong@gmail.com
22fqwed 2013-03-17
  • 打赏
  • 举报
回复

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧