16,467
社区成员
发帖
与我相关
我的任务
分享
我用C++和C#调用都报错,用Delphi调用没问题(DLL不是我们开发的没办法改),请大家帮忙看看;DoorIntf.dll 提取码:1234
目录
//定义创建访问IDDB接口对象的抽象工厂接口
IDoorIntfFactory = interface(IInterface)
['{F573C1C3-E0FD-44F4-8A32-4337B2FCF680}']
function CreateDDB(_host: wideString; _port: Integer): IDoorIntf; stdcall;
end;
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.
[uuid("F573C1C3-E0FD-44F4-8A32-4337B2FCF580")]
__interface IDoorIntfFactory{
virtual IDoorIntf* __stdcall CreateDDB(BSTR _host, Int32 _port) = 0;
};
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);
}
}
}
}
会不会还是wideString这个类型问题