2013-06-19 82 views
1

這是低於C++ DLL源文件。未處理的異常:DLL中的System.EntryPointNotFoundException

//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#天真,請幫我解決這個問題。 在此先感謝。

+1

確實[這個問題](http://stackoverflow.com/questions/ 3515270 /入口點未找到異常)幫助你? – jszigeti

+0

它只是沒有你認爲它的名字。使用您的DLL上的dumpbin.exe/exports來查看真實姓名。 –

+0

我正在嘗試爲CalSimpleInterest.dll使用dumpbin.exe。但我看不到任何細節。那麼如何使用dumpbin.exe/exports? –

回答

0

我不知道,但我不知道你是否嘗試導出類方法。只要嘗試在c或C++代碼文件中編寫您的方法並將其導出到頭文件中即可。然後再試一次。這只是一個嘗試...

此外,你可以檢查C/C++ - >高級 - >調用約定下的編譯器選項。確保選項是__cdecl(/ Gd)。如果它是__fastcall或__stdcall,WINAPI或任何你必須使用這個調用約定或切換到__cdecl(/ Gd)。

enter image description here

U可以使用DUMPBIN如所描述的或與圖形用戶接口等DependencyWalker /的Depends.exe的工具。

DUMPBIN.EXE /出口 「C:\用戶\ X \代碼\ bestcodeever \ myDllThatExportsSomeSmartThings.dll」 爲我的作品...