consuming managed C# DLL from unmanaged VB6

T

Tremendo

Hi,

I created a DLL using managed C# and this DLL needs to be consumed by two
applications:
- One is written in managed C#, and will
run on a PC that does have the .NET framework installed (no problems regarding this one).
- The other one is written in unmanaged Visual Basic 6, and will run on a PC that does NOT have the
..NET framework installed (my problems start here).

I wonder..
1) Is it required to install the .NET framework on the second PC, to consume
that DLL?
2) If the answer to 1) is yes, is there a way to create the DLL (or another version of the DLL), so
that I don't need to install the .NET framework on the second PC?
3) How do I have to export, in the DLL, the following managed C# function,
so that it can be consumed by the unmanaged VB6 application?

public bool Function(int i, double d,out string s)


Thank you very much.
 
W

Willy Denoyette [MVP]

Tremendo said:
Hi,

I created a DLL using managed C# and this DLL needs to be consumed by two
applications:
- One is written in managed C#, and will
run on a PC that does have the .NET framework installed (no problems regarding this one).
- The other one is written in unmanaged Visual Basic 6, and will run on a PC that does NOT
have the
.NET framework installed (my problems start here).

I wonder..
1) Is it required to install the .NET framework on the second PC, to consume
that DLL?

Yes. You can't run C# (or any other managed language) code without the framework installed.
2) If the answer to 1) is yes, is there a way to create the DLL (or another version of the
DLL), so
that I don't need to install the .NET framework on the second PC?

No, unless you want to implement the DLL in unmanaged code.

3) How do I have to export, in the DLL, the following managed C# function,
so that it can be consumed by the unmanaged VB6 application?

public bool Function(int i, double d,out string s)

You don't have to export anything, you simply have to annotate the method so that it becomes
COM visible.
Please consult the MSDN docs, start with "Introduction to COM Interop" for details on how to
design/implement your classes to be used as COM server.

Willy.
 
N

NewBieDev

Take a look at the following site, I did call a C# DLL function from
PowerBuilder and gets a return value from the C# DLL.

http://www.codeproject.com/dotnet/n...=14076&exp=0&fr=26&select=1254697#xx1254697xx


"Tremendo" <[email protected]>
???????:[email protected]...
Hi,

I created a DLL using managed C# and this DLL needs to be consumed by two
applications:
- One is written in managed C#, and will
run on a PC that does have the .NET framework installed (no problems
regarding this one).
- The other one is written in unmanaged Visual Basic 6, and will run on a PC
that does NOT have the
..NET framework installed (my problems start here).

I wonder..
1) Is it required to install the .NET framework on the second PC, to consume
that DLL?
2) If the answer to 1) is yes, is there a way to create the DLL (or another
version of the DLL), so
that I don't need to install the .NET framework on the second PC?
3) How do I have to export, in the DLL, the following managed C# function,
so that it can be consumed by the unmanaged VB6 application?

public bool Function(int i, double d,out string s)


Thank you very much.
 
T

Tremendo

Yes. You can't run C# (or any other managed language) code without the framework installed.

Ok. If I install the .NET framework on the second PC, will I be able to call that managed DLL from
unmanaged VB6 or I need to change the VB itself?

IOW, installing the .NET framework seems to be necessary. Is it also sufficient?


Thank you again.
 
C

Christof Nordiek

Tremendo said:
Ok. If I install the .NET framework on the second PC, will I be able to
call that managed DLL from
unmanaged VB6 or I need to change the VB itself?

IOW, installing the .NET framework seems to be necessary. Is it also
sufficient?

you can export the .NET classes as COM objects. Look in the docs for COM
interop.
 
T

Tremendo

Hi,

Thanks to all of you for your directions. I read some documents about
COM interop, and I think I'm closer now to knowing what should I do.


From what I've understood, these are the steps I should follow:
[I'm using Visual Studio 2005 on the first PC.]

1) The managed class I want to expose should have attributes like the
ones shown in the code below.
2) The managed DLL should be built with the option "Make assembly
COM-visible". That creates xx.dll and xx.pdb. Forget about xx.pdb.
3) Run "tlbexp xx.dll". This creates xx.tlb.
4) Run "regasm xx.dll /regfile". This creates xx.reg.
5) Copy the files xx.dll, xx.tlb and xx.reg to the second PC (the one
with unmanaged VisualBasic 6).
6) Install the .NET framework 2.0 on the second PC.
7) Execute xx.reg on the second PC (to add entries to its registry).
8) With the IDE of VB6, add a reference to the COM object named "xx",
and consume it as needed.

Some questions I have:

a) Are the steps more or less right?
b) Which is a good directory in the second PC to copy to and keep the
files xx.dll and xx.tlb? (the xx.reg won't be needed after being
executed).
c) The registry entries know where the xx.tlb is, and the xx.tlb knows
where the xx.dll is. Is this right?
d) Can I choose GUIDs at random? (I've seen that VS2005 includes a
tool to generate them in several formats).


Again, thank you for your time.
Tremendo.


----------------------------------------------

#region Using directives

using System;
using System.Text;
using System.Runtime.InteropServices;

#endregion

namespace N_Test
{
[GuidAttribute("C8660246-1962-4efd-9DAE-D3DC7533EA8E")]

[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface _C_Test
{
[DispIdAttribute(1)]
int TestAdd(int a,int b);
}

[GuidAttribute("8D43EC40-BA2D-4f0e-AD9D-3756030BF59A")]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ProgIdAttribute("N_Test.C_Test")]
public class C_Test : _C_Test
{
public int
result;
public C_Test()
{
result=2;
}

public int TestAdd(int a,int b)
{
result=a+b;
return(result);
}
} // C_Test
} // N_Test
 

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