Need COM Interop Guru help please...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard

    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 "IPersistentObj ect" and it has a base
    interface of "ISysObject ". How can I get to ISysObject
    given an instance of IPersistentObje ct? My code below
    compiles but it's not working {pardon the spacing but the
    newsgroup dooesn't do source code too well}:

    {
    IPersistentObje ct pobj = CreatePersisten tObject();

    ISysObject sysObj = (ISysObject)
    Marshal.GetType dObjectForIUnkn own
    (Marshal.GetIUn knownForObject( pobj), typeof(ISysObje ct));

    IntPtr punk = Marshal.GetIUnk nownForObject(s ysObj);

    sysObj.setObjec tName("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.NullRefe rence exception. Any ideas?

    --Richard


  • Mattias Sjögren

    #2
    Re: Need COM Interop Guru help please...

    Richard,
    [color=blue]
    >and then the documentation goes on to give
    >VB6 examples of how to get to a base interface given a
    >child interface.[/color]

    Can you post that VB6 code?

    [color=blue]
    >How can I get to ISysObject
    >given an instance of IPersistentObje ct?[/color]

    All you should need is a cast

    ISysObject sysObj = (ISysObject)pob j;



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Richard

      #3
      Per Request: VB6 sample code

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

      Dim pObj As IPersistentObje ct
      Dim sysObj As ISysObject

      Set pObj = sess.newObject( "dm_documen t")
      ....
      Set sysObj = pObj
      ....
      sysObj.setObjec tName docName

      --Richard

      Comment

      • Jeffrey Tan[MSFT]

        #4
        RE: Per Request: VB6 sample code


        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" <richardn@amgen .com>
        | Sender: "Richard" <richardn@amgen .com>
        | References: <0dfb01c3789f$7 5a61e20$a501280 a@phx.gbl>
        | Subject: Per Request: VB6 sample code
        | Date: Thu, 11 Sep 2003 15:17:31 -0700
        | Lines: 16
        | Message-ID: <04f101c378b2$7 e1c46c0$a401280 a@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: AcN4sn4cQ67pS9x ARQC6guIc1zcbqQ ==
        | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
        | Path: cpmsftngxa06.ph x.gbl
        | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1842 21
        | NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
        | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
        |
        | Yepp all they do in VB6 is cast to the other interface. I
        | tried that first in C# and I got a System.NullRefe rence
        | exception too. I dunno what's going wrong, it is a
        | mystery wrapped in an RCW... Here's their VB6 code:
        |
        | Dim pObj As IPersistentObje ct
        | Dim sysObj As ISysObject
        |
        | Set pObj = sess.newObject( "dm_documen t")
        | ...
        | Set sysObj = pObj
        | ...
        | sysObj.setObjec tName docName
        |
        | --Richard
        |
        |

        Comment

        • Mattias Sjögren

          #5
          Re: Per Request: VB6 sample code

          Richard,
          [color=blue]
          >I tried that first in C# and I got a System.NullRefe rence
          >exception too.[/color]

          There are a number of possible reasons that can cause a
          NullReferenceEx ception. 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

          Please reply only to the newsgroup.

          Comment

          Working...