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
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();
}
string str=SDKDAL.demo.GetText();
运行时报无法在 DLL“SDKDAL.dll”中找到名为“GetText”的入口点,帮忙解决这个问题。
去掉[DllImport("SDKDAL.dll")]
public static extern string GetText();
}
去掉那个声明。c#写的dll。你在使用的时候,项目右键,添加引用,选择那个dll。就可以按我写的调用了。不用那行声明了。那个声明是调用系统的。
你意思就是说c#的DLL不可以被c#调用,哪别的语言怎么可以调用呢?
1.项目右键,添加引用,选择你c#写的dll
2.直接通过命名空间.类名.方法,就可以用了
一锅浆糊啊你。别的语言,所有的windows下的dll,exe都要符合pe文件格式,才可以被系统调用。但是.net托管程序的dll和exe,不是标准的pe结构,而是启动后,执行了.net clr。所以不能用标准的pe可执行文件的调用方式。方法不同而已。你怎么理解的,谁说不能用了?
非托管程序调用c#的dll方法详见google : c++ 调用 c# dll
这是说dllimport的功能,不是说如何引用。
感谢你的回答,刚刚接触dllImport这方面东西,知识了解还不全面,所以问了这么多。