Thanks
// ------------------------------ (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...
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!
Please use specific newsgroup:
microsoft.public.dotnet.framework.interop
for more help...
"Valery Polyakov" <valery....@writeme.com> wrote in message news:#JfayA8NBHA.1692@tkmsftngp04...
But, in my case this throws a InvalidCastException during runtime. Do you know how to avoid it?
Valery
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 );
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...