关于c#调用c#DLL问题

tantpourelleyifu 2010-05-16 11:52:58
c#写的DLL 2  using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6  7 namespace SDKDAL 8 { 9     public static class demo10     {11         public static string GetText() 12         {13             return "测试";14         }15     }16 }17 18 19 下面是在asp.net调用(c#语言)20 using System;21 using System.Configuration;22 using System.Data;23 using System.Linq;24 using System.Web;25 using System.Web.Security;26 using System.Web.UI;27 using System.Web.UI.HtmlControls;28 using System.Web.UI.WebControls;29 using System.Web.UI.WebControls.WebParts;30 using System.Xml.Linq;31 using System.Runtime.InteropServices;32 33 public partial class _Default : System.Web.UI.Page 34 {35     protected void Page_Load(object sender, EventArgs e)36     {37         if(!IsPostBack)38         {39           string str=GetText();  //调用自己写的就报错,运行时报无法在 DLL“SDKDAL.dll”中找到名为“GetText”的入口点。40 41 42         //调用系统的可以43             string myString="请选择!";44             MessageBoxTEST(0, myString, "是否取消呢?", 3);45 46         }47 48     }49 50     [DllImport("User32.dll", EntryPoint = "MessageBox")]51     public static extern int MessageBoxTEST(int h, string m, string c, int type);52 53 54     [DllImport("SDKDAL.dll")]55     public static extern string GetText();56 }57 58 
...全文
160 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
tantpourelleyifu 2010-05-17
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wuyazhe 的回复:]
汗,你怎么理解的啊。我什么时候说不能调用了。反反复复说了多少次了。
1.项目右键,添加引用,选择你c#写的dll
2.直接通过命名空间.类名.方法,就可以用了


一锅浆糊啊你。

别的语言,所有的windows下的dll,exe都要符合pe文件格式,才可以被系统调用。但是.net托管程序的dll和exe,不是标准的pe结构,而是启动后,执行了.net clr。所以不能用标准的pe可……
[/Quote]

感谢你的回答,刚刚接触dllImport这方面东西,知识了解还不全面,所以问了这么多。
兔子-顾问 2010-05-17
  • 打赏
  • 举报
回复
算了。不解释了。你仔细看看我的回复吧。

dllImport是导入一个标准的pe文件格式的dll。你c#写的dll不是pe文件格式的。所以无法这样用。除非你用c++写extern "C"的函数。


这是说dllimport的功能,不是说如何引用。
兔子-顾问 2010-05-17
  • 打赏
  • 举报
回复
汗,你怎么理解的啊。我什么时候说不能调用了。反反复复说了多少次了。
1.项目右键,添加引用,选择你c#写的dll
2.直接通过命名空间.类名.方法,就可以用了


一锅浆糊啊你。

别的语言,所有的windows下的dll,exe都要符合pe文件格式,才可以被系统调用。但是.net托管程序的dll和exe,不是标准的pe结构,而是启动后,执行了.net clr。所以不能用标准的pe可执行文件的调用方式。方法不同而已。你怎么理解的,谁说不能用了?


非托管程序调用c#的dll方法详见google : c++ 调用 c# dll
tantpourelleyifu 2010-05-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wuyazhe 的回复:]
这个本来就不是这样写的。dllImport是导入一个标准的pe文件格式的dll。你c#写的dll不是pe文件格式的。所以无法这样用。除非你用c++写extern "C"的函数。
[/Quote]

你意思就是说c#的DLL不可以被c#调用,哪别的语言怎么可以调用呢?
兔子-顾问 2010-05-17
  • 打赏
  • 举报
回复
这个本来就不是这样写的。dllImport是导入一个标准的pe文件格式的dll。你c#写的dll不是pe文件格式的。所以无法这样用。除非你用c++写extern "C"的函数。
weihao20090930 2010-05-17
  • 打赏
  • 举报
回复
过来学习一下,呵呵
tantpourelleyifu 2010-05-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 noway8881 的回复:]
引用 5 楼 tantpourelleyifu 的回复:

引用 4 楼 wuyazhe 的回复:
帮你调整下格式而已。

汗,c#调用c#的,不用这么麻烦,直接项目添加引用。直接写
string str=SDKDAL.demo.GetText();


运行时报无法在 DLL“SDKDAL.dll”中找到名为“GetText”的入口点,帮忙解决这个问题。


去掉[Dll……
[/Quote]
去掉当然没问题了,现在就要用[DllImport("SDKDAL.dll")]调用。
兔子-顾问 2010-05-17
  • 打赏
  • 举报
回复
恩。对。楼上补充的对。
去掉那个声明。c#写的dll。你在使用的时候,项目右键,添加引用,选择那个dll。就可以按我写的调用了。不用那行声明了。那个声明是调用系统的。
noway8881 2010-05-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 tantpourelleyifu 的回复:]

引用 4 楼 wuyazhe 的回复:
帮你调整下格式而已。

汗,c#调用c#的,不用这么麻烦,直接项目添加引用。直接写
string str=SDKDAL.demo.GetText();


运行时报无法在 DLL“SDKDAL.dll”中找到名为“GetText”的入口点,帮忙解决这个问题。
[/Quote]

去掉[DllImport("SDKDAL.dll")]
public static extern string GetText();
}
tantpourelleyifu 2010-05-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyazhe 的回复:]
帮你调整下格式而已。

汗,c#调用c#的,不用这么麻烦,直接项目添加引用。直接写
string str=SDKDAL.demo.GetText();
[/Quote]

运行时报无法在 DLL“SDKDAL.dll”中找到名为“GetText”的入口点,帮忙解决这个问题。

兔子-顾问 2010-05-17
  • 打赏
  • 举报
回复
帮你调整下格式而已。

汗,c#调用c#的,不用这么麻烦,直接项目添加引用。直接写
string str=SDKDAL.demo.GetText();
tantpourelleyifu 2010-05-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 noway8881 的回复:]
在asp.net中调用MessageBox?
[/Quote]可以的
noway8881 2010-05-16
  • 打赏
  • 举报
回复
在asp.net中调用MessageBox?
兔子-顾问 2010-05-16
  • 打赏
  • 举报
回复

c#写的DLL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SDKDAL
{
public static class demo
{
public static string GetText()
{
return "测试";
}
}
}


下面是在asp.net调用(c#语言)
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Runtime.InteropServices;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string str=GetText(); //调用自己写的就报错,运行时报无法在 DLL“SDKDAL.dll”中找到名为“GetText”的入口点。


//调用系统的可以
string myString="请选择!";
MessageBoxTEST(0, myString, "是否取消呢?", 3);

}

}

[DllImport("User32.dll", EntryPoint = "MessageBox")]
public static extern int MessageBoxTEST(int h, string m, string c, int type);


[DllImport("SDKDAL.dll")]
public static extern string GetText();
}

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧