Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Examples of using Marshal.GetTypedObjectForIUnknown()?

849 views
Skip to first unread message

Valery Polyakov

unread,
Sep 7, 2001, 11:53:29 AM9/7/01
to
Hi, does anyone has examples or code snippets showing
usage of GetTypedObjectForIUnknown() to get a COM object
of a desired type form its IUnknown pointer?

Thanks

NETMaster

unread,
Sep 7, 2001, 12:40:58 PM9/7/01
to

Here it is:
- Let's create an object by known ProgID,
- query it's IUnknown,
- clone this IU back to the object by using GetTypedObjectForIUnknown,
- and free/Release all

// ------------------------------ (c) NETMaster ;-) ------------
// activate object of any COM-server via ProgID:
Type srvtype = Type.GetTypeFromProgID( "customize here: Vendor.ProgID" );
object actobj = Activator.CreateInstance( srvtype );

// query the IUnknown-pointer for this object (does internal AddRef!)
IntPtr iuptr = Marshal.GetIUnknownForObject( actobj );

// now clone this IUnknown-pointer back to the object..
object sameobj = Marshal.GetTypedObjectForIUnknown( iuptr, srvtype );

// reverse the GetIUnknownForObject
Marshal.Release( iuptr );
iuptr = IntPtr.Zero;

// free the clone
sameobj = null;

// free the origin
Marshal.ReleaseComObject( actobj );
actobj = null;
// -------------------------------------------

Ok?

"Valery Polyakov" <valery....@writeme.com> wrote in message news:840301c137b5$3cca8c80$19ef2ecf@tkmsftngxa01...

Valery Polyakov

unread,
Sep 7, 2001, 1:11:54 PM9/7/01
to
Thanks for your reply.

Now a question is: can you instead of

object sameobj = Marshal.GetTypedObjectForIUnknown( iuptr, srvtype );

have that "sameobj" to be of a desired type, corresponding to srvtype, noting that this type was already imported
through tlbimp and is known in the application namespace.

something like:

myCOMtype sameobj = Marshal.GetTypedObjectForIUnknown( iuptr, srvtype );

this would allow be to get the advantages of early binding in the application.

Thanks again


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

NETMaster

unread,
Sep 7, 2001, 1:28:57 PM9/7/01
to
sure, use typeof() and casting:
myCOMtype sameobj = (myCOMtype) Marshal.GetTypedObjectForIUnknown( iuptr, typeof( myCOMtype) );

Please use specific newsgroup:
microsoft.public.dotnet.framework.interop
for more help...

"Valery Polyakov" <valery....@writeme.com> wrote in message news:#JfayA8NBHA.1692@tkmsftngp04...

Valery Polyakov

unread,
Sep 7, 2001, 2:27:33 PM9/7/01
to

Thanks,

But, in my case this throws a InvalidCastException during runtime. Do you know how to avoid it?

Valery

NETMaster

unread,
Sep 7, 2001, 4:28:09 PM9/7/01
to
Ah, I see...
If you do TLB-import/reference a component, you have a
'managed wrapper class type',
and not a plain 'COM object'...

try this two steps :

// convert IUnknown-pointer to temporary COM-object
object iuobj = Marshal.GetObjectForIUnknown( iuptr );

// convert COM-object to wrapper-object
myCOMtype wrpobj = (myCOMtype) Marshal.CreateWrapperOfType( iuobj, typeof( myCOMtype ));

// free temporary COM-object
iuobj = null;


note, it depends on how you got the IUnknown-pointer and
if the types are compatible...


"Valery Polyakov" <valery....@writeme.com> wrote in message news:Otc7Dr8NBHA.1880@tkmsftngp05...


>
> myCOMtype sameobj = Marshal.GetTypedObjectForIUnknown( iuptr, srvtype );

Alexander Shaburov

unread,
Sep 26, 2001, 7:42:25 PM9/26/01
to
Hi, NETmaster.

You know as run COM-server, but how can I release Word or OUTLOOK from
memory in this VB6 code?

All 'nothing' don't work in this code.

Thank you for advise. Alex

=============VB6

Sub Main()

Dim an As New NetTest7.IClassMain

Dim wa As New Word.Application

an.Init_Class2 wa

Set an = Nothing

Set wa = Nothing

End Sub

============VC#

using System;

using System.Windows.Forms;

using System.Runtime.InteropServices; // For COMException


namespace NetTest7

{

public interface IClassMain

{

void Init_Class2(ref Word.Application wa);

}

public class ClassMain : IClassMain

{

public ClassMain()

{

MessageBox.Show("Create");

}

public void Init_Class2(ref Word.Application wa)

{

Word.Application wa1;

wa1 = (Word.Application )Marshal.CreateWrapperOfType( wa ,
typeof(Word.Application ) );


}

}

}

"NETMaster" <spam.ne...@swissonline.ch> wrote in message
news:e2u3fu9NBHA.1432@tkmsftngp03...

0 new messages