打印机的端口设置
来源:互联网 发布:android 淘宝订单效果 编辑:程序博客网 时间:2024/04/30 14:58
// PrintPortTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <winspool.h>
#include <iostream>
using namespace std;
#define MAX_PORTNAME_LEN 63 +1 // port name length
#define MAX_NETWORKNAME_LEN 48 +1 // host name length
#define MAX_SNMP_COMMUNITY_STR_LEN 32 +1 // SNMP Community String Name
#define MAX_QUEUENAME_LEN 32 +1 // lpr print que name
#define MAX_IPADDR_STR_LEN 15 +1 // ip address; string version
#define MAX_ADDRESS_STR_LEN 12 +1 // hw address length
#define MAX_DEVICEDESCRIPTION_STR_LEN 256+1
typedef struct _PORT_DATA_1 {
WCHAR sztPortName[MAX_PORTNAME_LEN];
DWORD dwVersion;
DWORD dwProtocol;
DWORD cbSize;
DWORD dwReserved;
WCHAR sztHostAddress[MAX_NETWORKNAME_LEN];
WCHAR sztSNMPCommunity[MAX_SNMP_COMMUNITY_STR_LEN];
DWORD dwDoubleSpool;
WCHAR sztQueue[MAX_QUEUENAME_LEN];
WCHAR sztIPAddress[MAX_IPADDR_STR_LEN];
BYTE Reserved[540];
DWORD dwPortNumber;
DWORD dwSNMPEnabled;
DWORD dwSNMPDevIndex;
} PORT_DATA_1, *PPORT_DATA_1;
BOOL MySetPrinter(LPTSTR pPrinterName, LPTSTR portname)
{
HANDLE hPrinter = NULL;
DWORD dwNeeded = 0;
PRINTER_INFO_2 *pi2 = NULL;
DEVMODE *pDevMode = NULL;
PRINTER_DEFAULTS pd;
BOOL bFlag;
LONG lFlag;
// Open printer handle (on Windows NT, you need full-access because you
// will eventually use SetPrinter)...
ZeroMemory(&pd, sizeof(pd));
pd.DesiredAccess = PRINTER_ALL_ACCESS;
bFlag = OpenPrinter(pPrinterName, &hPrinter, &pd);
if (!bFlag || (hPrinter == NULL))
return FALSE;
// The first GetPrinter tells you how big the buffer should be in
// order to hold all of PRINTER_INFO_2. Note that this should fail with
// ERROR_INSUFFICIENT_BUFFER. If GetPrinter fails for any other reason
// or dwNeeded isn't set for some reason, then there is a problem...
SetLastError(0);
bFlag = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
if ((!bFlag) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
(dwNeeded == 0))
{
ClosePrinter(hPrinter);
return FALSE;
}
// Allocate enough space for PRINTER_INFO_2...
pi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (pi2 == NULL)
{
ClosePrinter(hPrinter);
return FALSE;
}
// The second GetPrinter fills in all the current settings, so all you
// need to do is modify what you're interested in...
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded);
if (!bFlag)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
}
// If GetPrinter didn't fill in the DEVMODE, try to get it by calling
// DocumentProperties...
if (pi2->pDevMode == NULL)
{
dwNeeded = DocumentProperties(NULL, hPrinter,
pPrinterName,
NULL, NULL, 0);
if (dwNeeded <= 0)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
}
pDevMode = (DEVMODE *)GlobalAlloc(GPTR, dwNeeded);
if (pDevMode == NULL)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
}
lFlag = DocumentProperties(NULL, hPrinter,
pPrinterName,
pDevMode, NULL,
DM_OUT_BUFFER);
if (lFlag != IDOK || pDevMode == NULL)
{
GlobalFree(pDevMode);
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
}
pi2->pDevMode = pDevMode;
}
// Driver is reporting that it doesn't support this change...
if (!(pi2->pDevMode->dmFields & DM_ORIENTATION))
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return FALSE;
}
// Specify exactly what we are attempting to change...
pi2->pDevMode->dmFields = DM_ORIENTATION;
//pi2->pDevMode->dmOrientation = dmOrientation;
pi2->pPortName = portname;
// Do not attempt to set security descriptor...
pi2->pSecurityDescriptor = NULL;
// Make sure the driver-dependent part of devmode is updated...
lFlag = DocumentProperties(NULL, hPrinter,
pPrinterName,
pi2->pDevMode, pi2->pDevMode,
DM_IN_BUFFER | DM_OUT_BUFFER);
if (lFlag != IDOK)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return FALSE;
}
// Update printer information...
bFlag = SetPrinter(hPrinter, 2, (LPBYTE)pi2, 0);
if (!bFlag)
// The driver doesn't support, or it is unable to make the change...
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return FALSE;
}
// Tell other apps that there was a change...
SendMessageTimeout(HWND_BROADCAST, WM_DEVMODECHANGE, 0L,
(LPARAM)(LPCSTR)pPrinterName,
SMTO_NORMAL, 1000, NULL);
// Clean up...
if (pi2)
GlobalFree(pi2);
if (hPrinter)
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
// MySetPrinter(L"Microsoft XPS Document Writer", L"sampleport");
HANDLE hXVCPrinter = NULL ;
PRINTER_DEFAULTS PrinterDefaults;
PrinterDefaults.pDatatype = NULL;
PrinterDefaults.pDevMode = NULL;
PrinterDefaults.DesiredAccess = SERVER_ACCESS_ADMINISTER;
/*
/ *if(OpenPrinter(L",XcvMonitor Standard TCP/IP Port", &hXVCPrinter,&PrinterDefaults))
printf("open printer success./n");* /*/
PRINTER_DEFAULTS printerDefaults = { NULL, NULL, SERVER_ACCESS_ADMINISTER };
DWORD dwStatus;
DWORD cbInputData = 100;
PBYTE pOutputData = NULL;
DWORD cbOutputNeeded = 0;
PORT_DATA_1 portData;
HANDLE hXcv = NULL;
HANDLE hPrinter = NULL;
BOOL bResult;
bResult = OpenPrinter(L",XcvMonitor Standard TCP/IP Port", &hXcv, &printerDefaults);
try
{
pOutputData = new BYTE[cbInputData];
}
catch (...)
{
pOutputData = NULL;
}
//
// ########## fix
// 此处可以作为参数进行传递
//
WCHAR * wchPortname = L"sampleport";
WCHAR * qName = L"queuename";
WCHAR * hostname = L"192.168.1.1";
ZeroMemory(&portData, sizeof(PORT_DATA_1));
wcscpy(portData.sztPortName, wchPortname);
portData.dwPortNumber = 515;
portData.dwVersion = 1;
portData.dwProtocol = 2;
portData.cbSize = sizeof(PORT_DATA_1);
portData.dwReserved = 0L;
portData.dwSNMPEnabled = 0;
portData.dwSNMPDevIndex = 0;
portData.dwDoubleSpool = 0;
wcscpy(portData.sztQueue, qName);
wcscpy(portData.sztIPAddress, hostname);
wcscpy(portData.sztHostAddress, hostname);
bResult = XcvData(hXcv, L"AddPort", (PBYTE) &portData, sizeof(PORT_DATA_1), pOutputData, cbInputData, &cbOutputNeeded, &dwStatus);
DWORD errorCode = GetLastError();
printf("get error: %d/n", errorCode);
cout << bResult << endl;
if (hPrinter != NULL)
{
ClosePrinter(hPrinter);
}
if (hXcv != NULL)
{
ClosePrinter(hXcv);
}
if (pOutputData != NULL)
{
delete [] pOutputData;
}
HANDLE configPrinter = NULL;
// if(OpenPrinter(L"Microsoft XPS Document Writer", &configPrinter, &printerDefaults))
// {
// printf("open printer to config success.");
// PORT_INFO_3 portInfo;
// portInfo.dwSeverity = PORT_STATUS_TYPE_INFO;
// portInfo.dwStatus = 0;
// portInfo.pszStatus = L"Hello world!";
//ConfigurePort(L"Microsoft XPS Document Writer", configPrinter, "sampleport");
// //L"Microsoft XPS Document Writer"
// bool bSetPort = SetPort(
// NULL, // printer server name
// L"sampleport", // printer port name
// 3, // information level
// (LPBYTE)&portInfo // information buffer
// );
// DWORD dwSetPort = GetLastError();
// cout << endl << dwSetPort << endl;
// }
// else
// {
// printf("config port error.");
// }
cout << endl;
// cout << "config port result: " << bSetPort << endl;
cout << "赶紧去看看你的打印机端口…… ^_^" << endl;
cin.get();
cin.get();
}
- 打印机的端口设置
- 打印机驱动的安装和怎么设置端口呢?
- 打印机的大小设置
- 关于打印机的设置
- 设置打印机的状态
- 设置EXCEL的默认打印机
- 关于打印机设置的讨论
- 打印机共享的设置方法
- [转]本地打印机的设置
- 设置EXCEL的默认打印机
- Qt的打印机使用设置
- 设置打印机
- 设置打印机
- 打印机设置
- c#打印机设置,取得打印机列表及相应打印机的所有纸张格式
- VC/MFC得到电脑的默认打印机、设置默认打印机、遍历电脑打印机
- 【.Net码农】c#打印机设置,取得打印机列表及相应打印机的所有纸张格式
- 关于上篇修改打印机设置的补充.
- C#多线程学习(五) 多线程的自动管理(定时器)
- Windows Mobile 下使用DirectShow播放视频文件
- ASPxCaptcha 验证码设置
- 在VC中调用DirectShow全屏播放视频
- 回复帖子,怎么没有积分?⊙﹏⊙b汗
- 打印机的端口设置
- windows下mysql5.1安装启动不成功的问题
- 【JQuery入门学习】——JQuery简介
- Spy++的使用方法
- gcc和g++,以及gdb的安装问题
- 传引用,到底传递的什么?
- 重读CLDC之二:类Reader、StreamReader、Helper和InputStreamReader
- Oracle以DBA方式登陆
- c# 进程间通讯 COPYDATA