简体   繁体   中英

Unhandled Exception: System.EntryPointNotFoundException in DLL

This is below C++ DLL source file.

//SimpleInterest.CPP
#include <iostream>
using namespace std;
#include "CalSimpleInterest.h"

namespace simpleInt
{
    // total interest 
    double calculateInterest:: CalSimplInterest(double Principal, double Rate, double Time)
    {
        double interest = 0.0;
        interest = (Principal * Time * Rate) / 100;
        return interest;
    }
}

similary header file

//CalSimpleInterest.h
namespace simpleInt
{
    class calculateInterest
    {
        public:
        static __declspec(dllexport) double CalSimplInterest(double Principal, double Rate, double Time);
    };
}

I have compiled and created CalSimpleInterest.dll . Now I want to use CalSimplInterest() function in C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        // Set the library path
        const string dllFilePath =
        "C:\\Users\\ggirgup\\Documents\\Visual Studio 2012\\Projects\\CalSimpleInterest\\Debug\\CalSimpleInterest.dll";


        // This is the function we import from the C++ library.
        //[DllImport(dllFilePath)]
        [DllImport(dllFilePath, CallingConvention = CallingConvention.Cdecl)]
        public static extern double CalSimplInterest(double Principal, double Rate, double Time);

        [DllImport(dllFilePath, CallingConvention = CallingConvention.Cdecl)]
        public static extern double TotalPayback(double Principal, double Rate, double Time);

        static void Main(string[] args)
        {
            Console.WriteLine(
                "Call C++ function in C# ");

            // Call C++ which calls C#
            CalSimplInterest(1000,1,2);
           // TotalPayback(1000, 1, 2);
            // Stop the console until user's pressing Enter
            Console.ReadLine();
        }


    }
}

It is compiling successfully. But it is showing following Error at run time.

Unhandled Exception: System.EntryPointNotFoundException: Unable to find an entry  point named 'CalSimplInterest' in DLL 'C:\Users\ggirgup\Documents\Visual
Studio
 2012\Projects\CalSimpleInterest\Debug\CalSimpleInterest.dll'.
   at ConsoleApplication1.Program.CalSimplInterest(Double Principal, Double Rate , Double Time)
   at ConsoleApplication1.Program.Main(String[] args) in c:\Users\ggirgup\Docume nts\Visual Studio 2012\Projects\CsharpCallingCPPDLL\CsharpCallingCPPDLL\Program.
cs:line 46

As I am naive to C#, please help me to resolve this issue. Thanks in Advance.

i'm not sure but i wonder if you try to Export a class method. Just try to write your method in ac or c++ code file and Export it in the Header file. Then try it again. It's just a try...

Furthermore u can check the compiler options under C/C++ -> Advanced -> Calling Convention. Make sure the Option is __cdecl (/Gd). If it is __fastcall or __stdcall, WINAPI or whatever u have to use this calling convention or switch it to __cdecl (/Gd).

在此处输入图片说明

U can use Dumpbin as described or a tool like DependencyWalker/depends.exe with a graphical user Interface.

Dumpbin.exe /EXPORTS "c:\\user\\x\\code\\bestcodeever\\myDllThatExportsSomeSmartThings.dll" works for me...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM