FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to get an object already running with juno

 
Post new topic   Reply to topic     Forum Index -> Juno
View previous topic :: View next topic  
Author Message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Sat Oct 20, 2007 9:51 am    Post subject: How to get an object already running with juno Reply with quote

Greetings.

with COM object, one could get an object already open with a statement such as this,

Code:
Set oWorkbench = GetObject(, "TW4Win.Application")


and use it instead of opening a new object. How can this be accomplished with juno?

I tried,
Code:
DispatchObject(null,"TagEditor.Application")

and

DispatchObject(,"TagEditor.Application")


but these do not compile.

Any help would be greatly appreciated.

thanks,

jose
Back to top
View user's profile Send private message
John



Joined: 17 Jan 2006
Posts: 75

PostPosted: Sun Oct 21, 2007 5:14 am    Post subject: Reply with quote

Try this (untested):

Code:
auto unk = getActiveObject("TagEditor.Application");
scope (exit) tryRelease(unk);
scope obj = new DispatchObject(cast(IDispatch)unk);
Back to top
View user's profile Send private message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Sun Oct 21, 2007 3:37 pm    Post subject: Reply with quote

Thanks. I have created a quick program for you to try, which does not use TagEditor, but IE, instead. But, this program is not working,
Code:
import std.stdio;
import juno.com.client;
import juno.com.core;

void main()
{
  auto ie = new DispatchObject("InternetExplorer.Application");
  if (ie)
  {
    ie.set("Visible", true);
    ie.call("Navigate", "http://www.google.com");
    writefln ("IE opened...");
  }

  auto unk = getActiveObject("InternetExplorer.Application");
  scope (exit) tryRelease(unk);
  scope obj = new DispatchObject(cast(IDispatch)unk);

  if (objl)
   {
    writefln(obj);
    writefln ("IE Object appears ok...");
    obj.call("Quit");
    writefln ("close the opened IE...");
  }
  writefln ("Quit call should fail...");
  ie.call("Quit");
}


This is the run of it...

Code:

17:33:53.08>openIE
IE opened...
juno.com.client.DispatchObject
IE Object appears ok...
Error: ArgumentNullException: Value cannot be null.


somehow, it appears to be working, but the object that getActiveObject is getting, may be not be a true IE object.

thanks for the help.

jic
Back to top
View user's profile Send private message
John



Joined: 17 Jan 2006
Posts: 75

PostPosted: Mon Oct 22, 2007 2:43 am    Post subject: Reply with quote

InternetExplorer doesn't register itself with COM's running object table, and therefore you can't automate an existing instance.

It would be reasonable to assume that automating a current Tag Editor instance fails for the same reason.
Back to top
View user's profile Send private message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Mon Feb 04, 2008 11:23 pm    Post subject: Reply with quote

So, I googled this problem a bit and I found this c# code which opens an already opened Excel COM Object. How can I translate this to Juno?

Code:

public object GetExcelApp(ref bool Created)
{
   object obj2 = null;
   try
   {
      obj2 = RuntimeHelpers.GetObjectValue(Interaction.GetObject(null, "Excel.Application"));
      Created = false;
   }
   catch (Exception exception3)
   {
      ProjectData.SetProjectError(exception3);
      Exception exception1 = exception3;
      try
      {
         obj2 = RuntimeHelpers.GetObjectValue(Interaction.CreateObject("Excel.Application", ""));
         Created = true;
      }
      catch (Exception exception4)
      {
         ProjectData.SetProjectError(exception4);
         Exception exception2 = exception4;
         obj2 = null;
         ProjectData.ClearProjectError();
      }
      ProjectData.ClearProjectError();
   } return obj2;
}


thanks,

jose
Back to top
View user's profile Send private message
John



Joined: 17 Jan 2006
Posts: 75

PostPosted: Tue Feb 05, 2008 7:53 am    Post subject: Reply with quote

Code:
How can I translate this to Juno?


Have you tried yourself? Didn't it work? Why not post your own attempt instead of asking me to do all the work? Rather lazy...

As I said before, getActiveObject is the way to get a running instance - as long as the target application registers itself with COM's running object table. Excel *does* register itself, so getActiveObject will work.

It would appear that the code you've posted tries first to get a running instance of Excel. If it fails, it creates a new instance. And if that fails, it returns null. If it created a new instance, it sets "Created" to true.

Code:
IUnknown getExcelApp(ref bool created) {
  created = false;
  IUnknown ret;
  if ((ret = getActiveObject("Excel.Application")) is null) {
    if ((ret = coCreate!(IUnknown)("Excel.Application", ExecutionContext.All)) !is null) {
      created = true;
    }
  }
  return ret;
}

bool created;
auto excel = getExcelApp(created);
if (excel !is null) {
  scope(exit) tryRelease(excel);
  std.stdio.writefln(created);
}
Back to top
View user's profile Send private message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Tue Feb 05, 2008 10:55 am    Post subject: Reply with quote

John wrote:
Code:
How can I translate this to Juno?


Have you tried yourself? Didn't it work? Why not post your own attempt instead of asking me to do all the work? Rather lazy...


Indeed, John. My apologies. I should have, at least give it a try myself.

Thanks for the heads up.

jic
Back to top
View user's profile Send private message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Tue Feb 05, 2008 11:07 pm    Post subject: Reply with quote

Greetings.

Here is the code that I now have done...
Code:
import std.stdio;
import juno.com.client;
import juno.com.core;

IUnknown getExcelApp(ref bool created) {
  created = false;
  IUnknown ret;
  if ((ret = getActiveObject("Excel.Application")) is null) {
    if ((ret = coCreate!(IUnknown)("Excel.Application", ExecutionContext.All)) !is null) {
      created = true;
    }
  }
  return ret;
}

void main()
{
  bool created;
  auto excel = getExcelApp(created);
  if (excel !is null) {
    scope(exit) tryRelease(excel);
    std.stdio.writefln(created);
  }
}


I am getting
Code:
C:\cygwin\home\us319318\source\d\juno\com\core.d(2582): static assert  "GUID or string expected."


Here is the coCreate code where it is failing...
Code:
  public T coCreate(U)(U clsid, ExecutionContext context = ExecutionContext.InProcessServer) {
    GUID guid;
    static if (is(U == GUID)) {
      guid = clsid;
    }
    else static if (is(U == string)) {
      try {
        guid = GUID(clsid);
      }
      catch (FormatException) {
        int hr = CLSIDFromProgID(clsid.toUtf16z(), guid);
        if (FAILED(hr)) {
          static if (exceptionPolicy == ExceptionPolicy.ThrowIfNull)
            throw new COMException(hr);
          else
            return null;
        }
      }
    }
    else
      static assert(false, "GUID or string expected.");
Back to top
View user's profile Send private message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Tue Feb 05, 2008 11:10 pm    Post subject: Reply with quote

I should have said that the failure is that is not compiling...

thanks,

jic
Back to top
View user's profile Send private message
John



Joined: 17 Jan 2006
Posts: 75

PostPosted: Wed Feb 06, 2008 2:28 am    Post subject: Reply with quote

Here's the problem line from coCreate in juno.com.core:

Quote:
else static if (is(U == string)) {


I'm sure it used to work. Anyway, it should fix things if you change the line to this:

Code:
else static if (is(U : string)) {
Back to top
View user's profile Send private message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Wed Feb 06, 2008 9:15 am    Post subject: Reply with quote

John,

after the change it worked.

Thanks,

jose
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Juno All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group