请 问 如 何 用 C# 创 建 一 个 包 含 窗 体 的 DLL

sunyt 2003-05-26 12:09:44
using System;
using System.Windows.Forms;

namespace ClassLibrary1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
public Class1()
{
//
Forms myForm1=new Forms();
Forms.ShowDialog();

//
}
}
}
我建的是一个C#类库的工程 using system 下没有windows这个引用

本人为C#初学者 请各位高手指教

...全文
413 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
PoorAngel 2003-05-28
  • 打赏
  • 举报
回复
借此地问一个问题!
我打开VC# 项目,然后是ASP.NET Web应用程序,新建了一个项目,我现在想调用
System.Windows.Forms.MouseEventArgs e;
不可以应用;
错误是:c:\inetpub\wwwroot\Web_dataGrid\WebForm1.aspx.cs(23): 类型或命名空间名称“Windows”在类或命名空间“System”中不存在(是否缺少程序集引用?)
请问怎么办?
qimini 2003-05-28
  • 打赏
  • 举报
回复
自己写控件
sunyt 2003-05-28
  • 打赏
  • 举报
回复
另外请问 如何将FORM加入到tabPage中? C#是如何传递窗口句柄的?
sunyt 2003-05-27
  • 打赏
  • 举报
回复
JensiaTsang(桶哥) 现在的问题是如何在C#中调用这个DLL
JensiaTsang 2003-05-27
  • 打赏
  • 举报
回复
1 先建立一個EXE,做好Form,調試好.
2 再建立一個DLL,然后加進上一步的Form.
3 編譯得到一個帶Form的DLL
Gao2003 2003-05-27
  • 打赏
  • 举报
回复
用工具把这个.NET的DLL转换为标准的COM组件应该可以
sunyt 2003-05-27
  • 打赏
  • 举报
回复
cocosoft(pengyun) 我的调DLL的程序报的是找不到ShowDlg的入口点 不是找不到主入口点
是否在类库工程中要设置什么接口吗? 就象C里面的.DEF文件和_stdcall什么的
甴曱 2003-05-27
  • 打赏
  • 举报
回复
你本来写的就是类库,而不是主程序。它当然是没有主入口点的。
你可以再建一个项目,使项目包含Main()这一个函数。在这个函数中调用你写的这个类。就OK了
sunyt 2003-05-27
  • 打赏
  • 举报
回复
JensiaTsang(桶哥) 你说的方法可以用 但是我不明白的是我上面用的DLLIMPORT方法为什么总说找不到函数入口点呢?
JensiaTsang 2003-05-27
  • 打赏
  • 举报
回复
1 建立一個dll工程,加入form,編譯.產生dll檔;(會吧?!)
2 建立一個exe工程,在refrence里,右擊,Add refrences,然后找到上一步的那個dll,加進來
3 在exe工程里:
(1) using ClassLibrary1;//第一步中的dll檔名
(2) 在exe中的form里放一個button:事件如下.
private void button1_Click(object sender, System.EventArgs e)
{
Form1 aform=new Form1();
aform.ShowDialog();
}
4 不就show出來了嗎?! 參考前几個大俠說的.
sunyt 2003-05-27
  • 打赏
  • 举报
回复
高手帮忙!!!
sunyt 2003-05-27
  • 打赏
  • 举报
回复
JensiaTsang(桶哥) 我就是在IDE里用的啊
特别说明我的DLL也是用的C#的类库工程做的 代码如下:
public class Class1: System.Windows.Forms.Form
{
public Class1()
{
}
public void ShowDlg()
{
Form myForm1=new Form();
myForm1.ShowDialog();
}

}
不知道代码还缺什么 调用的时候总说找不到入口点ShowDlg
JensiaTsang 2003-05-27
  • 打赏
  • 举报
回复
using Project1;//project1為DLL檔名稱
加上上一句試試!
大俠,我看吶,你還是在IDE里調用吧,那樣比較簡單點,然后熟悉了,再來NotePad里調用.
sunyt 2003-05-27
  • 打赏
  • 举报
回复
using System;
using System.Runtime.InteropServices;
namespace csharp_win
{
public class Form1: System.Windows.Forms.Form
{
[DllImport("ClassLibrary1")]
static extern void ShowDlg();

static void Main()
{
int hmod=LoadLibrary("ClassLibrary1");
int funcaddr=GetProcAddress(hmod, "ShowDlg");
ShowDlg();
FreeLibrary(hmod);
}
}

}

为什么报如下错误
F:\MS.NET.TEST\csharp_win\Form1.cs(118): 名称“LoadLibrary”在类或命名空间“csharp_win.Form1”中不存在
F:\MS.NET.TEST\csharp_win\Form1.cs(119): 名称“GetProcAddress”在类或命名空间“csharp_win.Form1”中不存在
F:\MS.NET.TEST\csharp_win\Form1.cs(123): 名称“FreeLibrary”在类或命名空间“csharp_win.Form1”中不存在

还需要什么引用吗?

qimini 2003-05-26
  • 打赏
  • 举报
回复
你直接添加对System.Windows.Forms.dll引用,再在你的代码里添加
using System.Windows.Forms;语句。
TheAres 2003-05-26
  • 打赏
  • 举报
回复
引用,右键,添加引用,把System.Windows.Form.dll添加进来。
sunyt 2003-05-26
  • 打赏
  • 举报
回复
这个是DLL的代码
public class Class1: System.Windows.Forms.Form
{
private System.Windows.Forms.LinkLabel linkLabel2;

public Class1()
{
//

//
}
public void ShowDlg()
{
Form myForm1=new Form();
myForm1.ShowDialog();
}

}

这个是调用DLL的代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;//这是用到DllImport时候要引入的包

namespace csharp_win
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1: System.Windows.Forms.Form
{
[DllImport("ClassLibrary1.dll", CharSet=CharSet.Auto)]
static extern void ShowDlg();
public Form1()
{
InitializeComponent();
}

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
ShowDlg();

}
}
}

报错找不到ClassLibrary1.dll中ShowDlg入口点

请高手指教
sunyt 2003-05-26
  • 打赏
  • 举报
回复
tjq_tang(重新开始)
请问如何在C#7中调用DLL呢和VC6区别大吗?
sunyt 2003-05-26
  • 打赏
  • 举报
回复
.net代码如下
using System;
using System.Windows.Forms;

namespace ClassLibrary1
{
public class Class1: System.Windows.Forms.Form
{
public Class1()
{
}
public string ShowDlg()
{
Form myForm1=new Form();
myForm1.ShowDialog();

return "";
}

}
}

VC6代码如下
typedef char(*MyDll)();
HINSTANCE gLiMyDLL;//存储DLL句柄
MyDll mydll;
gLiMyDLL = LoadLibrary("ClassLibrary1.dll");//装入DLL;

if(gLiMyDLL == NULL)//装入失败
{
return;
}
mydll = (MyDll)GetProcAddress(gLiMyDLL,"ShowDlg");
mydll();

为什么mydll为NULL?
tjq_tang 2003-05-26
  • 打赏
  • 举报
回复
前面各位说得狠清楚了.
不过vc6 是无法调用vs.net写的dll.
除非你使用vc7
加载更多回复(8)

110,530

社区成员

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

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

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