|
系统有一项功能是通过远程调用条码打印机(斑马打印机 型号:gk888t)来打印条码,使用C#开发的.将代码中的ip地址换成"."后在本地运行可以打印成功.可是通过远程访问其他机器上的打印机就没有反应, 经调试发现,在open方法中调用的CreateFile方法执行后返回的结果都是"-1"(应该是没有获取到操作设备的句柄的意思), 无法正确的打开远程机器上的usb设备的端口. 求大神指教..附上打印功能的代码(网上扒下来修改后的).
[C#] 纯文本查看 复制代码 [StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
}
/// <summary>
/// 打开端口
/// </summary>
/// <returns></returns>
[DllImport("kernel32.dll")]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile);
/// <summary>
/// 发送命令
/// </summary>
/// <returns></returns>
[DllImport("kernel32.dll")]
private static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWriter,
out int lpNumberOfBytesWriten,
out OVERLAPPED lpOverLapped);
/// <summary>
/// 关闭端口
/// </summary>
/// <param name="hObject"></param>
/// <returns></returns>
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(int hObject);
/// <summary>
/// 中文处理
/// </summary>
/// <param name="ChineseText">待转变中文内容</param>
/// <param name="FontName">字体名称</param>
/// <param name="Orient">旋转角度0,90,180,270</param>
/// <param name="Height">字体高度</param>
/// <param name="Width">字体宽度,通常是0</param>
/// <param name="IsBold">1 变粗,0 正常</param>
/// <param name="IsItalic">1 斜体,0 正常</param>
/// <param name="ReturnPicData">返回的图片字符</param>
/// <returns></returns>
[DllImport("FNTHEX32.dll", CharSet = CharSet.Ansi)]
public static extern int GETFONTHEX(
string ChineseText,
string FontName,
int Orient,
int Height,
int Width,
int IsBold,
int IsItalic,
StringBuilder ReturnPicData);
/// <summary>
/// 中文处理
/// </summary>
/// <param name="ChineseText">待转变中文内容</param>
/// <param name="FontName">字体名称</param>
/// <param name="FileName">返回的图片字符重命</param>
/// <param name="Orient">旋转角度0,90,180,270</param>
/// <param name="Height">字体高度</param>
/// <param name="Width">字体宽度,通常是0</param>
/// <param name="IsBold">1 变粗,0 正常</param>
/// <param name="IsItalic">1 斜体,0 正常</param>
/// <param name="ReturnPicData">返回的图片字符</param>
/// <returns></returns>
[DllImport("FNTHEX32.dll", CharSet = CharSet.Ansi)]
public static extern int GETFONTHEX(
string ChineseText,
string FontName,
string FileName,
int Orient,
int Height,
int Width,
int IsBold,
int IsItalic,
StringBuilder ReturnPicData);
private int iHandle;
//打开LPT 端口
public bool Open(string PNPDeviceID)
{
string ss = "\\\\192.168.10.116\\" + PNPDeviceID.Replace('\\', '#') + "#{A5DCBF10-6530-11D2-901F-00C04FB951ED}";
iHandle = CreateFile(ss, (uint)FileAccess.ReadWrite, 0, 0, (int)FileMode.Open, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{
return false;
}
}
//打印函数,参数为打印机的命令或者其他文本!
public bool Write(string MyString)
{
if (iHandle != 1)
{
int i;
OVERLAPPED x;
byte[] mybyte = System.Text.Encoding.Default.GetBytes(MyString);
bool b = WriteFile(iHandle, mybyte, mybyte.Length, out i, out x);
return b;
}
else
{
throw new Exception("端口未打开~!");
}
}
//关闭打印端口
public bool Close()
{
return CloseHandle(iHandle);
}
/// <summary>
/// 打印
/// </summary>
/// <param name="strCommand"></param>
public void ZPL_Print(string strCommand)
{
#region 远程打印
//确定WMI操作的范围
ConnectionOptions options = new ConnectionOptions();
options.Username = "Administrator";//设定用于WMI连接操作的用户名
options.Password = "1234";//设定用户的口令
ManagementScope scope = new ManagementScope("\\\\192.168.10.116\\root\\cimv2", options);//修改查询范围
scope.Connect();
//确定WMI操作的内容
SelectQuery selectQuery = new SelectQuery("Win32_USBHub");
ManagementObjectSearcher query = new ManagementObjectSearcher(scope, selectQuery);
#endregion
//获取WMI操作内容
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject disk in queryCollection)
{
string PNPDeviceID = disk["PNPDeviceID"] as String;
Open(PNPDeviceID);
Write(strCommand);
Close();
}
} |
|