comparison cinst/windowsstore.c @ 253:3595ea4fd3fb

Use getLastErrorMsg from logging
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 01 Apr 2014 10:50:21 +0000
parents 4de97f74d038
children 759a898990d1
comparison
equal deleted inserted replaced
252:bd7fb50078b4 253:3595ea4fd3fb
4 4
5 #include "windowsstore.h" 5 #include "windowsstore.h"
6 #include "errorcodes.h" 6 #include "errorcodes.h"
7 #include "listutil.h" 7 #include "listutil.h"
8 #include "strhelp.h" 8 #include "strhelp.h"
9 9 #include "logging.h"
10 static LPWSTR
11 getLastErrorMsg()
12 {
13 LPWSTR bufPtr = NULL;
14 DWORD err = GetLastError();
15 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER |
16 FORMAT_MESSAGE_FROM_SYSTEM |
17 FORMAT_MESSAGE_IGNORE_INSERTS,
18 NULL, err, 0, (LPWSTR) &bufPtr, 0, NULL);
19 if (!bufPtr)
20 {
21 HMODULE hWinhttp = GetModuleHandleW (L"crypt32");
22 if (hWinhttp)
23 {
24 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER |
25 FORMAT_MESSAGE_FROM_HMODULE |
26 FORMAT_MESSAGE_IGNORE_INSERTS,
27 hWinhttp, HRESULT_CODE (err), 0,
28 (LPWSTR) &bufPtr, 0, NULL);
29 }
30 }
31 if (!bufPtr)
32 printf ("Error getting last error for code: %lx \n", err);
33 return bufPtr;
34 }
35 10
36 static PCCERT_CONTEXT 11 static PCCERT_CONTEXT
37 b64_to_cert_context(char *b64_data, size_t b64_size) 12 b64_to_cert_context(char *b64_data, size_t b64_size)
38 { 13 {
39 size_t buf_size = 0; 14 size_t buf_size = 0;
57 NULL); 32 NULL);
58 free (buf); /* Windows has a copy */ 33 free (buf); /* Windows has a copy */
59 34
60 if (pCert == NULL) 35 if (pCert == NULL)
61 { 36 {
62 LPWSTR error = getLastErrorMsg(); 37 char *error = getLastErrorMsg();
63 if (error) 38 if (error)
64 { 39 {
65 printf ("Failed to create cert context: %S \n", error); 40 printf ("Failed to create cert context: %s \n", error);
66 LocalFree (error); 41 free (error);
67 } 42 }
68 return NULL; 43 return NULL;
69 } 44 }
70 return pCert; 45 return pCert;
71 } 46 }
88 pc_to_remove = b64_to_cert_context(to_remove[i], 63 pc_to_remove = b64_to_cert_context(to_remove[i],
89 strnlen(to_remove[i], MAX_LINE_LENGTH)); 64 strnlen(to_remove[i], MAX_LINE_LENGTH));
90 65
91 if (pc_to_remove == NULL) 66 if (pc_to_remove == NULL)
92 { 67 {
93 LPWSTR error = getLastErrorMsg(); 68 char *error = getLastErrorMsg();
94 if (error) 69 if (error)
95 { 70 {
96 printf ("Failed to create cert context: %S \n", error); 71 printf ("Failed to create cert context: %s \n", error);
97 LocalFree (error); 72 free (error);
98 } 73 }
99 continue; 74 continue;
100 } 75 }
101 76
102 pCert = CertFindCertificateInStore (hStore, 77 pCert = CertFindCertificateInStore (hStore,
118 { 93 {
119 /* From MSDN: 94 /* From MSDN:
120 The CertDeleteCertificateFromStore function always frees 95 The CertDeleteCertificateFromStore function always frees
121 pCertContext by calling the CertFreeCertificateContext 96 pCertContext by calling the CertFreeCertificateContext
122 function, even if an error is encountered. */ 97 function, even if an error is encountered. */
123 LPWSTR error = getLastErrorMsg(); 98 char *error = getLastErrorMsg();
124 printf ("Error deleting certificate. %S", error); 99 printf ("Error deleting certificate. %s", error);
125 LocalFree (error); 100 free (error);
126 continue; 101 continue;
127 } 102 }
128 } 103 }
129 return; 104 return;
130 } 105 }
172 CERT_STORE_ADD_ALWAYS, 147 CERT_STORE_ADD_ALWAYS,
173 NULL); 148 NULL);
174 CertFreeCertificateContext (pc_to_add); 149 CertFreeCertificateContext (pc_to_add);
175 if (!ret) 150 if (!ret)
176 { 151 {
177 LPWSTR error = getLastErrorMsg(); 152 char *error = getLastErrorMsg();
178 if (error) 153 if (error)
179 { 154 {
180 printf ("Failed to add certificate: %S \n", error); 155 printf ("Failed to add certificate: %s \n", error);
181 LocalFree (error); 156 free (error);
182 } 157 }
183 } 158 }
184 } 159 }
185 return; 160 return;
186 } 161 }

http://wald.intevation.org/projects/trustbridge/