Search code examples
c++dllimportdeclspec

Having trouble importing function from C++ dll error LNK 2019


I am trying to write and test a dll file in C++ that I can call whenever I want filesystem level access to things. I am currently having a huge headache when trying to access the methods in this dll in C++. Strangely enough, I was able to call the code in a separate C# program with little trouble, but I want to get an understanding of how dll interaction works in C++.

And this is the .cpp for my dummy executable that should only call my "newMain" test method.

// dummy.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
//#pragma comment(lib,"visa32.lib")
#pragma message("automatic link to adsInterface.dll")
#pragma message(lib, "adsInterface.lib"

extern "C" int __stdcall newMain();

int _tmain(int argc, _TCHAR* argv[])
{
 newMain();
 std::string i;
 std::cin >> i
 return 0;
}

The problem is, when I run it I get this error:

error LNK2019: unresolved external symbol _newMain@0 referenced in function _wmain
error LNK1120: 1 unresolved externals

Here is the .h for adsInterface:

// adsInterface.h

#ifndef ADSINTERFACE_H
#define ADSINTERFACE_H

/* //save this for later i have no clue how this really works.
#ifdef ADSAPI_EXPORTS
#define ADSAPI __declspec(dllexport)
#else
#define ADSAPI __declspec(dllexport)
#endif
*/

namespace ADSInterface
{
  //test method. should print to console.
  __declspec(dllexport) int __stdcall newMain();

  void hello();
}
#endif

and here is my .cpp for adsInterface:

// adsInterface.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "adsInterface.h"
#include <iostream>

namespace ADSInterface
{
  /* this is where the actual internal class and other methods will go */

  void hello()
  {
    std::cout << "hello from the DLL!" << std::endl;
  }


  __declspec(dllexport) int __stdcall newMain()
  {
    hello();
    return 0;
  }
}

I'll also include the .def file i used when compiling the dll:

; adsInterface.def - defines exports for adsInterface.dll

LIBRARY ADSINTERFACE
;DESCRIPTION 'A C++ dll that allows viewing/editing of alternate data streams'

EXPORTS
  newMain @1

Strangely enough, I was able to import the method in C# with this line (I did not have to include a .lib file either):

[DllImport("./adsInterface.dll")] private static extern void newMain();

And it ran when I called it normally:

newMain();

I've been reading many different guides on how to import dll functions, and I've reached the point where I think I'm just mangling together different ways of importation between the languages and just making a mess of things. If anyone is able to provide some insight on how I should be importing dll methods in C++ that would be much appreciated.


Solution

  • delete this declaration:

    extern "C" int __stdcall newMain();
    

    and call ADSInterface::newMain() from _tmain.

    In the posted code you did not define anything matching that declaration, did you?

    Alternatively make the implementation calling the other one, or drag the one from namespace to global.