View previous topic :: View next topic |
Author |
Message |
jicman
Joined: 22 Dec 2004 Posts: 298 Location: Rochester, NY
|
Posted: Sat Oct 20, 2007 9:51 am Post subject: How to get an object already running with juno |
|
|
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 |
|
|
John
Joined: 17 Jan 2006 Posts: 75
|
Posted: Sun Oct 21, 2007 5:14 am Post subject: |
|
|
Try this (untested):
Code: | auto unk = getActiveObject("TagEditor.Application");
scope (exit) tryRelease(unk);
scope obj = new DispatchObject(cast(IDispatch)unk); |
|
|
Back to top |
|
|
jicman
Joined: 22 Dec 2004 Posts: 298 Location: Rochester, NY
|
Posted: Sun Oct 21, 2007 3:37 pm Post subject: |
|
|
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 |
|
|
John
Joined: 17 Jan 2006 Posts: 75
|
Posted: Mon Oct 22, 2007 2:43 am Post subject: |
|
|
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 |
|
|
jicman
Joined: 22 Dec 2004 Posts: 298 Location: Rochester, NY
|
Posted: Mon Feb 04, 2008 11:23 pm Post subject: |
|
|
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 |
|
|
John
Joined: 17 Jan 2006 Posts: 75
|
Posted: Tue Feb 05, 2008 7:53 am Post subject: |
|
|
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 |
|
|
jicman
Joined: 22 Dec 2004 Posts: 298 Location: Rochester, NY
|
Posted: Tue Feb 05, 2008 10:55 am Post subject: |
|
|
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 |
|
|
jicman
Joined: 22 Dec 2004 Posts: 298 Location: Rochester, NY
|
Posted: Tue Feb 05, 2008 11:07 pm Post subject: |
|
|
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 |
|
|
jicman
Joined: 22 Dec 2004 Posts: 298 Location: Rochester, NY
|
Posted: Tue Feb 05, 2008 11:10 pm Post subject: |
|
|
I should have said that the failure is that is not compiling...
thanks,
jic |
|
Back to top |
|
|
John
Joined: 17 Jan 2006 Posts: 75
|
Posted: Wed Feb 06, 2008 2:28 am Post subject: |
|
|
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 |
|
|
jicman
Joined: 22 Dec 2004 Posts: 298 Location: Rochester, NY
|
Posted: Wed Feb 06, 2008 9:15 am Post subject: |
|
|
John,
after the change it worked.
Thanks,
jose |
|
Back to top |
|
|
|