2013-02-28 53 views
1

我想從C#調用方法「Talk」。我瀏覽了其他相關帖子,但它沒有幫助我。無法在DLL'<DLL_name>'中找到名爲'<function>'的入口點

Managed.Program.cs

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

using System.Runtime.InteropServices; 

namespace Managed 
{ 
    class Program 
    { 
     [DllImport("Unmanaged.exe", CallingConvention=CallingConvention.Cdecl,EntryPoint="Talk",CharSet=CharSet.Ansi)] 
     public static extern int Talk(); 
     static void Main(string[] args) 
     { 
      int value=Talk(); 
     } 
    } 
} 

Unmanaged.h

#ifndef UNMANAGED_H 
#define UNMANAGED_H 
extern "C" 
{ 
__declspec(dllexport) int Talk(); 
} 
#endif 

Unmanaged.cpp

#include "stdafx.h" 
#include "conio.h" 
#include "Unmanaged.h" 

int Talk() 
{ 
    int x=10,y=5; 
    return (x+y); 
} 
+1

您是否將您的非託管DLL複製到與C#可執行文件相同的文件夾中? [編輯]等等,你正在調用EXE?你不應該那樣做。您需要創建一個DLL。 – 2013-02-28 12:24:41

+0

轉到您的客戶端應用程序 - >添加 - >現有項目 - >到達您的DLL生成路徑 - >選擇DLL - >添加爲鏈接,你就完成了 – 2015-01-20 07:51:58

回答

2

您需要將庫部署爲DLLDllImport只適用於使用P/Ivoke的.dll庫。

在VS中創建DLL時,請在Win32下選擇控制檯應用程序,並將單選按鈕設置爲「動態鏈接庫(DLL)」。

然後照你所做的那樣做。有關信息,請參閱here

相關問題