COM interop problems 
Author Message
 COM interop problems

Hi,

I've just started learning dotnet (c#, framework, visual studio.net, ...)
and I wanted to know if it was easy to use a existing COM server from C#
like it is in VB6. It seems not. Indeed my first simple app already generate
a System.InvalidCastException exception. Here the code under visual
studio.net beta1:

namespace ConsoleApplication1
{
    using System;

    /// <summary>
    ///    Summary description for Class1.
    /// </summary>
    public class Class1
    {
        public Class1()
        {
        }

        public static int Main(string[] args)
        {
           SQLDMO.Application dmoApp = new SQLDMO.Application();
           SQLDMO.NameList dmoList = dmoApp.ListAvailableSQLServers(); -->
this line throw a System.InvalidCastException exception

            return 0;
        }
    }

Quote:
}

PS :  I've added a reference to the SQLDMO COM server using the solution
explorer (right click reference --> add reference --> COM ...)

Any reason why that exception is thrown ? I've made the smae test in VB.Net
an the exception is still there (the code works with VB6).

Thanks,
Gaetano.



Mon, 21 Jul 2003 20:01:55 GMT  
 COM interop problems

says...

Quote:
> Hi,

> I've just started learning dotnet (c#, framework, visual studio.net, ...)
> and I wanted to know if it was easy to use a existing COM server from C#
> like it is in VB6. It seems not.

Use the tlbimp utility to create a .NET assembly that acts as a "wrapper"
to interface to your COM object.  Then add a reference to the assembly in
your C# project.

--
Patrick Steele

Lead Software Architect



Mon, 21 Jul 2003 22:04:05 GMT  
 COM interop problems
OK, I will try that solution. But I was thinking that VisualStudio.Net was
doing that job for me "on the fly".


Quote:

> says...
> > Hi,

> > I've just started learning dotnet (c#, framework, visual studio.net,
...)
> > and I wanted to know if it was easy to use a existing COM server from C#
> > like it is in VB6. It seems not.

> Use the tlbimp utility to create a .NET assembly that acts as a "wrapper"
> to interface to your COM object.  Then add a reference to the assembly in
> your C# project.

> --
> Patrick Steele

> Lead Software Architect



Mon, 21 Jul 2003 22:07:29 GMT  
 COM interop problems

says...

Quote:
> OK, I will try that solution. But I was thinking that VisualStudio.Net was
> doing that job for me "on the fly".

Perhaps in the future -- I don't know.  It would be kind of handy if you
could add a reference to a COM object and it would automatically run
tlbimp and generate the assembly (assuming it doesn't exist, of course).

--
Patrick Steele

Lead Software Architect



Mon, 21 Jul 2003 22:29:31 GMT  
 COM interop problems


Quote:
> OK, I will try that solution. But I was thinking that VisualStudio.Net was
> doing that job for me "on the fly".

It does.

Just go into Project | Add Reference, select the COM tab and add a reference
to a COM component. VS.NET will automatically generate the wrapper for you
behind the scenes.

I have done this several times with VB.NET - never tried it with C#.

--
Rockford Lhotka



Mon, 21 Jul 2003 22:41:42 GMT  
 COM interop problems
It does, if you add a reference to a COM object a NET assembly will be
generated and put in your /bin/debug or /bin/release folder.

Willy.



Quote:
> OK, I will try that solution. But I was thinking that VisualStudio.Net was
> doing that job for me "on the fly".




> > says...
> > > Hi,

> > > I've just started learning dotnet (c#, framework, visual studio.net,
> ...)
> > > and I wanted to know if it was easy to use a existing COM server from
C#
> > > like it is in VB6. It seems not.

> > Use the tlbimp utility to create a .NET assembly that acts as a
"wrapper"
> > to interface to your COM object.  Then add a reference to the assembly
in
> > your C# project.

> > --
> > Patrick Steele

> > Lead Software Architect



Mon, 21 Jul 2003 22:48:36 GMT  
 COM interop problems
I've tried the tlbimp solution and it doesn't work (the same exception is
generated).
And I think also VisualStudio.Net (at least the beta 1) does the tlbimp job
on the fly. Just right-click the reference main item in the tree control (of
the solution explorer), select add reference in the menu and then click on
the COM tab. There, you will find a list of all COM servers you have on your
machine. If you select one of these COM server, VisualStudio will generate
the tlbimp output on the fly in the project bin\debug (or release)
directory. You can also verify that by right-clicking the reference added to
the solution, it is pointing to another file (the tlbimp outpur I guess)
than the COM server DLL.

Anyway, I'm still looking for an explanation on the exception.

Regards,
Gaetano.


Quote:

> says...
> > OK, I will try that solution. But I was thinking that VisualStudio.Net
was
> > doing that job for me "on the fly".

> Perhaps in the future -- I don't know.  It would be kind of handy if you
> could add a reference to a COM object and it would automatically run
> tlbimp and generate the assembly (assuming it doesn't exist, of course).

> --
> Patrick Steele

> Lead Software Architect



Mon, 21 Jul 2003 22:53:00 GMT  
 COM interop problems
I think dmoApp.ListAvailableSQLServers(); returns a Interface pointer, so
you shoukd declare dmoList as the interface (wathever the name is), not the
wrapped class.

Willy.



Quote:
> Hi,

> I've just started learning dotnet (c#, framework, visual studio.net, ...)
> and I wanted to know if it was easy to use a existing COM server from C#
> like it is in VB6. It seems not. Indeed my first simple app already
generate
> a System.InvalidCastException exception. Here the code under visual
> studio.net beta1:

> namespace ConsoleApplication1
> {
>     using System;

>     /// <summary>
>     ///    Summary description for Class1.
>     /// </summary>
>     public class Class1
>     {
>         public Class1()
>         {
>         }

>         public static int Main(string[] args)
>         {
>            SQLDMO.Application dmoApp = new SQLDMO.Application();
>            SQLDMO.NameList dmoList = dmoApp.ListAvailableSQLServers(); -->
> this line throw a System.InvalidCastException exception

>             return 0;
>         }
>     }
> }

> PS :  I've added a reference to the SQLDMO COM server using the solution
> explorer (right click reference --> add reference --> COM ...)

> Any reason why that exception is thrown ? I've made the smae test in
VB.Net
> an the exception is still there (the code works with VB6).

> Thanks,
> Gaetano.



Tue, 22 Jul 2003 04:12:26 GMT  
 COM interop problems
Is it possible the COM server you are using exports type information?  You
may have said which object you are using but it did not show up in my
reader.  As far as I can tell, unless there is a type library, VS.NET
doesn't make the RCW (Runtime Callable Wrapper).  I've used it successfully
with many older objects, just to test it.  But when I used it on custom
interfaces, it didn't work too well.

The DirectX Sample they give you shows how you have to write your own RCW
when the IDE can't figure out the type info for you.  I guess DirectX is one
of those cases.  They had a great demo on the PDC CDs as well.

--
Chris Gomez



Quote:
> I've tried the tlbimp solution and it doesn't work (the same exception is
> generated).
> And I think also VisualStudio.Net (at least the beta 1) does the tlbimp
job
> on the fly. Just right-click the reference main item in the tree control
(of
> the solution explorer), select add reference in the menu and then click on
> the COM tab. There, you will find a list of all COM servers you have on
your
> machine. If you select one of these COM server, VisualStudio will generate
> the tlbimp output on the fly in the project bin\debug (or release)
> directory. You can also verify that by right-clicking the reference added
to
> the solution, it is pointing to another file (the tlbimp outpur I guess)
> than the COM server DLL.

> Anyway, I'm still looking for an explanation on the exception.

> Regards,
> Gaetano.




> > says...
> > > OK, I will try that solution. But I was thinking that VisualStudio.Net
> was
> > > doing that job for me "on the fly".

> > Perhaps in the future -- I don't know.  It would be kind of handy if you
> > could add a reference to a COM object and it would automatically run
> > tlbimp and generate the assembly (assuming it doesn't exist, of course).

> > --
> > Patrick Steele

> > Lead Software Architect



Wed, 23 Jul 2003 02:54:57 GMT  
 COM interop problems
OK.

But how do you explain then that I can do (same) early-binding with VB6 and
the same SQLDMO com server ?


Quote:
> Is it possible the COM server you are using exports type information?  You
> may have said which object you are using but it did not show up in my
> reader.  As far as I can tell, unless there is a type library, VS.NET
> doesn't make the RCW (Runtime Callable Wrapper).  I've used it
successfully
> with many older objects, just to test it.  But when I used it on custom
> interfaces, it didn't work too well.

> The DirectX Sample they give you shows how you have to write your own RCW
> when the IDE can't figure out the type info for you.  I guess DirectX is
one
> of those cases.  They had a great demo on the PDC CDs as well.

> --
> Chris Gomez



> > I've tried the tlbimp solution and it doesn't work (the same exception
is
> > generated).
> > And I think also VisualStudio.Net (at least the beta 1) does the tlbimp
> job
> > on the fly. Just right-click the reference main item in the tree control
> (of
> > the solution explorer), select add reference in the menu and then click
on
> > the COM tab. There, you will find a list of all COM servers you have on
> your
> > machine. If you select one of these COM server, VisualStudio will
generate
> > the tlbimp output on the fly in the project bin\debug (or release)
> > directory. You can also verify that by right-clicking the reference
added
> to
> > the solution, it is pointing to another file (the tlbimp outpur I guess)
> > than the COM server DLL.

> > Anyway, I'm still looking for an explanation on the exception.

> > Regards,
> > Gaetano.




> > > says...
> > > > OK, I will try that solution. But I was thinking that
VisualStudio.Net
> > was
> > > > doing that job for me "on the fly".

> > > Perhaps in the future -- I don't know.  It would be kind of handy if
you
> > > could add a reference to a COM object and it would automatically run
> > > tlbimp and generate the assembly (assuming it doesn't exist, of
course).

> > > --
> > > Patrick Steele

> > > Lead Software Architect



Mon, 28 Jul 2003 22:20:19 GMT  
 COM interop problems
OK.

But how do you explain then that I can do (same) early-binding with VB6 and
the same SQLDMO com server ?


Quote:
> Is it possible the COM server you are using exports type information?  You
> may have said which object you are using but it did not show up in my
> reader.  As far as I can tell, unless there is a type library, VS.NET
> doesn't make the RCW (Runtime Callable Wrapper).  I've used it
successfully
> with many older objects, just to test it.  But when I used it on custom
> interfaces, it didn't work too well.

> The DirectX Sample they give you shows how you have to write your own RCW
> when the IDE can't figure out the type info for you.  I guess DirectX is
one
> of those cases.  They had a great demo on the PDC CDs as well.

> --
> Chris Gomez



> > I've tried the tlbimp solution and it doesn't work (the same exception
is
> > generated).
> > And I think also VisualStudio.Net (at least the beta 1) does the tlbimp
> job
> > on the fly. Just right-click the reference main item in the tree control
> (of
> > the solution explorer), select add reference in the menu and then click
on
> > the COM tab. There, you will find a list of all COM servers you have on
> your
> > machine. If you select one of these COM server, VisualStudio will
generate
> > the tlbimp output on the fly in the project bin\debug (or release)
> > directory. You can also verify that by right-clicking the reference
added
> to
> > the solution, it is pointing to another file (the tlbimp outpur I guess)
> > than the COM server DLL.

> > Anyway, I'm still looking for an explanation on the exception.

> > Regards,
> > Gaetano.




> > > says...
> > > > OK, I will try that solution. But I was thinking that
VisualStudio.Net
> > was
> > > > doing that job for me "on the fly".

> > > Perhaps in the future -- I don't know.  It would be kind of handy if
you
> > > could add a reference to a COM object and it would automatically run
> > > tlbimp and generate the assembly (assuming it doesn't exist, of
course).

> > > --
> > > Patrick Steele

> > > Lead Software Architect



Mon, 28 Jul 2003 22:20:19 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Com interop problem with Framework 1.1

2. Com/Interop problem

3. COM Interop problem

4. COM Interop problem

5. COM Interop problem

6. Problem passing VB COM+ ASP Request object to .NET component System.Web.HTTPRequest through COM interop

7. COM interop or vbscript problem

8. COM interop or VBScript problem

9. Automation error referencing .net dll from com (com interop)

10. COM Interop question

11. Strong named COM Interop

12. ParamArray and COM Interop

 

 
Powered by phpBB® Forum Software