0

Using Visual Studio 2013, I am currently trying to generate a .dll with C++ Code, which i want to include into a VB.NET project. To create the .dll i tried to follow these tutorials:

For now my .dll Project contains only the following files:

  • External Dependencies (auto generated by VS2013)
  • Header Files: stdafx.h, targetver.h (auto generated)
  • dllmain.cpp with default entry point Method DllMain and stdafx.cpp (auto generated)
  • MyDLL.cpp and containing the implementation of my methods which should be exported (for now one void method without parameters)
  • Headerfile MyDLL.h
  • Source.def (i only added the .def file after i tried using the __declspec(dllexport) statement)

MyDll.h contains:

#ifndef _DLL_MYDLL_H_
#define _DLL_MYDLL_H_
#include <iostream>
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

extern "C"
{
DECLDIR void MyMethod();
}
#endif

MyDLL.cpp contains:

#include "stdafx.h"
#include "MyDLL.h"

#define DLL_EXPORT

extern "C"
{
void MyMethod(){ 
//my code 
}

When i create the .dll (by using "build > build solution" in VS2013) it compiles without errors and warnings. However, when i try to set a reference in my VB-Project by using "project > add reference" and selecting the .dll that was created in the DEBUG-Folder of my DLL-Project, i get an error stating that the reference could not be added and that i should make sure that the file is accessible and that it is a valid assembly or COM component.

Am i missing some vital settings in my dll/vb project here? Thanks in advance for your advice.

H W
  • 2,556
  • 3
  • 21
  • 45
  • You can only add a managed DLL as a reference, you have a native C++ dll. Perhaps you meant to use C++/CLI? – Roger Rowland Aug 27 '14 at 13:18
  • I am new to the .NET framework, so i am not quite sure what the ideal approach would be. I assumed that i need to use P/Invoke later on to handle the problems with the unmanaged code, but i don't even get to this point when i can't reference my .dll. Is there another way than using the "add reference" functionality of VS2013 to access the methods of my .dll? If there isn't (or if this approach bad practice) - how do i convert my project to C++/CLI? – H W Aug 27 '14 at 13:39
  • 1
    possible duplicate of [using a class defined in a c++ dll in c# code](http://stackoverflow.com/questions/315051/using-a-class-defined-in-a-c-dll-in-c-sharp-code) – IdeaHat Aug 27 '14 at 14:08
  • Remember that the method for doing this in VB.NET is (essentially) the same as any .NET language. And C# is MUCH more common (http://langpop.com/, though I think they glom together VB and VB.NET) when googling things. So if you can't find it for VB.NET, look for C#. – IdeaHat Aug 27 '14 at 14:12
  • http://msdn.microsoft.com/en-us/library/ms235282.aspx – IdeaHat Aug 27 '14 at 14:13
  • After copying the .dll to the executable directory of my project, i could access the .dll by using Private Declare Sub MyMethod Lib "MyDLL.dll" (). Thanks a lot for your advice! – H W Aug 29 '14 at 06:21

0 Answers0