Solved

150 tricky points: Passing a COM Connection object

Posted on 2002-04-01
4
209 Views
Last Modified: 2011-09-20
Hi,

I’m building a COM DLL in Delphi, or to be more precise, an automation server that will be called from Visual Basic code.  I need to pass a COM Connection object to the Delphi DLL and then, because of compatibility issues with existing code, I need to use the connection object I received through a TADOConnection Delphi object.

Currently, I‘m receiving the COM object through an OleVariant parameter in my procedure, but I don’t know how to pass it to FConnection:

procedure TSomething.Set_COMConnection(COMConn: OleVariant);
var
     FConnection: TADOConnection;

begin

end;

If it’s of any help, the GET function seems to be working OK:

function TSomething.Get_COMConnection: OleVariant;
begin
     Result := FConnection.ConnectionObject;
end;

any ideas?

Thank you very much in advance,


F.
0
Comment
Question by:fabianmx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 150 total points
ID: 6910533

procedure TSomething.Set_COMConnection(COMConn: OleVariant);
var FConnection: TADOConnection;
    _c:          _Connection;
begin

 // Do whatever needs to be done to intialize the
 // TADOConnection object FConnection
 // ..

 // Note: I am assuming this procedure is declared as
 // SafeCall, thus the reason I am not try..except
 // wrapping any of the code. (If SafeCall, then the
 // procedure already has an exception frame.

 // Cast as IUnknown, and QI for the _Connection interface
 if (IUnknown(COMConn).QueryInterface(_Connection, _c) = S_OK) then
     FConnection.ConnectionObject:=_c
 else
   // Raise exception, or do whatever you need to do to
   // indicate failure.
   raise Exception.Create(DISP_E_TYPEMISMATCH);

end;

Hope this helps,
Russell
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6910995
Russell is right. You can typecast an OLEVariant into an IUnknown and use it then as such. Therefore, I would use the following code (looks prettier than Russels, but does pretty much the same):

    FConnection.ConnectionObject:=IUnknown(COMConn) as _Connection;

This eliminates the need for the manual QueryInterface() call and the exception raising if the type is not correct, and it does look more Delphi-like.
0
 

Author Comment

by:fabianmx
ID: 6911204
Hello Russell,

I tried your code but it didn't work.  The thing is, ConnectionObject is a read-only property, and although the project compiles without any error message, it crashes with a runtime error 8000FFFF, and a generic "Method 'COMConnection' of object 'ISomething' Failed"

However, the condition

if (IUnknown(COMConn).QueryInterface(_Connection, _c) = S_OK) then

evaluated as TRUE, yet I still can't pass it to FConnection

0
 

Author Comment

by:fabianmx
ID: 6911612
Hi Russell,

Nevermind my last comment, the code worked perfectly, it's just that I forgot to intialize the TADOConnection object FConnection.

As usual an excellent answer.  

AvonWyss, I'm giving the points to Russell because he offered a working solution first.  Although your code is neater I'd rather stick to Russell's because since I'm a novice Delphi programmer I'd rather have the code as explicit as possible.

Thanx anyway,


F.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help on project with Soap 10 63
Not able to call Delphi XE10 dll function from Delphi 6.0 4 49
Browsing a TTreeView in Delphi 5 32
Problem working with dynamic array - help 2 36
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question