Access violation when calling COM object method in Delphi

I am trying to use a COM object from a third-party vendor to control certain events in their software application. I have managed to do this in a java script but when I try to do the same in Delphi I get a runtime error when I call a specific method. (Access violation at address 0x00000004. Read of address 0x00000004.)
 
In JScript the code looks like this:
 
var captureCmd = cmdFactory.CreateCmd("startcapture");
var sender = new ActiveXObject("DartfishMDTK.DFMetadataSender");
sender.Initialize();
sender.SendCommand(captureCmd);

In Delphi:
 
 
    var
        DFSender: IDFMetadataSender2;    // Version 2 of the interface includes SendCommand method
        DFCommand : IDFMetadataCmd;
        DFCmdFactory : IDFMetadataCmdFactory;
...
 
    DFSender := CreateComObject(CLASS_DFMetadataSender) as IDFMetadataSender2;
    if not Assigned(DFSender) then
    begin
      ShowMessage('Could not get Dartfish interface for DFMetadataSender.');
      Exit;
    end;
    DFSender.Initialize;
 
    DFCommand := DFCmdFactory.CreateCmd('startcapture');
    DFSender.SendCommand(DFCommand);

I am using a type library file : DFMETADATATOOLKITLib_TLB.pas that was created with the tlibimp utiliy:
tlibimp -P+ "c:\Program Files\Dartfish\Dartfish software\bin\im_MetaDataToolkit.dll"
 
I came across a similar query that was answered at http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20809752.html.
 
I would appreciate any help.
LVL 1
riaanvddoolAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

illusion_chaserCommented:
Register your COM.
If it is an EXE, then use "C:\myCOM.exe /regserver" (start -> run).
If it is a DLL, then use "regsvr32 C:\myCOM.dll" (start -> run).

Also, try adding "CoInitialize(nil);" before "DFSender := CreateComObject(CLASS_DFMetadataSender) as IDFMetadataSender2;" line. Add "CoUninitialize;" after "DFSender.SendCommand(DFCommand);" line.

riaanvddoolAuthor Commented:
As far as I know the COM has been registered by the installation program. If I open regedit I can see the key:
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\DartfishMDTK.DFMetadataSender

Also, it works in JScript.

Or are there different ways of registering the COM that I  am missing?

riaanvddoolAuthor Commented:
I get undeclared identifier at CoInitialize. Is there a unit which I should add?

The DFSender.SendCommand(DFCommand) method is called n times. Is it ok to call CoUnitialize when the form is destoyed?

Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

illusion_chaserCommented:
Add "ActiveX" to your uses section;
illusion_chaserCommented:
Yes call CoInit... when form is created and CoUninit... when it is destroyed.
riaanvddoolAuthor Commented:
CoInit... now on Form Create and CoUninit... on Destroy.
Still gives runtime exception.

illusion_chaserCommented:
Instead of line:
DFSender := CreateComObject(CLASS_DFMetadataSender) as IDFMetadataSender2;

Try line:
DFSender := IDispatch(CreateOleObject('DartfishMDTK.DFMetadataSender')) as IDFMetadataSender2;
illusion_chaserCommented:
Also, you can try importing the TLB from the ActiveX component using Delphi (go to Project -> Import Type Library). See if the get the same DFMETADATATOOLKITLib_TLB.PAS as you have now.
riaanvddoolAuthor Commented:
Tried line:
DFSender := IDispatch(CreateOleObject('DartfishMDTK.DFMetadataSender')) as IDFMetadataSender2;

Now using TLB generated by Import Type Library

Still no luck :(

riaanvddoolAuthor Commented:
I have now tried adding a component (BlahSender)(installed into the palette by the Import Type Library dialog) to the form.
Calling
BlahSender.SendCommand(DFCommand);
still gives the same exception.
illusion_chaserCommented:
You say that using java script you are successful...
Can you send this ActiveX + your test code, so I can test it (coz, I have nothing more to guess about).
My email: illusion_chaser@hotmail.com
Eduard GherguLeader SibiuCommented:
Hello!

Try to create derivate classes from the interfaces and create objects based on your classes instead of interfaces. Usually, an interface must be implemented. ;)
riaanvddoolAuthor Commented:
As I have it the interface is implemented in the COM object. If it was not implemented none of the methods would work. In this case only the SendCommand method fails.

I have checked that the SendCommand method executes and has the desired effect in the third party application. So it is not a case of a silent exception.

Something I should probably mention is that the Interface IDFMetadataSender2 is the same as IDFMetadataSender except that SendCommnd is only present in IDFMetadataSender2.
illusion_chaserCommented:
Hi. I am glad I helped you with this one.
Here is the late binding code.

Regards.
ic

//------------------------------------------------------------------------------------
implementation

{$R *.dfm}

uses DFMETADATATOOLKITLib_TLB, ActiveX, ComObj;

var
  DartfishEnabled: Boolean;
  DFSender: OleVariant;
  DFCommand: OleVariant;
  DFCmdFactory: OleVariant;

procedure TForm1.InitializeDartfish;
begin
  DartfishEnabled := False;

try    
   DFCmdFactory := CreateOleObject('DartfishMDTK.DFMetadataCmdFactory');
   if (VarIsEmpty(DFCmdFactory)) then
   begin
     ShowMessage('Could not get Dartfish interface for DFCmdFactory.');
     Exit;
   end;

   DFSender := CreateOleObject('DartfishMDTK.DFMetadataSender');
   if (VarIsEmpty(DFSender)) then
   begin
     ShowMessage('Could not get Dartfish interface for DFMetadataSender.');
     Exit;
   end;

   DFSender.Initialize;

   DartfishEnabled := True;
except
   on E: Exception do
     ShowMessage('Could not get Dartfish. Exception message: ' + E.Message);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CoInitialize(nil);
  InitializeDartfish;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if DartfishEnabled then
begin
   DFCommand := DFCmdFactory.CreateCmd('startcapture');
   DFSender.SendCommand(DFCommand);
end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FinalizeDartfish;
  CoUninitialize;
end;

procedure TForm1.FinalizeDartfish;
begin
  //For late binding (when you use OlaVariant)
  if (not VarIsEmpty(DFSender)) then DFSender := Unassigned;
  if (not VarIsEmpty(DFSender)) then DFCommand := Unassigned;
  if (not VarIsEmpty(DFSender)) then DFCmdFactory := Unassigned;
end;

end.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.