bitscuit
bitscuit

Reputation: 1042

How to add C++ dll project to Visual Basic references?

I am using Visual Studio 2015 Community Edition and I followed Microsoft's tutorial on creating implicit dll's here https://msdn.microsoft.com/en-us/library/ms235636.aspx and I referenced the dll successfully in a C++ console application (I did it just to see if it would work).

When I tried adding the dll project to the references for a Visual Basic Windows Form Application, I got an error saying "A reference to 'DLL Project Name' could not be added." After some research, I think it's because VB targets the .NET framework while the C++ dll targets Windows, but that's all I managed to figure out. I would greatly appreciate any help on reconciling this, or setting up some solution that involves a C++ dll and a GUI project that uses the dll (I just chose VB for the GUI since it's really quick and easy to set up).

Note that both the DLL project and the Visual Basic project are in the same solution.

Upvotes: 1

Views: 1920

Answers (2)

Mike Pettigrew
Mike Pettigrew

Reputation: 164

A C++ DLL should be added to your *.NET application with a post-build event "xcopy" command like this one:

xcopy "$(SolutionDir)DLL\$(ConfigurationName)"\*.dll "$(TargetDir)"*.* /Y

from your selected project go to Project-->Properties-->Compile-->Build Events-->Post-build event command line

Enter the appropriate copy command.

In the above example, several C++ DLLs were collected in a "DLL" folder in the Solutions Directory. They all get copied to the application folder every time the application is built. There were DEBUG and RELEASE versions of those DLLs in separate sub-folders.

As other posters noted, you also must have correct p-invoke declarations for your C++ DLLs in your *.NET code.

Upvotes: 0

Joseph Rosson
Joseph Rosson

Reputation: 354

Your tutorial won't help you invoke the code from .NET and you are correct in assuming it to be a .NET framework inter-op. issue. You can use p-invoke to import and call the DLL or create a COM wrapper around your DLL and register it in the GAC then reference it via a COM CreateObject call.

Other possibilities are to convert it to a C++/CLI .NET C++ DLL.

References:

P-Invoke

COM

-UPDATE-

I suppose I should also mention that if you target Universal Windows Platform (UWP), it also provides a clean binding across .NET and C++ and hides some of the busy COM wire-up.

Upvotes: 1

Related Questions