这是在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#很天真,请帮我解决这个问题。 在此先感谢。
答案 0 :(得分:0)
我不确定,但我想知道你是否尝试导出类方法。只是尝试在c或c ++代码文件中编写方法并将其导出到Header文件中。然后再试一次。这只是一次尝试...
此外,您可以检查C / C ++下的编译器选项 - &gt;高级 - &gt;召集公约。确保选项是__cdecl(/ Gd)。如果它是__fastcall或__stdcall,WINAPI或任何你必须使用此调用约定或将其切换到__cdecl(/ Gd)。
你可以按照描述使用Dumpbin,也可以使用带有图形用户界面的DependencyWalker / depends.exe等工具。
Dumpbin.exe / EXPORTS“c:\ user \ x \ code \ bestcodeever \ myDllThatExportsSomeSmartThings.dll”对我有用......