1c2c66affSColin Finck /*
2c2c66affSColin Finck  * PROJECT:     ReactOS Spooler API
3c2c66affSColin Finck  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4c2c66affSColin Finck  * PURPOSE:     Functions related to Print Providers
51f6f08ecSColin Finck  * COPYRIGHT:   Copyright 2015-2017 Colin Finck (colin@reactos.org)
6c2c66affSColin Finck  */
7c2c66affSColin Finck 
8c2c66affSColin Finck #include "precomp.h"
9c2c66affSColin Finck 
10c2c66affSColin Finck BOOL WINAPI
AddPrintProvidorA(PSTR pName,DWORD Level,PBYTE pProviderInfo)1146b91659SColin Finck AddPrintProvidorA(PSTR pName, DWORD Level, PBYTE pProviderInfo)
1246b91659SColin Finck {
13*7bffb703SJames Tabor     LPWSTR nameW = NULL;
14*7bffb703SJames Tabor     PROVIDOR_INFO_1W pi1W;
15*7bffb703SJames Tabor     PROVIDOR_INFO_2W pi2W;
16*7bffb703SJames Tabor     DWORD len;
17*7bffb703SJames Tabor     BOOL res;
18*7bffb703SJames Tabor     PBYTE pPI = NULL;
19*7bffb703SJames Tabor 
2046b91659SColin Finck     TRACE("AddPrintProvidorA(%s, %lu, %p)\n", pName, Level, pProviderInfo);
21*7bffb703SJames Tabor 
22*7bffb703SJames Tabor     ZeroMemory(&pi1W, sizeof(PROVIDOR_INFO_1W));
23*7bffb703SJames Tabor     pi2W.pOrder = NULL;
24*7bffb703SJames Tabor 
25*7bffb703SJames Tabor     switch (Level)
26*7bffb703SJames Tabor     {
27*7bffb703SJames Tabor         case 1:
28*7bffb703SJames Tabor         {
29*7bffb703SJames Tabor             PROVIDOR_INFO_1A *pi1A = (PROVIDOR_INFO_1A*)pProviderInfo;
30*7bffb703SJames Tabor             if (pi1A->pName)
31*7bffb703SJames Tabor             {
32*7bffb703SJames Tabor                 len = MultiByteToWideChar(CP_ACP, 0, pi1A->pName, -1, NULL, 0);
33*7bffb703SJames Tabor                 pi1W.pName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
34*7bffb703SJames Tabor                 MultiByteToWideChar(CP_ACP, 0, pi1A->pName, -1, pi1W.pName, len);
35*7bffb703SJames Tabor             }
36*7bffb703SJames Tabor             if (pi1A->pEnvironment)
37*7bffb703SJames Tabor             {
38*7bffb703SJames Tabor                 len = MultiByteToWideChar(CP_ACP, 0, pi1A->pEnvironment, -1, NULL, 0);
39*7bffb703SJames Tabor                 pi1W.pEnvironment = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
40*7bffb703SJames Tabor                 MultiByteToWideChar(CP_ACP, 0, pi1A->pEnvironment, -1, pi1W.pEnvironment, len);
41*7bffb703SJames Tabor             }
42*7bffb703SJames Tabor             if (pi1A->pDLLName)
43*7bffb703SJames Tabor             {
44*7bffb703SJames Tabor                 len = MultiByteToWideChar(CP_ACP, 0, pi1A->pDLLName, -1, NULL, 0);
45*7bffb703SJames Tabor                 pi1W.pDLLName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
46*7bffb703SJames Tabor                 MultiByteToWideChar(CP_ACP, 0, pi1A->pDLLName, -1, pi1W.pDLLName, len);
47*7bffb703SJames Tabor             }
48*7bffb703SJames Tabor             pPI = (PBYTE)&pi1W;
49*7bffb703SJames Tabor         }
50*7bffb703SJames Tabor             break;
51*7bffb703SJames Tabor 
52*7bffb703SJames Tabor         case 2:
53*7bffb703SJames Tabor         {
54*7bffb703SJames Tabor             PROVIDOR_INFO_2A *pi2A = (PROVIDOR_INFO_2A*)pProviderInfo;
55*7bffb703SJames Tabor             if (pi2A->pOrder)
56*7bffb703SJames Tabor             {
57*7bffb703SJames Tabor                 len = MultiByteToWideChar(CP_ACP, 0, pi2A->pOrder, -1, NULL, 0);
58*7bffb703SJames Tabor                 pi2W.pOrder = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
59*7bffb703SJames Tabor                 MultiByteToWideChar(CP_ACP, 0, pi2A->pOrder, -1, pi2W.pOrder, len);
60*7bffb703SJames Tabor             }
61*7bffb703SJames Tabor             pPI = (PBYTE)&pi2W;
62*7bffb703SJames Tabor         }
63*7bffb703SJames Tabor             break;
64*7bffb703SJames Tabor 
65*7bffb703SJames Tabor         default:
66*7bffb703SJames Tabor             SetLastError(ERROR_INVALID_LEVEL);
6746b91659SColin Finck             return FALSE;
6846b91659SColin Finck     }
6946b91659SColin Finck 
70*7bffb703SJames Tabor     if (pName)
71*7bffb703SJames Tabor     {
72*7bffb703SJames Tabor         len = MultiByteToWideChar(CP_ACP, 0, pName, -1, NULL, 0);
73*7bffb703SJames Tabor         nameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
74*7bffb703SJames Tabor         MultiByteToWideChar(CP_ACP, 0, pName, -1, nameW, len);
75*7bffb703SJames Tabor     }
76*7bffb703SJames Tabor 
77*7bffb703SJames Tabor     res = AddPrintProvidorW( nameW, Level, pPI );
78*7bffb703SJames Tabor 
79*7bffb703SJames Tabor     if (pName) HeapFree(GetProcessHeap(), 0, nameW);
80*7bffb703SJames Tabor     if (pi1W.pName) HeapFree(GetProcessHeap(), 0, pi1W.pName);
81*7bffb703SJames Tabor     if (pi1W.pEnvironment) HeapFree(GetProcessHeap(), 0, pi1W.pEnvironment);
82*7bffb703SJames Tabor     if (pi1W.pDLLName) HeapFree(GetProcessHeap(), 0, pi1W.pDLLName);
83*7bffb703SJames Tabor     if (pi2W.pOrder) HeapFree(GetProcessHeap(), 0, pi2W.pOrder);
84*7bffb703SJames Tabor 
85*7bffb703SJames Tabor     return res;
86*7bffb703SJames Tabor }
87*7bffb703SJames Tabor 
8846b91659SColin Finck BOOL WINAPI
AddPrintProvidorW(PWSTR pName,DWORD Level,PBYTE pProviderInfo)89c2c66affSColin Finck AddPrintProvidorW(PWSTR pName, DWORD Level, PBYTE pProviderInfo)
90c2c66affSColin Finck {
91*7bffb703SJames Tabor     DWORD dwErrorCode;
92*7bffb703SJames Tabor     WINSPOOL_PROVIDOR_CONTAINER ProvidorContainer;
93*7bffb703SJames Tabor 
941f6f08ecSColin Finck     TRACE("AddPrintProvidorW(%S, %lu, %p)\n", pName, Level, pProviderInfo);
95*7bffb703SJames Tabor 
96*7bffb703SJames Tabor     if ((Level < 1) || (Level > 2))
97*7bffb703SJames Tabor     {
98*7bffb703SJames Tabor         SetLastError(ERROR_INVALID_LEVEL);
99c2c66affSColin Finck         return FALSE;
100c2c66affSColin Finck     }
101c2c66affSColin Finck 
102*7bffb703SJames Tabor     ProvidorContainer.ProvidorInfo.pProvidorInfo1 = (WINSPOOL_PROVIDOR_INFO_1*)pProviderInfo;
103*7bffb703SJames Tabor     ProvidorContainer.Level = Level;
104*7bffb703SJames Tabor 
105*7bffb703SJames Tabor     RpcTryExcept
106*7bffb703SJames Tabor     {
107*7bffb703SJames Tabor         dwErrorCode = _RpcAddPrintProvidor( pName, &ProvidorContainer );
108*7bffb703SJames Tabor     }
109*7bffb703SJames Tabor     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
110*7bffb703SJames Tabor     {
111*7bffb703SJames Tabor         dwErrorCode = RpcExceptionCode();
112*7bffb703SJames Tabor         ERR("_RpcEnumPorts failed with exception code %lu!\n", dwErrorCode);
113*7bffb703SJames Tabor     }
114*7bffb703SJames Tabor     RpcEndExcept;
115*7bffb703SJames Tabor 
116*7bffb703SJames Tabor     SetLastError(dwErrorCode);
117*7bffb703SJames Tabor     return (dwErrorCode == ERROR_SUCCESS);
118*7bffb703SJames Tabor }
119*7bffb703SJames Tabor 
120c2c66affSColin Finck BOOL WINAPI
DeletePrintProvidorA(PSTR pName,PSTR pEnvironment,PSTR pPrintProviderName)12146b91659SColin Finck DeletePrintProvidorA(PSTR pName, PSTR pEnvironment, PSTR pPrintProviderName)
12246b91659SColin Finck {
123*7bffb703SJames Tabor     UNICODE_STRING NameW, EnvW, ProviderW;
124*7bffb703SJames Tabor     BOOL Ret;
125*7bffb703SJames Tabor 
12646b91659SColin Finck     TRACE("DeletePrintProvidorW(%s, %s, %s)\n", pName, pEnvironment, pPrintProviderName);
127*7bffb703SJames Tabor 
128*7bffb703SJames Tabor     AsciiToUnicode(&NameW, pName);
129*7bffb703SJames Tabor     AsciiToUnicode(&EnvW, pEnvironment);
130*7bffb703SJames Tabor     AsciiToUnicode(&ProviderW, pPrintProviderName);
131*7bffb703SJames Tabor 
132*7bffb703SJames Tabor     Ret = DeletePrintProvidorW(NameW.Buffer, EnvW.Buffer, ProviderW.Buffer);
133*7bffb703SJames Tabor 
134*7bffb703SJames Tabor     RtlFreeUnicodeString(&ProviderW);
135*7bffb703SJames Tabor     RtlFreeUnicodeString(&EnvW);
136*7bffb703SJames Tabor     RtlFreeUnicodeString(&NameW);
137*7bffb703SJames Tabor 
138*7bffb703SJames Tabor     return Ret;
13946b91659SColin Finck }
14046b91659SColin Finck 
14146b91659SColin Finck BOOL WINAPI
DeletePrintProvidorW(PWSTR pName,PWSTR pEnvironment,PWSTR pPrintProviderName)142c2c66affSColin Finck DeletePrintProvidorW(PWSTR pName, PWSTR pEnvironment, PWSTR pPrintProviderName)
143c2c66affSColin Finck {
144*7bffb703SJames Tabor     DWORD dwErrorCode;
145*7bffb703SJames Tabor 
1461f6f08ecSColin Finck     TRACE("DeletePrintProvidorW(%S, %S, %S)\n", pName, pEnvironment, pPrintProviderName);
147*7bffb703SJames Tabor 
148*7bffb703SJames Tabor     RpcTryExcept
149*7bffb703SJames Tabor     {
150*7bffb703SJames Tabor         dwErrorCode = _RpcDeletePrintProvidor( pName, pEnvironment, pPrintProviderName );
151*7bffb703SJames Tabor     }
152*7bffb703SJames Tabor     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
153*7bffb703SJames Tabor     {
154*7bffb703SJames Tabor         dwErrorCode = RpcExceptionCode();
155*7bffb703SJames Tabor         ERR("_RpcEnumPorts failed with exception code %lu!\n", dwErrorCode);
156*7bffb703SJames Tabor     }
157*7bffb703SJames Tabor     RpcEndExcept;
158*7bffb703SJames Tabor 
159*7bffb703SJames Tabor     SetLastError(dwErrorCode);
160*7bffb703SJames Tabor     return (dwErrorCode == ERROR_SUCCESS);
161c2c66affSColin Finck }
162