Calling a IUnknown derived COM object from VB.NET (framework 1.1 )

R

rep_movsd

Hi

I have a C++ com object that exposes IStream and IPropertyBag, this is
derived from IUnknown, there is no typelib or IDispatch etc

I need to write a simple VB.NET app in VB 2003 that can create the COM
object given the CLSID, get IStream and IPropertybag from that object
then set a couple of BSTR properties, and use that IStream to read.

Can someone give me the simplest code equivalent in VB.NET to the
following in c++ (simplified for clarity)?


// {77D4C560-6A25-44af-8DA5-44FDF9B2A154}
DEFINE_GUID(CLSID_XorEncryptor, 0x77d4c560, 0x6a25, 0x44af, 0x8d,
0xa5, 0x44, 0xfd, 0xf9, 0xb2, 0xa1, 0x54);

CoInitialize(0);
CComPtr<IUnknown> pCrypt;
pCrypt.CoCreateInstance(CLSID_XorEncryptor);
CComQIPtr<IPropertyBag> pProps(pCrypt);
CComQIPtr<IStream> pCryptStream(pCrypt);

pProps->Write(...);
pProps->Write(...);

pCryptStream->Seek(...);
pCryptStream->Read(...);


Thanks in advance
Vivek
 
M

Mattias Sjögren

Can someone give me the simplest code equivalent in VB.NET to the
following in c++ (simplified for clarity)?


// {77D4C560-6A25-44af-8DA5-44FDF9B2A154}
DEFINE_GUID(CLSID_XorEncryptor, 0x77d4c560, 0x6a25, 0x44af, 0x8d,
0xa5, 0x44, 0xfd, 0xf9, 0xb2, 0xa1, 0x54);

CoInitialize(0);
CComPtr<IUnknown> pCrypt;
pCrypt.CoCreateInstance(CLSID_XorEncryptor);
CComQIPtr<IPropertyBag> pProps(pCrypt);
CComQIPtr<IStream> pCryptStream(pCrypt);

pProps->Write(...);
pProps->Write(...);

pCryptStream->Seek(...);
pCryptStream->Read(...);


Dim CLSID_XorEncryptor As New
Guid("77D4C560-6A25-44af-8DA5-44FDF9B2A154")
Dim crypt As Object =
Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_XorEncryptor))
Dim props As IPropertyBag = CType(crypt, IPropertyBag)
Dim cryptStream As UCOMIStream = CType(crypt, UCOMIStream)

props.Write(...)
cryptStream.Seek(...)
cryptStream.Read(...)

IStream is defined (as UCOMIStream) in the
System.Runtime.InteropServices namespace. You can find IPropertyBag
defined here (in C#, hopefully you can translate)
http://www.pinvoke.net/default.aspx/Interfaces/IPropertyBag.html


Mattias
 
R

rep_movsd

Forgive me for being VB.NET dense ...

I got as far as

<ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), _
Guid("55272A00-42CB-11CE-8135-00AA004BB851")> _
Interface IPropertyBag
Sub Read(<MarshalAs(UnmanagedType.LPWStr)> ByVal lpstrText As
String, ByRef pVar As Object, <MarshalAs(UnmanagedType.IUnknown)>
ByVal pErrLog As Object)
Sub Write(<MarshalAs(UnmanagedType.LPWStr)> ByVal lpstrText As
String, ByRef pVar As Object)
End Interface

.....

Dim CLSID_XorEncryptor As New
Guid("77D4C560-6A25-44af-8DA5-44FDF9B2A154")
Dim crypt As Object =
Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_XorEncryptor))
Dim props As IPropertyBag = CType(crypt, IPropertyBag)
Dim cryptStream As UCOMIStream = CType(crypt, UCOMIStream)

I need to do the following :

Set property "FILENAME" to a file name ( BSTR )
Set property "KEY" to a key (BSTR )

Call Read on pStream repeatedly and write it to a file till its
done...

I just dont have time and inclination to figure out the syntax
and .NET runtime functions myself.

Can someone just write these few lines of code for me?

Vivek
 
Top