COM object (no type library) - how do I access the interface from VB.NET?

R

Robin Tucker

I have a COM object, which implements straightforwardly the IDataObject
interface. It seems this isn't the same IDataObject interface we now have
with .NET. Basically, the COM object implements low level interfaces. It
does not have a type library. We use it freely with our unmanaged C++
projects. What I would like to do is load or create one of these objects in
VB.NET, but I cannot seem to do it.

I've tried:

Dim theObject As Object

theObject = GetObject ( "c:\gas.tgw" ) * gas.tgw is a file-type we
associated with the COM object

but how do I get the IDataObject interface from theObject?

Is it possible to use basic low-level COM interfaces with VB.NET?

Any answers would be much appreciated.
 
D

David Browne

Robin Tucker said:
I have a COM object, which implements straightforwardly the IDataObject
interface. It seems this isn't the same IDataObject interface we now have
with .NET. Basically, the COM object implements low level interfaces. It
does not have a type library. We use it freely with our unmanaged C++
projects. What I would like to do is load or create one of these objects in
VB.NET, but I cannot seem to do it.

I've tried:

Dim theObject As Object

theObject = GetObject ( "c:\gas.tgw" ) * gas.tgw is a file-type we
associated with the COM object

but how do I get the IDataObject interface from theObject?

Is it possible to use basic low-level COM interfaces with VB.NET?

Sure, you can use the COM interfaces but you need a type library. Then you
need to generate an Interop Assembly.

The type library may be embedded in the dll, or you could create it if you
have the MSIL interface definitions.

David
 
R

Robin Tucker

So I definately need a type library? There is no way we can generate one
for this program - it implements IDataObject in COM but it isn't an
automation object. I would have thought it would just be a case of finding
the interface using "FindInterfaces". But obviously not. In this case,
VB.NET can't be properly supporting com I guess, if it only supports objects
with type libs.
 
D

David Browne

Robin Tucker said:
So I definately need a type library? There is no way we can generate one
for this program - it implements IDataObject in COM but it isn't an
automation object. I would have thought it would just be a case of finding
the interface using "FindInterfaces". But obviously not. In this case,
VB.NET can't be properly supporting com I guess, if it only supports objects
with type libs.

How do the unmanaged C++ programs use this component?

Do they have the interface definitions?
Do they use IDispach?

David
 
R

Robin Tucker

Something like this - we use CoGetObject - this is part of a method that
gets data from the object and converts it into a DIB for rendering. As you
can see, I may have my work cut-out in VB.NET! (but vb might get ditched if
I cannot do things like this).


// Connect to the object.
IDataObjectPtr image;
TryMethod(CoGetObject(display_name, 0, IID_IDataObject, (void**)&image));
// First try to get a DIB.
FORMATETC fe = { CF_DIB, 0, DVASPECT_CONTENT, index, TYMED_HGLOBAL };
if (SUCCEEDED(image->GetData(&fe, &sm)))
{
 
R

Robin Tucker

We implement the following interfaces on the object:

IClientSecurity
IDataObject
IDispatch
IMarshal
IMultiQI
IOleContainer
IOleInPlaceActiveObject
IOleInPlaceObject
IOleItemContainer
IOleObject
IOleWindow
IParseDisplayName
IPersist
IPersistFile
IPersistStorage
IProxyManager
IUnknown
 
F

Fergus Cooney

Hi Robin,

Have you tried hauling in your component by adding a Reference to your
project? When I tried that (a long time ago) it said something like "There's
no blah, blah, would you like me to create an Interop wrapper?". Yes please
and off you go. It may not be clever enough to map all your functions but then
again, it may.

Regards,
Fergus
MVP [Windows Start button, Shutdown dialogue]
 
R

Robin Tucker

No chance! It says it isn't a valid COM component or assembly, which, in a
sense, I guess is true. The component won't show up in the COM tab because
it doesn't have a typelib and it isn't an automation object. My guess is it
just isn't possible and I will either have to create a C++ source file that
will do it for me and somehow links into VB.NET or I will ditch VB.NET and
go back to plain old C++, ATL etc.
 
F

Fergus Cooney

Hi Robin,

Shame about that. I'd go the C++ wrapper and then get a .NET wrapper. And
then you can write some wrapper classes around it in VB.NET just so it feels
really warm and cosy. :)

Go back?? You can't go back. Or maybe I'm just biased.

Best wishes,
Fergus
MVP [Windows Start button, Shutdown dialogue]
 
R

Robin Tucker

What I don't understand about this situation is the files will happily embed
in Word documents (Insert->Object) - so I must be doing something wrong!

I don't want to go back to C++ - luckily I'm just experimenting with VB.NET
at the start of a project which, after much deliberation, we decided would
be best written in VB.NET. Of course, provided our existing COM components
were compatible. Designing a nice looking UI is a pain in the ass with ATL.
I am going to jump out of the office window if I have to do that.
 
D

David Browne

Robin Tucker said:
Something like this - we use CoGetObject - this is part of a method that
gets data from the object and converts it into a DIB for rendering. As you
can see, I may have my work cut-out in VB.NET! (but vb might get ditched if
I cannot do things like this).


// Connect to the object.
IDataObjectPtr image;
TryMethod(CoGetObject(display_name, 0, IID_IDataObject, (void**)&image));
// First try to get a DIB.
FORMATETC fe = { CF_DIB, 0, DVASPECT_CONTENT, index, TYMED_HGLOBAL };
if (SUCCEEDED(image->GetData(&fe, &sm)))
{
Great!!

You should have what you need.

In your C++ program source folder you should have either the .tlb or an .idl
file.

If you have an .idl file, just run midl.exe on it and it will output the
type library (as well as some .c stubs which you don't need). Then in
dotnet just add a reference to the .tlb file.

David
 
M

Matt Evans [MSFT]

A few things to try:

midl
tlbimp
tlbexp (i think)

failing that, a fantastic way to make managed wrappers for non managed code
(COM or otherwise) is by using Managed C++
If you're proficient with C++ managed C++ will let you perfectly tune how
you expose your existing C/C++ code to the managed world. You can write
the remainder of your app in VB.NET...




--------------------
From: "Robin Tucker" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vb
Subject: Re: COM object (no type library) - how do I access the interface from VB.NET?
Date: Thu, 18 Sep 2003 22:10:13 +0100
Lines: 29
Message-ID: <[email protected]>
References: <[email protected]>
<[email protected]>
NNTP-Posting-Host: 80.176.255.158
X-Trace: news.demon.co.uk 1063919309 20922 80.176.255.158 (18 Sep 2003 21:08:29 GMT)
X-Complaints-To: (e-mail address removed)
NNTP-Posting-Date: Thu, 18 Sep 2003 21:08:29 +0000 (UTC)
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
X-Priority: 3
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MSMail-Priority: Normal
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!kibo.news.demon.net!news.demon.co.uk!demon!not-for-mail
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:139063
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

No chance! It says it isn't a valid COM component or assembly, which, in a
sense, I guess is true. The component won't show up in the COM tab because
it doesn't have a typelib and it isn't an automation object. My guess is it
just isn't possible and I will either have to create a C++ source file that
will do it for me and somehow links into VB.NET or I will ditch VB.NET and
go back to plain old C++, ATL etc.



Fergus Cooney said:
Hi Robin,

Have you tried hauling in your component by adding a Reference to your
project? When I tried that (a long time ago) it said something like "There's
no blah, blah, would you like me to create an Interop wrapper?". Yes please
and off you go. It may not be clever enough to map all your functions but then
again, it may.

Regards,
Fergus
MVP [Windows Start button, Shutdown dialogue]

Matt Evans
--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top