Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
0 down vote favorite


I have following Code in C#: (I used R.Giesecke Dllexport Template to create a unmanaged Dll with C#... it creates me a ".lib" and ".dll" file with (managed) C# )
C#
//UnmanagedExports.cs
using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;

namespace AddDll
{
   class MyAddDll
   {
      [DllExport("Add", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
      public static double Add(double a, double b)
      {
         return a + b;
      }
   }
}

The C# Dll Code runs well without errors and creats me a AddDll.lib and AddDll.dll file.

I would like to import this Dll in C++:
C++
//CallAdd.h

#pragma once

#define DllImport __declspec(dllimport) 

namespace AddDll
{
    class MyAddDll
    {
    public:
        static DllImport double Add(double a, double b);
    };
}

C++
// CallAdd.cpp

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



using namespace std;

int main()
{
    double a = 4.5;
    double b = 5.5;

    cout << "a + b = " << AddDll::MyAddDll::Add(a, b) << endl;

    system("pause");

    return 0;
}

I've got following errors:

Warning 1 Platform is AnyCpu, creating binaries for each CPU platform in a separate subfolder...<br />
<br />
    Error 2 error LNK2019: unresolved external symbol "__declspec(dllimport) public: static double __cdecl AddDll::MyAddDll::Add(double,double)" (__imp_?Add@MyAddDll@AddDll@@SANNN@Z) referenced in function _main<br />
<br />
    Error 3 error LNK1120: 1 unresolved externals


In Visual Studio I did following under my C++ Console Application Properties:

- In Common Properties ‐> Framework and References. Add AddDll Project reference.
- In Configuration Properties ‐> Linker ‐> Input. Additional Dependencies AddDll.lib
- In Configuration Properties ‐> Linker ‐> General. Additional Library Directories, add
directory where AddDll.lib and AddDll.dll are located

Can anyone help me to eliminate the errors?

Thx in advance!!
Posted
Updated 19-Jun-22 4:21am
Comments
Daniele Rota Nodari 23-Jul-14 2:44am    
Hi there.

Have you resolved this issue?

you need to build the C#-dll for the same CPU as your C++ project. :-O
 
Share this answer
 
Comments
Member 10854414 1-Jul-14 9:07am    
Thx! The warning is away. But the errors are still there :(
 
Share this answer
 
Comments
Member 10854414 1-Jul-14 12:39pm    
Hey Pravveen,
the warning is away.
You could also just compile your C# code as normal (.NET) code, and compile your C++ code as C++/CLI. That can use both .NET and unmanaged code in the same files. If you need separation between .NET and the unmanaged C++ code, you could also write a wrapper DLL in C++/CLI to interface between the 2.
 
Share this answer
 
Comments
Member 10854414 1-Jul-14 12:39pm    
Hey David,
thx for your answer. The problem is, I cannot find an existing wrapper for arrays. I need a template to do this...
Hi.

The error you reported states the linker is looking for the function export with __cdecl calling convention but you exported from C# as StdCall.

Have you tried either changing the CallingConvention within your C# code or adding __stdcall keyword within the C import instruction?

Please see CallingConvention Enumeration[^], Linker Tools Error LNK2019[^] and __stdcall[^].

Regards,
Daniele.
 
Share this answer
 
Hello,

This question is 2 years old, but the answer is never old. I am answering the question with absolute correct answer. Other developers may benefit from my answer. It was posted three time because the web site stated it failed to post 2 times by mistake.

The problem relies on how you import the function in your native C++ code. You can not do the way you are doing now. If you use the following C++ code to import your function, the the name of the export function must be mangled, you must come up with exact mangled name as Visual C++ does. I have removed my article link, the article is well-written though.

C++
//CallAdd.h

#pragma once

#define DllImport __declspec(dllimport) 

namespace AddDll
{
    class MyAddDll
    {
    public:
        static DllImport double Add(double a, double b);
    };
}


Insead of using namespace and class, you should simply use extern "C" statement like the one shown below.

C++
extern "C"
{
    double Add(double a, double b);
}


Since the default calling convention in C++ is __cdecl, you will also need to change the calling convention in the C# code, remember that name of a __stdcall of a export function needs to be mangled as well, in this case, it is Add@16, you don't want to do mangled name, so, please just use __cdecl, it is much simpler.

C#
//UnmanagedExports.cs
using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;
 
namespace AddDll
{
   class MyAddDll
   {
      [DllExport("Add", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
      public static double Add(double a, double b)
      {
         return a + b;
      }
   }
}
 
Share this answer
 
There must be a match between the configuration of the managed code and the DLL, so you should create different versions of the DLL for Release/x86 (32 bit), Debug/x86, Release/x86 and Release/x64
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900