-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrinterPortManager.cpp
117 lines (115 loc) · 4.02 KB
/
PrinterPortManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <Windows.h>
#include <Winspool.h>
#include <iostream>
#include<string.h>
#include <string>
#pragma region code for buld and run as executable
//UNCOMMENT THE FOLLOWING LINES TO BUILD AND RUN THE PROJECT AS AN EXECUTABLE:
//static DWORD CreateDeletePort(int action, LPWSTR portName, HANDLE printerObject);
//
//int main(int argc, char** argv)
//{
// if (argc < 3) //Check if the necessary 3 command line arguments are provided.
// {
// std::cerr << "Usage: " << argv[0] << " <port_name> <action>" << std::endl;
// return -1;
// }
//
// size_t size = strlen(argv[1]) + 1; //determining the size of the second command-line Argument:
// wchar_t* _portName = new wchar_t[size]; //allocate Memory for a Wide Character String:
//
// size_t outSize;
//
// //use mbstowcs_s (i.e. MultiByte String to Wide Character String, Secure) to convert Multibyte string to wide sharacter String:
// if (mbstowcs_s(&outSize, _portName, size, argv[1], size - 1) != 0)
// {
// delete[] _portName; //free memory allocated for _portName.
// std::cerr << "Failed to convert string." << std::endl;
// return -1;
// }
//
// LPWSTR portName = _portName; //assign and Convert string to long pointer:
//
// int action = atoi(argv[2]); //convert the third command-line argument from string to integer
//
// HANDLE printerObject;
// PRINTER_DEFAULTS PrinterDefaults; //A structure that contains default settings for a printer
// memset(&PrinterDefaults, 0, sizeof(PrinterDefaults)); //Initialization of PRINTER_DEFAULTS
//
// PrinterDefaults.pDatatype = NULL;
// PrinterDefaults.pDevMode = NULL;
// PrinterDefaults.DesiredAccess = SERVER_ACCESS_ADMINISTER;
//
// //Open the printer
// LPWSTR printer = const_cast<LPTSTR>(TEXT(",XcvMonitor Local Port"));
//
// if (!OpenPrinter(printer, &printerObject, &PrinterDefaults))
// {
// delete[] _portName; //free memory allocated for _portName.
// return -1;
// }
//
// DWORD result = CreateDeletePort(action, portName, printerObject); //create or delete port
// delete[] _portName; //free memory allocated for _portName.
// return result;
//}
//
//static DWORD CreateDeletePort(int action, LPWSTR portName, HANDLE printerObject)
//{
//
// DWORD needed = 0;
// DWORD xcvresult = 0;
//
// if (action == 0)
// {
// XcvData(printerObject, L"AddPort", (BYTE*)portName, (lstrlenW(portName) + 1) * 2, NULL, 0, &needed, &xcvresult);
// }
// else
// {
// XcvData(printerObject, L"DeletePort", (BYTE*)portName, (lstrlenW(portName) + 1) * 2, NULL, 0, &needed, &xcvresult);
// }
//
// //Cleanup
// ClosePrinter(printerObject);
//
// return xcvresult;
//}
#pragma endregion
#pragma region code for build DLL
//UNCOMMENT THE FOLLOWING LINES TO BUILD THE PROJECT AS A DLL:
//extern "C" {
// __declspec(dllexport) DWORD CreateDeletePort(int action, LPWSTR portName);
//}
//
//__declspec(dllexport) DWORD CreateDeletePort(int action, LPWSTR portName) {
// HANDLE printerObject;
// PRINTER_DEFAULTS PrinterDefaults; //A structure that contains default settings for a printer
// memset(&PrinterDefaults, 0, sizeof(PrinterDefaults)); //Initialization of PRINTER_DEFAULTS
//
// PrinterDefaults.pDatatype = NULL;
// PrinterDefaults.pDevMode = NULL;
// PrinterDefaults.DesiredAccess = SERVER_ACCESS_ADMINISTER;
//
// //Open the printer
// LPWSTR printer = const_cast<LPTSTR>(TEXT(",XcvMonitor Local Port"));
//
// if (!OpenPrinter(printer, &printerObject, &PrinterDefaults))
// {
// return -1;
// }
//
// DWORD needed = 0;
// DWORD xcvresult = 0;
//
// if (action == 0)
// {
// XcvData(printerObject, L"AddPort", (BYTE*)portName, (wcslen(portName) + 1) * sizeof(wchar_t), NULL, 0, &needed, &xcvresult);
// }
// else
// {
// XcvData(printerObject, L"DeletePort", (BYTE*)portName, (wcslen(portName) + 1) * sizeof(wchar_t), NULL, 0, &needed, &xcvresult);
// }
// ClosePrinter(printerObject);
// return xcvresult;
//}
#pragma endregion