DLLs in C#, legacy code concerns

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

OK, I just want to make sure I am correct in the following statements, and
then to get some advice as to how I should proceed.

First, is it accurate to say that in order for C# to use a DLL, the classes
and members exposed must utilize a COM interface?

I tried adding a refernce to a DLL that was just created (in C++ to cross
compile between Microsoft Visual C++ 6.0 and Borland C++ Builder 5, providing
messaging functionality through ACE) to solve a communications problem
between Borland and Microsoft compilers, only to have Visual Studio .NET 2003
(version 7.1 I believe) tell me that I cannot add the DLL file because it is
neither a valid assembly, nor a COM object.

Is is possible to use this DLL in Visual Studio .NET 2003 if I write my user
interface and export routines in C++ instead of C#?

How would you suggest I gain access to the messaging functionality located
in this DLL for my application?

Thanks in advance for all the help,

Andrew S. Giles
 
Andrew,

Basically, if you want to interop with unmanaged code, you have two
choices. The first would be to expose it through a COM object, the second
is to export functions from a DLL. You could also create a managed wrapper
in C++ if you wish, and then set your reference to that.

Hope this helps.
 
ok, I am trying to get this to work, and wonder if I have messed something up.

The DLL contains a public class that I will call Fred.

So, I write the following in C#:
[DllImport("vpcmsgmfcd.dll")]
public static extern Fred classFred;

and I get an error asking what namespace am I supposed to be using.

I know I am doing something wrong, but I just do not know what. I am sorry
to be asking these annoyingly simple questions, but any help would be greatly
appreciated.

Andrew
 
Andrew,

If you want to export a class, then you have to wrap it in a manged
wrapper in C++, or you have to use COM interop. Using the DllImport
attribute works only for functions exported from the dll, not for classes.


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

Andrew S. Giles said:
ok, I am trying to get this to work, and wonder if I have messed something
up.

The DLL contains a public class that I will call Fred.

So, I write the following in C#:
[DllImport("vpcmsgmfcd.dll")]
public static extern Fred classFred;

and I get an error asking what namespace am I supposed to be using.

I know I am doing something wrong, but I just do not know what. I am
sorry
to be asking these annoyingly simple questions, but any help would be
greatly
appreciated.

Andrew

BenW said:
You can access non COM dlls lookup extern keyword and DllImport
attribute.
BenW
 
I can only export functions, but not classes, is this correct?

Andrew

Nicholas Paldino said:
Andrew,

Basically, if you want to interop with unmanaged code, you have two
choices. The first would be to expose it through a COM object, the second
is to export functions from a DLL. You could also create a managed wrapper
in C++ if you wish, and then set your reference to that.

Hope this helps.


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

Andrew S. Giles said:
OK, I just want to make sure I am correct in the following statements, and
then to get some advice as to how I should proceed.

First, is it accurate to say that in order for C# to use a DLL, the
classes
and members exposed must utilize a COM interface?

I tried adding a refernce to a DLL that was just created (in C++ to cross
compile between Microsoft Visual C++ 6.0 and Borland C++ Builder 5,
providing
messaging functionality through ACE) to solve a communications problem
between Borland and Microsoft compilers, only to have Visual Studio .NET
2003
(version 7.1 I believe) tell me that I cannot add the DLL file because it
is
neither a valid assembly, nor a COM object.

Is is possible to use this DLL in Visual Studio .NET 2003 if I write my
user
interface and export routines in C++ instead of C#?

How would you suggest I gain access to the messaging functionality located
in this DLL for my application?

Thanks in advance for all the help,

Andrew S. Giles
 
Andrew... Also, if you write it in ATL COM, all you do is add a reference to the dll and Visual Studio
creates the managed wrapper class for you.

http://www.geocities.com/jeff_louie/COM_Interop/atl_com.htm
How would you suggest I gain access to the messaging functionality located
in this DLL for my application?<

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
Back
Top