双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

调用Delphi编写的DLL问题,请大家帮忙看看。

m277877855 m277877855 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

我用C++和C#调用都报错,用Delphi调用没问题(DLL不是我们开发的没办法改),请大家帮忙看看;DoorIntf.dll 提取码:1234

目录

Delphi接口定义

Delphi调用

C++接口定义

C++调用



Delphi接口定义

 //定义创建访问IDDB接口对象的抽象工厂接口
  IDoorIntfFactory = interface(IInterface)
    ['{F573C1C3-E0FD-44F4-8A32-4337B2FCF680}']
    function CreateDDB(_host: wideString; _port: Integer): IDoorIntf; stdcall;
  end;

Delphi调用

unit Unit1;
interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,uDoorIntf;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    factory: IDoorIntfFactory;
    db: IDoorIntf;
  protected
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  const
  commdb = 'DoorIntf.dll'; //动态库路径
var
  aLib: THandle;
  function CoDoorIntfFactory: IDoorIntfFactory; stdcall;

procedure DoDoorIntfFactory; stdcall;
function CoDoorIntfFactory;stdcall external commdb;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var ip:string;
begin

  ip:='127.0.0.1';
  factory := CoDoorIntfFactory;
  db := factory.CreateDDB(ip, 10);
  if db.ping then
  begin
    MessageBox(0, '连接成功', '系统提示', MB_OK);
  end
  else
  begin
    MessageBox(0, '连接失败', '系统提示', MB_OK);
  end;
end;

end.

 

C++接口定义

[uuid("F573C1C3-E0FD-44F4-8A32-4337B2FCF580")]
__interface IDoorIntfFactory{
	 virtual IDoorIntf*  __stdcall CreateDDB(BSTR _host, Int32 _port) = 0;
};

C++调用

typedef IDoorIntfFactory*(__stdcall *CoDoorIntfFactory)();//函数原型
	HINSTANCE hDllInst = LoadLibrary(L"DoorIntf.dll");
	if (hDllInst != NULL) {
		{
			BSTR bstr = ::SysAllocString(L"127.0.0.1");
			long nRes;
			IDoorIntfFactory* Ifactory = NULL;
			CoDoorIntfFactory Cfactory = (CoDoorIntfFactory)GetProcAddress(hDllInst, "CoDoorIntfFactory");

			if (Cfactory)
			{
				Ifactory=Cfactory();//这里报错
				if (NULL != Ifactory)
				{
					Ifactory->CreateDDB(bstr, 2);
				}
			}
		}
	}

 

...全文
评论 3
收藏 已收藏
分享
返回顶部
m277877855
VC/MFC 16,550 成员 421,605 内容