0

I wish to call C++ function (here Score()) which is present in Score_Update1.dll. Both C# & C++ files get compiled successfully. I have also put above dll into the Debug/bin of C# project. But when I run C# code it gives EntryPointNotFoundException. What could be the reason behind this Exception? I tried dependency walker for Score_Update1.dll. But it doesn't show any Entry Point

I wish to use PInvoke technique for calling C++ function from C#

 // Score_Update1.h #pragma once #include <iostream> using namespace std; using namespace System; extern "C"{ #define MYAPI __declspec(dllexport) namespace Score_Update1 { public class MYAPI UpdateScore { // TODO: Add your methods for this class here. public: void Score(); }; } } // This is the main Score_Updat1.dll DLL file. #include "stdafx.h" #include "Score_Update1.h" using namespace Score_Update1; void UpdateScore::Score() { cout<<"Score has been updated"; } 

C# code is as follows:

 using Score_Update1; using System.Runtime.InteropServices; namespace GameTesting { class Game { [DllImport("Score_Update1.dll")] internal extern static void Score(); static void Main(string[] args) { try { Game.Score(); } catch (Exception ex) { Console.WriteLine(ex); } } } } 
2
  • probably you have to set calling convention: [DllImport("Score_Update1.dll", CallingConvention=CallingConvention.Cdecl)] Commented Jan 15, 2015 at 6:24
  • @Maher That member function uses __thiscall Commented Jan 15, 2015 at 8:44

1 Answer 1

0

The reason for EntryPointNotFoundException is that the DLL does not contain an entry point named Score. If you look at the exported names using dumpbin or some similar tool you will see mangled names.

However, using the mangled name isn't going to help you here. You've exported a class and the function you want to call is a member function. You cannot directly instantiate a C++ class from pinvoke. And you cannot call member functions. If you wish to use pinvoke you would need to flatten the class to a C style interface. Another route would be to compile the C++ code to a mixed mode C++/CLI assembly and consume that.

Sign up to request clarification or add additional context in comments.

2 Comments

How to make C++ class as C style interface? Can I access functions by doing it..?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.