Using a DLL in an application

G

Geoff Jones

Hi

I have a DLL written in C++/MFC which has functions in it. I would like to
use it within a VB.net application. However, when I try to add a reference
to it, I'm told that "it isn't a valid assembly or COM component. Only
assemblies with extension 'dll' and COM components can be referrenced".

Can anybody tell me why it isn't working? It does have the extension 'dll'
so I guess it is the wrong "assembly" - whatever that means!!!

Thanks in advance

Geoff
 
P

Patrick Philippot

Geoff said:
I have a DLL written in C++/MFC which has functions in it. I would
like to use it within a VB.net application. However, when I try to
add a reference to it, I'm told that "it isn't a valid assembly or
COM component. Only assemblies with extension 'dll' and COM
components can be referrenced".
Can anybody tell me why it isn't working? It does have the extension
'dll' so I guess it is the wrong "assembly" - whatever that means!!!

Geoff,

A .Net assembly contains metadata describing the types contained in the
DLL or in the EXE. A DLL compiled in VC++/MFC contains no .Net metadata,
no MSIL, no managed code. Nothing that .Net can directly understand and
use. It belongs to a different world. As the message says, you can only
use .Net or COM (via Interop) DLLs directly from .Net code. When you try
to reference a DLL, .Net tries to find the metadata or it tries to
create metadata from the typelib associated with the COM component.

If you want to use classes implemented in your MFC DLL, you can create
a COM wrapper around your C++ classes (preferably in Managed C++) and
use / reference that COM component via Interop.

On the other hand, if your DLL directly exports "standard" functions
that can otherwise be used from any other language like VB6, you can do
the same in VB .Net and call these functions like any other Win32 API by
using the DllImport attribute. Just, do not reference that DLL.
 
G

Geoff Jones

Hi Patrick

Many thanks for your response.

Could you direct me to some examples on how to "wrap" around the C++ class?

Geoff
 
P

Patrick Philippot

Geoff said:
Could you direct me to some examples on how to "wrap" around the C++
class?

Geoff,

I suggest that you first try to transform your existing MFC DLL into an
Automation server. Adding Automation to an existing project is not that
easy in VS6, though. I already had to do that and I found easier to
start a new project, specify Automation support in the App Wizard and
then copy and paste code from the existing project into the new project.
If you want to modify the existing project directly, you'll find
instructions there: http://www.codeproject.com/com/mfc_autom.asp . Once
your DLL is an Automation server, using it from .Net will be easy. Just
reference it from the COM tab of the Add Reference dialog.

If the Automation approach is not possible, you'll have to use P/Invoke
directly. These links should help:

http://www.codeguru.com/Cpp/Cpp/cpp_managed/interop/article.php/c6867/
http://www.ondotnet.com/pub/a/dotnet/2003/06/23/mcinterop.html

On your disk, look at:

C++ class (CTestClass) in DLL
<SDK
PATH>\FrameworkSDK\Samples\Technologies\Interop\PlatformInvoke\Custom\LIB\PinvokeLib.cpp
Visual Basic .Net code to access
<SDK
PATH>\FrameworkSDK\Samples\Technologies\Interop\PlatformInvoke\Custom\VB\ClassMethods.cs

This thread is related to the above sample:
http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#aa4f3fc8124dadc5

You can also read this:

<VS NET PATH>\VC7\managedextensionsspec.doc
<VS NET PATH>\VC7\migration_guide.doc

Hope this helps.
 
G

Geoff Jones

Many thanks Patrick

Geoff

Patrick Philippot said:
Geoff,

I suggest that you first try to transform your existing MFC DLL into an
Automation server. Adding Automation to an existing project is not that
easy in VS6, though. I already had to do that and I found easier to start
a new project, specify Automation support in the App Wizard and then copy
and paste code from the existing project into the new project. If you want
to modify the existing project directly, you'll find instructions there:
http://www.codeproject.com/com/mfc_autom.asp . Once your DLL is an
Automation server, using it from .Net will be easy. Just reference it from
the COM tab of the Add Reference dialog.

If the Automation approach is not possible, you'll have to use P/Invoke
directly. These links should help:

http://www.codeguru.com/Cpp/Cpp/cpp_managed/interop/article.php/c6867/
http://www.ondotnet.com/pub/a/dotnet/2003/06/23/mcinterop.html

On your disk, look at:

C++ class (CTestClass) in DLL
<SDK
PATH>\FrameworkSDK\Samples\Technologies\Interop\PlatformInvoke\Custom\LIB\PinvokeLib.cpp
Visual Basic .Net code to access
<SDK
PATH>\FrameworkSDK\Samples\Technologies\Interop\PlatformInvoke\Custom\VB\ClassMethods.cs

This thread is related to the above sample:
http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#aa4f3fc8124dadc5

You can also read this:

<VS NET PATH>\VC7\managedextensionsspec.doc
<VS NET PATH>\VC7\migration_guide.doc

Hope this helps.
 
G

Geoff Jones

Hi Patrick

Sorry to bother you again but you give me some further help?

I've re-compiled the DLL using "automation" and added it to the .Net
project. This DLL has a class defined within it. How can I create an
instance of the class from within the VB.net application?

Thanks in advance

Geoff
 

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

Similar Threads


Top