您的位置:首页 > 编程语言 > C语言/C++

使用C#生成dll并用C++调用dll

2013-01-28 11:22 645 查看
上回说到用telnet的类做了一个wpf的程序,这回我需要做一个dll,当然是没有界面的,里面只有很简单的一个函数PuttyShow(int ID)。

开始的工作都是一样一样的:

一、如何使用c#创建一个dll文件:

1.首先创建一个新类库工程文件

File->New->Project->Visual C# Projects->Class Library。填入工程文件名称,并且选择文件要存放的目录。
2.引用telnet.dll,并把telnet需要的三个cs文件包含进来,我的做法是把这三个cs文件和telnet.dll一起放在一个叫telnet的文件夹,直接拖进我们现在这个工程,连引用都省了。然后在class1中编写代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Telnet;

namespace TelnetClass
{
public class Class1
{
public void PuttyShow(int ID)
{

string f = null;
Terminal tn = new Terminal("192.168.235.10", 23, 10, 80, 40); // hostname, port, timeout [s], width, height
tn.Connect(); // physcial connection
do
{
f = tn.WaitForString("login");   //this word will change with your service
if (f == null)
throw new TerminalException("No password prompt found");
//Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
tn.SendResponse("shrcwcyy\n", true);    // send username
//                tn.SendResponse(Console.ReadLine(), true);    // input username
System.Threading.Thread.Sleep(1000);      //pause 1s
//              Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
tn.SendResponse("sh45wc67\n", true);    // send password
System.Threading.Thread.Sleep(2000);
tn.SendResponse("cd mimo_s\n", true);
System.Threading.Thread.Sleep(1000);      //pause 1s
if ( 1 == ID )
{
tn.SendResponse("bsub lsf.run\n", true);
}
if ( 2 == ID )
{
tn.SendResponse("bsub lsf.run\n", true);
}
if ( 3 == ID )
{
tn.SendResponse("bsub lsf.run\n", true);
}
System.Threading.Thread.Sleep(1000);      //pause 1s

//Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
//System.Threading.Thread.Sleep(1000);

//textBlock1.Text = tn.VirtualScreen.Hardcopy().TrimEnd();   //send output to textblock
//                  tn.SendResponse("bjobs\n", true);    // send password
//                  Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
//                  System.Threading.Thread.Sleep(1000);
} while (false);
}
}
}


3.点击项目名称-》属性,查看命名空间,.net版本的信息,一般不用更改,确认后,点build。

生成的组件会在工程文件的bin\debug目录里,文件扩展名是dll。感觉非常强大,我们的这个dll相当于包含了原来telnet的那个dll和那三个cs文件!

二、如何使用C++调用C#的dll
C#调用C#写的dll很简单,在使用telnet类库的时候我们就做过。下面说说怎么样用C++调用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: