?
Solved

using CreateObject from delphi

Posted on 2006-05-03
11
Medium Priority
?
4,607 Views
Last Modified: 2008-01-16
hi, i have an application that we use at the office that provides an interface for the programmers, apparently from a DLL or a service.
It came with a good example to that interface as a VBScript:

language = "VBScript"
On Error Resume Next

Dim MyObj
Set MyObj = CreateObject ("TheApp.Application")

MyObj.NoGUI = False
done = MyObj.PerformTheThing()

If not done then
  MsgBox "MyApp: " & MyObj.GetErrorText
End If

Set MyObj = nothing


now, i am in need to perform th exact same thing from an application being written in delphi, is there any way to call CreateObject and perform the same functions?
code will be appreciated,

thanks
0
Comment
Question by:urif
  • 6
  • 5
11 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 16603631
var MyObj: variant

MyObj := CreateOLEObject("TheApp.Application")

Im guessing this is what you are looking for
0
 

Author Comment

by:urif
ID: 16603695
yes and no, i already tried that one but apparently i am failing to add a unit because it does not know what createoleobject is. i tried several of the standard units but so far nothing.
any ideas?
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 16603800
ComObj should be the unit you need to add in your USES clause
0
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!

 

Author Comment

by:urif
ID: 16604038
ok, now here's part of the code (console app). all i get are exceptions:

uses
  ComObj;

var _ComObj: Variant;
     done:boolean;
begin
    done:=false;
    writeln('creating object...');
   // CoInitializeEx(NIL,1); //do i need this???
    try
      _ComObj:=CreateOleObject('TheApp.Application');
    except
       writeln('exeption...');
       exit;
    end;

    writeln(performing default action...');

    try
       done:=_ComObj.DefaultFunction(); //do i need the parentesis?
    except
       writeln('exeption when calling function...');
       exit;
    end;

    if not done then
    begin
       writeln('failed to unmount all volumes');
    end;


end.

basically what i need is to open the COM object "TheApp.Application", and call 1 or 2 of its functions.
from vbs or even plain old C, it seems to be quite trivial, but so far from delphi it seems more complicated that it should...
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 16604112
No its pretty much the same in the way you use them in Delphi as well.
Ive never used it from within a console application though.

Have you tried stepping through your code to see where each error is occuring?
Can you give us some of the error messages?
0
 

Author Comment

by:urif
ID: 16604422
i'll post soon the exceptions, but can you post a working example of how to call the COM object from delphi?

forget about the console, it'll be a GUI application at the end, i just wrote a quick test program then....

thanks again
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 16604520
var _ComObj: Variant;
     done:boolean;
begin
    done:=false;

    MessageDlg('creating object...', mtInformation, [mbOK], 0);

    try
      _ComObj:= CreateOleObject('TheApp.Application');
    except
      begin
            MessageDlg('exeption...', mtInformation, [mbOK], 0);
        _ComObj.Unassigned
      end;
    end;

    if VarIsEmpty( _ComObj ) then
    begin
      MessageDlg('Missing COM Object...', mtWarning, [mbOK], 0);
    end;

    MessageDlg('performing default action...', mtWarning, [mbOK], 0);

    try
       done :=_ComObj.DefaultFunction;
    except
          MessageDlg('exeption when calling function...', mtInformation, [mbOK], 0);
    end;

    if not done then
    begin
      MessageDlg('failed to unmount all volumes', mtInformation, [mbOK], 0);
    end;
end;
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 16604526
Sorry should have an exit in here so that it doesnt do further processing

    if VarIsEmpty( _ComObj ) then
    begin
      MessageDlg('Missing COM Object...', mtWarning, [mbOK], 0);
      Exit;
    end;
0
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 400 total points
ID: 16604533
actually reformmating the sequence is probably better

var
  _ComObj: Variant;
  done:boolean;
begin
    done:=false;

    MessageDlg('creating object...', mtInformation, [mbOK], 0);

    try
      _ComObj:= CreateOleObject('TheApp.Application');
    except
      begin
            MessageDlg('exeption...', mtInformation, [mbOK], 0);
        _ComObj.Unassigned
      end;
    end;

    if VarIsEmpty( _ComObj ) then
    begin
      MessageDlg('Missing COM Object...', mtWarning, [mbOK], 0);
    end
    else
    begin
      MessageDlg('performing default action...', mtWarning, [mbOK], 0);
      try
         done :=_ComObj.DefaultFunction;
      except
        MessageDlg('exeption when calling function...', mtInformation, [mbOK], 0);
      end;
    end;

    if not done then
    begin
      MessageDlg('failed to unmount all volumes', mtInformation, [mbOK], 0);
    end;
end;
0
 

Author Comment

by:urif
ID: 16604574
great! on the GUI it works, not the command line, but nevermind.
the only thing is on _ComObj.Unassigned i am getting a "Method Unassigned not supported by automation object" exception
0
 

Author Comment

by:urif
ID: 16604615
exellent! thanks so much for the help.

i got it working now
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

807 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