473,322 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Need COM Interop Guru help please...

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
Nov 15 '05 #1
4 1177
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
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

Nov 15 '05 #3

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" <ri******@amgen.com>
| Sender: "Richard" <ri******@amgen.com>
| References: <0d****************************@phx.gbl>
| Subject: Per Request: VB6 sample code
| Date: Thu, 11 Sep 2003 15:17:31 -0700
| Lines: 16
| Message-ID: <04****************************@phx.gbl>
| 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
|
|

Nov 15 '05 #4
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rmn | last post by:
Hi, I'm looking for the translation betwen the color spaces, but I must be mistaken with the methods to use, losing the values. Please, I need a little help of a "coloured guru"....
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
7
by: R Reyes | last post by:
Can someone please explain to me why I can't get the MS Word Interop assembly to work in my VS2005 project? I'm trying to manipulate MS Word from my Web Form application and I can't get passed...
16
by: Singulus | last post by:
Hello all, I've searched for similar threads, I've found some bit of useful info here and there, but nevertheless I want to post my questions...So, how can I (we, in fact the forum can benefit...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.