pass adoconnection from vba to delphi

2019-05-01 06:48发布

I'm looking to create a COM object in a VBA macro and then pass it to a Delphi DLL (D2009). What should my procedure declaration in Delphi look like?

Background: I'm expecting (hoping) the VBA macro to: create the COM object, invoke the Delphi DLL, pass the COM object to the Delphi DLL procedure, stay alive until the Delphi DLL closes itself (the DLL will have embedded forms for the user to interact with).

I think I'll need to create a callback function to let the VBA macro know that I'm done so it can tidy up but I'll work on that independently of this question.

UPDATE More specifically: What should the exported function declaration be for the Delphi DLL.

标签: delphi com vb6
1条回答
再贱就再见
2楼-- · 2019-05-01 07:05

you have to pass ADO Connection interface link _Connection to delphi procedure then create TADOConnection instance and replace ConnectionObject with new interface link

library Project1;
uses ADODB;

{$R *.res}

    procedure SetConnection(aDBConnection : _Connection);  stdcall;
    var connect : TADOConnection;
    begin
        connect := TADOConnection.Create(nil);
        try
            connect.ConnectionObject := aDBConnection;
            //here you can use your connection
        finally
            connect.Free();
        end;
    end;


exports SetConnection name 'SetDBConnection';

begin
end.

it is better to use stdcall calling convention. using export keyword setConnection proc is available from uotside with SetDBConnection name , so you can LoadLibrary and getProcAddress to find its entry point (really I don't know VBA so I can't say how to load library using it)

查看更多
登录 后发表回答