♣
如何调用这个DLL,最好有示例!
原dll是这样写的:
using system;
using system.componentmodel;
using system.collections;
using system.diagnostics;
using system.runtime.interopservices;
namespace commlib
{
/// <summary>
/// serialport 的摘要说明。
/// </summary>
public class serialport : system.componentmodel.component
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public serialport(system.componentmodel.icontainer container)
{
/// <summary>
/// windows.forms 类撰写设计器支持所必需的
/// </summary>
container.add(this);
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
public serialport()
{
/// <summary>
/// windows.forms 类撰写设计器支持所必需的
/// </summary>
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
portnum = "com1:"; // 1 端口号
baudrate = 9600; // 9600 波特率
bytesize = 8; // 8位 数据位
parity = 0; // 0-4=no,odd,even,mark,space 校验位
stopbits = 1; // 0,1,2 = 1, 1.5, 2 停止位
}
#region 申明api
//win32 api constants
private const uint generic_read = 0x80000000;
private const uint generic_write = 0x40000000;
private const int open_existing = 3;
private const int invalid_handle_value = -1;
[structlayout(layoutkind.sequential)]
private struct dcb
{
//taken from c struct in platform sdk
public int dcblength; // sizeof(dcb)
public int baudrate; // current baud rate
public int fbinary; // binary mode, no eof check
public int fparity; // enable parity checking
public int foutxctsflow; // cts output flow control
public int foutxdsrflow; // dsr output flow control
public int fdtrcontrol; // dtr flow control type
public int fdsrsensitivity; // dsr sensitivity
public int ftxcontinueonxoff; // xoff continues tx
public int foutx; // xon/xoff out flow control
public int finx; // xon/xoff in flow control
public int ferrorchar; // enable error replacement
public int fnull; // enable null stripping
public int frtscontrol; // rts flow control
public int fabortonerror; // abort on error
public int fdummy2; // reserved
public ushort wreserved; // not currently used
public ushort xonlim; // transmit xon threshold
public ushort xofflim; // transmit xoff threshold
public byte bytesize; // number of bits/byte, 4-8
public byte parity; // 0-4=no,odd,even,mark,space
public byte stopbits; // 0,1,2 = 1, 1.5, 2
public char xonchar; // tx and rx xon character
public char xoffchar; // tx and rx xoff character
public char errorchar; // error replacement character
public char eofchar; // end of input character
public char evtchar; // received event character
public ushort wreserved1; // reserved; do not use
}
[structlayout(layoutkind.sequential)]
private struct commtimeouts
{
public int readintervaltimeout;
public int readtotaltimeoutmultiplier;
public int readtotaltimeoutconstant;
public int writetotaltimeoutmultiplier;
public int writetotaltimeoutconstant;
}
[structlayout(layoutkind.sequential)]
private struct overlapped
{
public int internal;
public int internalhigh;
public int offset;
public int offsethigh;
public int hevent;
}
[dllimport("kernel32")]
private static extern int createfile(
string lpfilename, // file name
uint dwdesiredaccess, // access mode
int dwsharemode, // share mode
int lpsecurityattributes, // sd
int dwcreationdisposition, // how to create
int dwflagsandattributes, // file attributes
int htemplatefile // handle to template file
);
[dllimport("kernel32")]
private static extern bool getcommstate(
int hfile, // handle to communications device
ref dcb lpdcb // device-control block
);
[dllimport("kernel32")]
private static extern bool buildcommdcb(
string lpdef, // device-control string
ref dcb lpdcb // device-control block
);
[dllimport("kernel32")]
private static extern bool setcommstate(
int hfile, // handle to communications device
ref dcb lpdcb // device-control block
);
[dllimport("kernel32")]
private static extern bool getcommtimeouts(
int hfile, // handle to comm device
ref commtimeouts lpcommtimeouts // time-out values
);
· 网友精彩回答:
我知道点,你参考下,你把你的文件写成.cs 用csc编译放到你用的解决方氨下的bin目录中,然后在你用到的程序中继承dll中的类
在别的命名空间里先“添加引用”,引用这个dll,然后再using commlib就能用里面的方法了
yun , 代码太多??
如果仅仅是调用不到,请引用!
- 更多问题:
- · 六位数的QQ不要钱啦,点击马上申请!放心这里不是病毒
- · CPropertySheet的问题,跪求各位给个答案
- · 打包时设置注册码的问题?
- · 安裝redhat 7.2找不到顯卡怎麼辦?
- · QQ六位号免费申请网址,请大家放心,这里不是病毒
- · 学生的和工作的差别到底在什么地方?
- · fastreport中显示两个数据集的问题
- · 为什么我在编译程序的时候总回弹出请选择可执行文件 对话框
- · 请问为什么不能将信息写入XML文档?
- · 怎么实现工具栏的自动隐藏
- · 请教华硕P2B-F上图拉丁的问题!!
- · 留言本问题
- · 关于用installshield制作安装程序
- · 求数据库后台分析的实例和解决方案
- · 请问C#里有象OutLook左边象treeview一样的控件吗?
- · 录音出现的问题.请高手帮忙,高分相送

