pass adoconnection from vba to delphi

▼魔方 西西 提交于 2020-01-02 04:54:19

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/9434762/pass-adoconnection-from-vba-to-delphi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!