首页 新闻 会员 周边

C#调用C++编写的DLL报错

0
悬赏园豆:30 [已解决问题] 解决于 2011-08-19 13:17

无法在 DLL“D:\jk\DynamicLibrary\Debug\MathFuncsDll”中找到名为“helloworld”的入口点。 
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.EntryPointNotFoundException: 无法在 DLL“D:\jk\DynamicLibrary\Debug\MathFuncsDll”中找到名为“helloworld”的入口点。

源错误: 

行 17: protected void Page_Load(object sender, EventArgs e)
行 18: {
行 19: string s = helloworld();
行 20: Response.Write("<script>alert('"+s+"')</script>");
行 21: }

报的上面得错误
我的代码如下:

首先在.h文件里写如下代码定义方法:
// MathFuncsDll.h
#include <string> 


namespace MathFuncs
{
  class MyMathFuncs
  {
  public:
static __declspec(dllexport) std::string helloworld();
  // Returns a + b
  static __declspec(dllexport) double Add(double a, double b);

  // Returns a - b
  static __declspec(dllexport) double Subtract(double a, double b);

  // Returns a * b
  static __declspec(dllexport) double Multiply(double a, double b);

  // Returns a / b
  // Throws DivideByZeroException if b is 0
  static __declspec(dllexport) double Divide(double a, double b);
  };
}

在.cpp文件里写如下代码实现方法:
// MathFuncsDll.cpp

#include "MathFuncsDll.h"

#include <stdexcept>
#include <string>
using namespace std;

namespace MathFuncs
{

std::string MyMathFuncs::helloworld()
{
return "helloworld";
}
  double MyMathFuncs::Add(double a, double b)
  {
  return a + b;
  }

  double MyMathFuncs::Subtract(double a, double b)
  {
  return a - b;
  }

  double MyMathFuncs::Multiply(double a, double b)
  {
  return a * b;
  }

  double MyMathFuncs::Divide(double a, double b)
  {
  if (b == 0)
  {
  throw new invalid_argument("b cannot be zero!");
  }

  return a / b;
  }
}

然后在web页面里写下面的代码调用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;

namespace MyWebDll
{
  public partial class _Default : System.Web.UI.Page
  {
  [DllImport(@"D:\jk\DynamicLibrary\Debug\MathFuncsDll")]
  public static extern string helloworld();


  protected void Page_Load(object sender, EventArgs e)
  {
  string s = helloworld();
  Response.Write("<script>alert('"+s+"')</script>");
  }
  }
}

然后就报上面的错误。请高手指点。谢谢!
没写过c++的,第一次用,有些东西表达不清楚还请见谅,谢谢啦!

蒋坤的主页 蒋坤 | 初学一级 | 园豆:152
提问于:2011-08-11 13:57
< >
分享
最佳答案
0

哦!根据错误信息

System.EntryPointNotFoundException: 无法在 DLL“D:\jk\DynamicLibrary\Debug\MathFuncsDll”中找到名为“helloworld”的入口点。

判定为你的C++的方法没有静态公布出来!导致C#J静态调用不到!

可以在baidu搜一下!c++如何公布自己写的方法

收获园豆:10
JasNature | 菜鸟二级 |园豆:451 | 2011-08-11 21:53
http://www.cnblogs.com/chengxingliang/archive/2011/03/09/1978288.html 我写的Dll有问题。但是我不知道哪里有问题,关键咱不是做C++的,会调用就好了,谢谢你们,还有楼下的两位好人。
蒋坤 | 园豆:152 (初学一级) | 2011-08-19 13:19
其他回答(2)
0

添加一个def文件,在里将helloworld 公布出来

例:

helloworld @ 1

收获园豆:10
一滴血 | 园豆:1602 (小虾三级) | 2011-08-12 10:28
0

你可以搜下怎么把一个类作为接口。depends这个你可以看下你封装的DLL是否有问题。

收获园豆:10
静幽独白 | 园豆:145 (初学一级) | 2011-08-16 09:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册