powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / C++ [игнор отключен] [закрыт для гостей] / GetLastWin32Error
4 сообщений из 4, страница 1 из 1
GetLastWin32Error
    #34641947
MegaSHad
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Как можно (если можно) получить строку содержащую текст последней ошибки по индексу, который возвращает функция GetLastWin32Error()
...
Рейтинг: 0 / 0
GetLastWin32Error
    #34642156
mikhail_n
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Когда сам столкнулся с такой проблемой, ничего лучще нижеприведенного не нашёл, хотя особо и не заморачивался поиском/изобретением чего-нибудь получше.



Код: plaintext
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.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
#include <windows.h>
#include <stdio.h>
#include "CryptErr.h"

#ifdef __CPLUSPLUS
extern "C"
	{
#endif

void WhatIsError(bool bGLE, bool bGraphicOutput, DWORD dwError, LPSTR pTitle)
{
	/*This function was developed in 1996 by Robert Coleridge,
	  Microsoft Developer Network Group and can be found in MSDN as an
	  appendix to the article 'The Cryptography API, or How to Keep a Secret'.

	  Was slightly modified by mikhail_n on 10/09/2002 */
	
	char Msg[ 1024 ];
	bool bDisplayError = TRUE;

	if (bGLE !=  0 )
	{
		dwError = GetLastError();
	}

	switch (dwError)
	{
		default:
			sprintf(Msg, "Unknown error: %ld.", dwError);
			break;
		case  0 :
			bDisplayError = FALSE;
			break;
		case ERROR_BUSY:
			strcpy(Msg, "The CSP context specified by the container handle is currently being used by another process.");
			break;
		case ERROR_INVALID_HANDLE:
			strcpy(Msg, "One of the parameters specifies an invalid handle.");
			break;
		case ERROR_INVALID_PARAMETER:
			strcpy(Msg, "One of the parameters contains an invalid value. This is most often an invalid pointer.");
			break;
		case ERROR_MORE_DATA:
			strcpy(Msg, "The buffer specified by the pbData parameter is not large enough to hold the returned data.");
			break;
		case ERROR_NONE_MAPPED:
			strcpy(Msg, "No mapping between account names and security IDs was done.");
			break;
		case ERROR_NOT_ENOUGH_MEMORY:
			strcpy(Msg, "The operating system ran out of memory during the operation.");
			break;
		case NTE_BAD_ALGID:
			if(!strcmp(pTitle, "CryptGenKey"))
				strcpy(Msg, "The Algid parameter specifies an algorithm that this CSP does not support.");
			else if(!strcmp(pTitle, "CryptEncrypt") || !strcmp(pTitle, "CryptDecrypt"))
				strcpy(Msg, "The hKey session key specifies an algorithm that this CSP does not support.");
			else
				strcpy(Msg, "The simple key BLOB you are trying to import is not encrypted with the expected key exchange algorithm.");
			break;
		case NTE_BAD_DATA:
			if(!strcmp(pTitle, "CryptEncrypt") || !strcmp(pTitle, "CryptDecrypt"))
				strcpy(Msg, "The data to be encrypted/decrypted is invalid, e.g. when a block cipher is used and the Final flag is FALSE, the value specified by pdwDataLen must be a multiple of the block size.");
			else
				strcpy(Msg, "Either the algorithm that works with the public key you are trying to import is not supported, or an attempt was made to import a session key that was encrypted with something other than one of your public keys.");
			break;
		case NTE_BAD_FLAGS:
			strcpy(Msg, "The dwFlags parameter has an illegal value.");
			break;
		case NTE_BAD_KEY:
			if(!strcmp(pTitle, "CryptExportKey"))
				strcpy(Msg, "One or both keys specified by hKey and hExpKey are invalid.");
			else if(!strcmp(pTitle, "CryptEncrypt") || !strcmp(pTitle, "CryptDecrypt"))
				strcpy(Msg, "The hKey parameter does not contain a valid handle to a key.");
			else
				strcpy(Msg, "The dwKeySpec parameter contains an invalid value.");
			break;
		case NTE_BAD_KEY_STATE:
			strcpy(Msg, "You do not have permission to export the key. That is, when the hKey key was created, the CRYPT_EXPORTABLE flag was not specified.");
			break;
		case NTE_BAD_KEYSET:
			strcpy(Msg, "The Registry entry for the key container could not be opened and may not exist.");
			break;
		case NTE_BAD_KEYSET_PARAM:
			strcpy(Msg, "The pszContainer or pszProvider parameter is set to an invalid value.");
			break;
		case NTE_BAD_LEN:
			strcpy(Msg, "The size of the output buffer is too small to hold the generated ciphertext/plaintext.");
			break;
		case NTE_BAD_PROV_TYPE:
			strcpy(Msg, "The value of the dwProvType parameter is out of range. All provider types must be from 1 to 999, inclusive.");
			break;
		case NTE_BAD_PUBLIC_KEY:
			strcpy(Msg, "The key BLOB type specified by dwBlobType is PUBLICKEYBLOB, but hExpKey does not contain a public key handle.");
			break;
		case NTE_BAD_SIGNATURE:
			strcpy(Msg, "The provider DLL signature did not verify correctly. Either the DLL or the digital signature has been tampered with.");
			break;
		case NTE_BAD_TYPE:
			if(!strcmp(pTitle, "CryptImportKey"))
				strcpy(Msg, "The key BLOB type is not supported by this CSP and is possibly invalid.");
			else if(!strcmp(pTitle, "CryptExportKey"))
				strcpy(Msg, "The dwBlobType parameter specifies an unknown BLOB type.");
			else
				strcpy(Msg, "The dwParam parameter specifies an unknown parameter.");
			break;
		case NTE_BAD_VER:
			strcpy(Msg, "The key BLOB's version number does not match the CSP version. This usually indicates that the CSP needs to be upgraded.");
			break;
		case NTE_BAD_UID:
			if(!strcmp(pTitle, "CryptExportKey") || !strcmp(pTitle, "CryptEncrypt") || !strcmp(pTitle, "CryptDecrypt"))
				strcpy(Msg, "The CSP context that was specified when the hKey key was created cannot be found.");
			else
				strcpy(Msg, "The hProv parameter does not contain a valid context handle.");
			break;
		case NTE_DOUBLE_ENCRYPT:
			strcpy(Msg, "The application attempted to encrypt/decrypt the same data twice.");
			break;
		case NTE_EXISTS:
			strcpy(Msg, "The dwFlags parameter is CRYPT_NEWKEYSET, but the key container already exists.");
			break;
		case NTE_FAIL:
			strcpy(Msg, "The function failed in some unexpected way.");
			break;
		case NTE_KEYSET_ENTRY_BAD:
			strcpy(Msg, "The Registry entry for the pszContainer key container was found (in the HKEY_CURRENT_USER window), but is corrupt. See the section System Administration for details about CryptoAPI's Registry usage.");
			break;
		case NTE_KEYSET_NOT_DEF:
			strcpy(Msg, "No Registry entry exists in the HKEY_CURRENT_USER window for the key container specified by pszContainer.");
			break;
		case NTE_NO_KEY:
			if(!strcmp(pTitle, "CryptExportKey"))
				strcpy(Msg, "A session key is being exported, and the hExpKey parameter does not specify a public key.");
			else
				strcpy(Msg, "The key requested by the dwKeySpec parameter does not exist.");
			break;
		case NTE_NO_MEMORY:
			strcpy(Msg, "The CSP ran out of memory during the operation.");
			break;
		case NTE_PROV_DLL_NOT_FOUND:
			strcpy(Msg, "The provider DLL file does not exist or is not on the current path.");
			break;
		case NTE_PROV_TYPE_ENTRY_BAD:
			strcpy(Msg, "The Registry entry for the provider type specified by dwProvType is corrupt. This error may relate to either the user default CSP list or the machine default CSP list. See the section System Administration for details about CryptoAPI's Registry usage.");
			break;
		case NTE_PROV_TYPE_NO_MATCH:
			strcpy(Msg, "The provider type specified by dwProvType does not match the provider type found in the Registry. Note that this error can only occur when pszProvider specifies an actual CSP name.");
			break;
		case NTE_PROV_TYPE_NOT_DEF:
			strcpy(Msg, "No Registry entry exists for the provider type specified by dwProvType.");
			break;
		case NTE_PROVIDER_DLL_FAIL:
			strcpy(Msg, "The provider DLL file could not be loaded, and may not exist. If it exists, then the file is not a valid DLL.");
			break;
		case NTE_SIGNATURE_FILE_BAD:
			strcpy(Msg, "An error occurred while loading the DLL file image, prior to verifying its signature.");
			break;
		case NTE_SILENT_CONTEXT:
			strcpy(Msg, "The provider could not perform the action because the context was acquired as silent.");
			break;
	}

	if (bDisplayError && bGraphicOutput)
	{
		if (pTitle != NULL)
		{
			MessageBox(NULL, Msg, pTitle, MB_OK|MB_ICONERROR);
		}
		else
		{
			MessageBox(NULL, Msg, "CryptoAPI Manager Error", MB_OK|MB_ICONERROR);
		}
	}
	else if(bDisplayError)
		puts(Msg);

	return;
}

#ifdef __CPLUSPLUS
	}
#endif
...
Рейтинг: 0 / 0
GetLastWin32Error
    #34642224
White Owl
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
А что, функцию FormatMessage() уже отменили?
...
Рейтинг: 0 / 0
GetLastWin32Error
    #34659594
White Owl гы гы :)

#ifdef DebugVersion
WCHAR ErrMess[260];
#endif
void OutputLastError()
{
#ifdef DebugVersion
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM + FORMAT_MESSAGE_IGNORE_INSERTS,
0,GetLastError(),0,(LPWSTR)&ErrMess,260,0);
DPRINT((LPWSTR)&ErrMess);
#endif
return;
}
...
Рейтинг: 0 / 0
4 сообщений из 4, страница 1 из 1
Форумы / C++ [игнор отключен] [закрыт для гостей] / GetLastWin32Error
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]