在C#项目中调用c ++ dll函数/ API

时间:2018-02-27 09:33:10

标签: c# c++

我是C#的新手,很难从C#项目调用C ++ API 我创建了文件“socket.dll”,它有C ++ API。以下是摘录

source.cpp + source.h-> socket.dll

 #include "Source.h"

 void PiblRequestCallback(PibMsgConf &msg, void *param)
 {
  cout << "In PibRequestCallback" << endl;
 }

 uint32_t GetPib() 
 {

  return 25;
 }

我从C#项目调用上面的API“GetPib”。以下是代码段:

(WindowsFormsAPP)

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.Runtime.InteropServices;

 namespace WindowsFormsApp1
 {
     public partial class Form1 : Form
     {
         [DllImport("socket.dll")]
         public static extern UInt32 GetPib();

         public Form1()
         {
             InitializeComponent();
         }

         private void GetPib_button_Click(object sender, EventArgs e)
         {
             pib_len_tbox.Text = GetPib().ToString();
         }
     }
 }

在运行C#代码段时,我收到错误

System.EntryPointNotFoundException: 'Unable to find an entry point named 
'GetPib' in DLL 'socket.dll'.'

要在socket.dll文件或C#项目中进行哪些更改才能使其正常工作。

1 个答案:

答案 0 :(得分:0)

导入外部功能:

     [DllImport("socket.dll", EntryPoint = "getpib")]
     public static extern UInt32 GetPib();

如果未找到入口点,则使用dumpbin查找入口点(使用visual studio命令提示符)

dumpbin /exports [dll path]