求解一段简单的C#代码
下面是一段C#代码,我不是太懂,大概的意思是用类似于[DllImport("kernel32.dll")]这样的东西来调用打印机来打印文件,但是我还是看不太懂每一行或者每...
下面是一段C#代码,我不是太懂,大概的意思是用类似于[DllImport("kernel32.dll")]这样的东西来调用打印机来打印文件,但是我还是看不太懂每一行或者每一个方法的意思,有没有哪位C#大神帮我注释一下或者解释一下这段代码每一段的意思啊,每一个方法分别是干什么用的,谢谢,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Barcode_Print
{
class LPTControl
{
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
}
[DllImport("kernel32.dll")]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
);
[DllImport("kernel32.dll")]
private static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
out int lpNumberOfBytesWritten,
out OVERLAPPED lpOverlapped
);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(
int hObject
);
private int iHandle;
public bool Open()
{
iHandle = CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{ 展开
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Barcode_Print
{
class LPTControl
{
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
}
[DllImport("kernel32.dll")]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
);
[DllImport("kernel32.dll")]
private static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
out int lpNumberOfBytesWritten,
out OVERLAPPED lpOverlapped
);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(
int hObject
);
private int iHandle;
public bool Open()
{
iHandle = CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{ 展开
2个回答
展开全部
debug下,就知道实际情况了:
执行顺序如下:
public static int Y=A.X+1; //此时B.Y = 0;
public static int X; //此时A.X = 0;
static A(){ X=B.Y+1; } //此时A.X = 1;
又回到public static int Y = A.X + 1; //此时B.Y = 2;
static B(){}
静态类执行顺序
静态变量的初始化(并附默认值)
静态构造函数
要多实际操作,才能帮助我们理解程序。
请采纳答案,支持我一下。
执行顺序如下:
public static int Y=A.X+1; //此时B.Y = 0;
public static int X; //此时A.X = 0;
static A(){ X=B.Y+1; } //此时A.X = 1;
又回到public static int Y = A.X + 1; //此时B.Y = 2;
static B(){}
静态类执行顺序
静态变量的初始化(并附默认值)
静态构造函数
要多实际操作,才能帮助我们理解程序。
请采纳答案,支持我一下。
展开全部
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; //这个引用不能缺!
namespace Barcode_Print //命名空间为 Barcode_Print
{
// LPTControl 类
class LPTControl
{
//[StructLayout(……)]用来约束OVERLAPPED在内存中存放方式
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
}
//[DllImport("kernel32.dll")]用来引入动态链接库 kernel32.dll
[DllImport("kernel32.dll")]
// CreateFile 是动态链接库 kernel32.dll 中的函数
// 在C#中,动态链接库中函数必须用 static extern
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
);
[DllImport("kernel32.dll")]
// WriteFile 是动态链接库 kernel32.dll 中的函数
private static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
out int lpNumberOfBytesWritten,
out OVERLAPPED lpOverlapped
);
[DllImport("kernel32.dll")]
// CloseHandle 是动态链接库 kernel32.dll 中的函数
private static extern bool CloseHandle(
int hObject
);
// 私有变量
private int iHandle;
// 类方法 Open
public bool Open()
{
// 调用动态链接库中的函数CreateFile
iHandle = CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{
……
}
}
}
}
追问
由于字数限制,后面还有一小部分代码,追问这里还是写不下,这里是链接,里面有完整版的,把后面的也请帮我注释一下呗,谢谢了。
http://zhidao.baidu.com/question/1702782659547597300.html?quesup2&oldq=1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |