[英]System.EntryPointNotFoundException and DllImport(“kernel32.dll”)
[英]Unhandled Exception: System.EntryPointNotFoundException in DLL
這是在C ++ DLL源文件下面。
//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;
}
}
相似頭文件
//CalSimpleInterest.h
namespace simpleInt
{
class calculateInterest
{
public:
static __declspec(dllexport) double CalSimplInterest(double Principal, double Rate, double Time);
};
}
我已經編譯並創建了CalSimpleInterest.dll。 現在,我想在C#中使用CalSimplInterest()函數。
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();
}
}
}
它編譯成功。 但是它在運行時顯示以下錯誤。
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
由於我不熟悉C#,請幫助我解決此問題。 提前致謝。
我不確定,但我想知道您是否嘗試導出類方法。 只需嘗試在ac或c ++代碼文件中編寫您的方法,然后將其導出到Header文件中即可。 然后再試一次。 只是嘗試...
此外,您可以在C / C ++->高級->調用約定下檢查編譯器選項。 確保選項是__cdecl(/ Gd)。 如果是__fastcall或__stdcall,則WINAPI或您必須使用此調用約定或將其切換到__cdecl(/ Gd)的任何對象。
U可以使用描述的Dumpbin或帶有圖形用戶界面的工具DependencyWalker / depends.exe。
Dumpbin.exe / EXPORTS“ c:\\ user \\ x \\ code \\ bestcodeever \\ myDllThatExportsSomeSmartThings.dll”對我有用...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.