Need COM Interop Guru help please...

  • Thread starter Thread starter Richard
  • Start date Start date
R

Richard

Hi,

I am coding in C# and working with COM objects that were
written in Java. The product documentation for the Java
stuff states that the MS JVM does not support interface
inheritance - and then the documentation goes on to give
VB6 examples of how to get to a base interface given a
child interface.

Well I need to get to the base interface in C#! I have an
interface called "IPersistentObject" and it has a base
interface of "ISysObject". How can I get to ISysObject
given an instance of IPersistentObject? My code below
compiles but it's not working {pardon the spacing but the
newsgroup dooesn't do source code too well}:

{
IPersistentObject pobj = CreatePersistentObject();

ISysObject sysObj = (ISysObject)
Marshal.GetTypedObjectForIUnknown
(Marshal.GetIUnknownForObject(pobj), typeof(ISysObject));

IntPtr punk = Marshal.GetIUnknownForObject(sysObj);

sysObj.setObjectName("MyObject");
}

When this code runs "punk" reveals a non-zero value which
I'm taking to mean that in fact the QueryInterface did
succeed; the sysObj is an RCW that referes to something
that supports IUnknown. However when I try to use the
interface, to call "setObjectName", I get a
System.NullReference exception. Any ideas?

--Richard
 
Richard,
and then the documentation goes on to give
VB6 examples of how to get to a base interface given a
child interface.

Can you post that VB6 code?

How can I get to ISysObject
given an instance of IPersistentObject?

All you should need is a cast

ISysObject sysObj = (ISysObject)pobj;



Mattias
 
Yepp all they do in VB6 is cast to the other interface. I
tried that first in C# and I got a System.NullReference
exception too. I dunno what's going wrong, it is a
mystery wrapped in an RCW... Here's their VB6 code:

Dim pObj As IPersistentObject
Dim sysObj As ISysObject

Set pObj = sess.newObject("dm_document")
....
Set sysObj = pObj
....
sysObj.setObjectName docName

--Richard
 
Hi Richard,

NullReference exception related to the object.
Before you invoke the setObjectName method of sysObj, is you
sysObj a valid reference?
If it is valid, I think your problem must be something related to your
COM self, you can create a script client to invoke the COM to determine
this.

Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "Richard" <[email protected]>
| Sender: "Richard" <[email protected]>
| References: <[email protected]>
| Subject: Per Request: VB6 sample code
| Date: Thu, 11 Sep 2003 15:17:31 -0700
| Lines: 16
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcN4sn4cQ67pS9xARQC6guIc1zcbqQ==
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:184221
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Yepp all they do in VB6 is cast to the other interface. I
| tried that first in C# and I got a System.NullReference
| exception too. I dunno what's going wrong, it is a
| mystery wrapped in an RCW... Here's their VB6 code:
|
| Dim pObj As IPersistentObject
| Dim sysObj As ISysObject
|
| Set pObj = sess.newObject("dm_document")
| ...
| Set sysObj = pObj
| ...
| sysObj.setObjectName docName
|
| --Richard
|
|
 
Richard,
I tried that first in C# and I got a System.NullReference
exception too.

There are a number of possible reasons that can cause a
NullReferenceException. For example using the wrong thread apartment
(are you missing the STAThread attribute?) or a bug in the COM
object's IUnknown implementation.



Mattias
 
Back
Top