[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Error with Microsoft.VisualBasic.Interaction.GetObject

jwang

10/4/2007 3:04:00 AM

I tried to use Microsoft.VisualBasic.Interaction.GetObject() to get a
reference to a running instance of an application that registers itself as a
COM server. There seems to be a problem with the the GetObject() method.
According to the MSDN documentation GetObject() takes two augments as
following:

public static Object GetObject (
[OptionalAttribute] string PathName,
[OptionalAttribute] string Class

So the first argument is optional if the second argument is present. Here
is what the doc says:

"If PathName is a zero-length string (""), GetObject returns a new object
instance of the specified class type. If the PathName argument is omitted,
GetObject returns a currently active object of the class type specified in
Class. If no object of the specified type exists, an error occurs."

This is exactly how GetObject() method in classic VB works.

However, if I omit the first argument, I get compilation error, "Error 4 No
overload for method 'GetObject' takes '1' arguments ".

Here is my code segment:

System.Object m_ltapp;

sProgID = "LTProject747";

m_ltapp = Microsoft.VisualBasic.Interaction.GetObject(sProgID);

I use the similar code in classic VB, it works just fine.

Any suggestion? Am I missing something here?

Thanks.

2 Answers

(Mattias Sjögren)

10/4/2007 5:42:00 AM

0

>Here is my code segment:
>
>System.Object m_ltapp;
>
>sProgID = "LTProject747";
>
>m_ltapp = Microsoft.VisualBasic.Interaction.GetObject(sProgID);
>
>I use the similar code in classic VB, it works just fine.
>
>Any suggestion? Am I missing something here?

If you only want to specify the Class parameter the call should be

m_ltapp = Microsoft.VisualBasic.Interaction.GetObject(null, sProgID);

You can also consider calling Marshal.GetActiveObject to avoid going
via the VB library.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.n... | http://www.dotneti...
Please reply only to the newsgroup.

jwang

10/4/2007 1:00:00 PM

0

Mattias,
Thanks for the input. Actually I tried GetObject(null, sProgID) before, and
I got the "Cannot create ActiveX component" exception. In our classic VB
code, if I use GetObject(, sProgID), I would get the exception, too. So we
always use GetObject(sProgID) to get reference to a specific running
instance.

I tried using Marshal.GetActiveObject(sProgID), and got "Invalid calss
string" exception.

The two things about the application I am trying to get a reference: a) it
only supports late binding for automation, b) it registers itself to the ROT
as a COM server on start. These seem to make things different.

Thanks









"Mattias Sjögren" wrote:

> >Here is my code segment:
> >
> >System.Object m_ltapp;
> >
> >sProgID = "LTProject747";
> >
> >m_ltapp = Microsoft.VisualBasic.Interaction.GetObject(sProgID);
> >
> >I use the similar code in classic VB, it works just fine.
> >
> >Any suggestion? Am I missing something here?
>
> If you only want to specify the Class parameter the call should be
>
> m_ltapp = Microsoft.VisualBasic.Interaction.GetObject(null, sProgID);
>
> You can also consider calling Marshal.GetActiveObject to avoid going
> via the VB library.
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.org
> http://www.msjogren.n... | http://www.dotneti...
> Please reply only to the newsgroup.
>