Using a C# Class Library .dll in VB 6

J

Jay Douglas

Hello all,
I've wrote a .dll in c#. The .dll needs to be used by a VB 6
application. At first, I couldn't set a reference from the VB app, but then
I found the register for COM Interop in the build configuration in the
VS.net IDE. The VB 6 IDE now lists my class library as a reference. But
there are still a few issues. I can set a variable/object to the class.
i.e.:

Dim o As MyC#Namespace.MyC#Class (Note that the namespace/class doesn't have
C# in it)

When I try to access a public method, the VB6 Intellisence doesn't pull up
the list of Methods/Properties that are available with the Class Library.
Example:

variant foo = o.MyMethod <-- (No intellisence after o.)

The structure of my class library is as such:

namespace MyC#Namespace {

public class MyC#Class
{
public string MyMethod {
return "test";
}
}
}

Any suggestions are appreciated.
 
K

Kumar Gaurav Khanna [.NET MVP]

Hi!

Set the ComVisible attribute for the methods in question, or to have all the
methods become available to COM clients, set it for the class.
 
K

Kumar Gaurav Khanna [.NET MVP]

Hi!

Assumed that ComVisible was false. Also using AutoDual isn't advised
primarily because of versioning issues.

--

Regards,
Gaurav Khanna
----------------------------------------------------------------------------
----------------
Microsoft MVP - .NET, MCSE Windows 2000/NT4, MCP+I
WinToolZone - Spelunking Microsoft Technologies
http://www.wintoolzone.com/
Nicholas Paldino said:
Kumar,

All types in an assembly are accessible to COM by default.

The reason why the original poster is not seeing his methods is because
the ClassInterface attribute is not being applied. It should be applied to
his types, constructed with the value ClassInterfaceType.AutoDual. By
default, it is AutoDispatch (or considered that without the attribute), so
he could probably get to his methods, but through late binding.

To the original poster, it should be noted that this is a bad way to
expose your classes to COM. COM is an interface-based framework, and is
different from .NET in that .NET is class-based. Check out the section of
the .NET framework titled "Interoperating with Unmanaged Code", located at
(watch for line wrap):

http://msdn.microsoft.com/library/d...html/cpconinteroperatingwithunmanagedcode.asp

In particular, check out the section titled "Exposing .NET Framework
Components to COM".

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Kumar Gaurav Khanna said:
Hi!

Set the ComVisible attribute for the methods in question, or to have all the
methods become available to COM clients, set it for the class.

--

Regards,
Gaurav Khanna
--------------------------------------------------------------------------
--
----------------
Microsoft MVP - .NET, MCSE Windows 2000/NT4, MCP+I
WinToolZone - Spelunking Microsoft Technologies
http://www.wintoolzone.com/
but
then doesn't
have pull
up
 
J

Jay Douglas

Okay, this is what I've figured out. Intellsence works in VB and everything
seems to be working just fine. Tell me what you think.

using System;
using System.Runtime.InteropServices;

namespace Squarei
{
//DEFINE THE PUBLIC INTERFACE FOR THE COM OBJECT
public interface IComInteropTest
{
void SetHTML(string strHtml);
string GetHTML();
}


//IMPLEMENT THE CLASS BELOW USING THE PUBLIC INTERFACE DEFINED ABOVE
[ClassInterface(ClassInterfaceType.None)]
public class ComInteropTest : IComInteropTest
{

public string strUrl = string.Empty;
public string strHtml = string.Empty;

public void SetHTML(string strHtml)
{
this.strHtml = strHtml;
}

public string GetHTML()
{
return strHtml;
}
}
}
 

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