I am having a problem converting some C++, from MSDN, into PB. I have tried umpteen PB interpretations to no avail.
The real problem is that I do not fully understand what the MSDN code is doing.
Here is the code from MSDN along with a structure definition and what I have found in PB's docs.
The code requires Win Vista or later and PB compilers 6 or 10.
This is what MSDN say:
The opening code is for the first method - that is, the buffer will allocated for us.
The first issue which I do not understand is 'PCRYPT_PROVIDERS pBuffer = NULL;'. I understand the need but do not know how to accomplish it.
The second issue is that the assignment given to the buffer which BCryptEnumRegisteredProviders will allocate for us is the same as the assignment used for the CRYPT_PROVIDERS structure.
It seems to me that we do not have a structure on the one hand and a separate buffer on the other but a structure base which gets redefined, the first Dword being cProviders, the number of providers, and then a 'bunch' of rgpszProviders each one pointing to the individual providers.
Whatever interpretation I have used I always get cbBuffer = 334 but I have had many values for cProviders ranging from 4 to 400,000 when I should be getting about 33.
Don't laugh becuase I have been at this a couple of days. It is an aspect that I have never seen before and although always a struggle for me to convert MSDN's C++ to PB I usually get there in the end.
Can anyone shed any light on what the MSDN code is actually doing?
The real problem is that I do not fully understand what the MSDN code is doing.
Here is the code from MSDN along with a structure definition and what I have found in PB's docs.
The code requires Win Vista or later and PB compilers 6 or 10.
Code:
C++ #include <windows.h> #ifndef NT_SUCCESS #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0) #endif void EnumProviders1() { NTSTATUS status; ULONG cbBuffer = 0; PCRYPT_PROVIDERS pBuffer = NULL; /* Get the providers, letting the BCryptEnumRegisteredProviders function allocate the memory. */ status = BCryptEnumRegisteredProviders(&cbBuffer, &pBuffer); if (NT_SUCCESS(status)) { if (pBuffer != NULL) { // Enumerate the providers. for (ULONG i = 0; i < pBuffer->cProviders; i++) { printf("%S\n", pBuffer->rgpszProviders[i]); } } } else { printf("BCryptEnumRegisteredProviders failed with error " "code 0x%08x\n", status); } if (NULL != pBuffer) { /* Free the memory allocated by the BCryptEnumRegisteredProviders function. */ BCryptFreeBuffer(pBuffer); } } ---------------------------------------------------------------- C++ typedef struct _CRYPT_PROVIDERS { ULONG cProviders; PWSTR *rgpszProviders; } CRYPT_PROVIDERS, *PCRYPT_PROVIDERS; Members cProviders Contains the number of elements in the rgpszProviders array. rgpszProviders An array of null-terminated Unicode strings that contains the names of the registered providers. ---------------------------------------------------------------- From PB's BCrypt.inc with compilers 6 and 10. DECLARE FUNCTION BCryptEnumRegisteredProviders LIB "BCrypt.dll" _ ALIAS "BCryptEnumRegisteredProviders" (pcbBuffer AS DWORD, _ ppBuffer AS ANY) AS LONG TYPE CRYPT_PROVIDERS cProviders AS DWORD rgpszProviders AS DWORD END TYPE ----------------------------------------------------------------
You use the BCryptEnumRegisteredProviders function to enumerate the registered providers. The BCryptEnumRegisteredProviders function can be called in one of two ways:
1. The first is to have the BCryptEnumRegisteredProviders function allocate the memory. This is accomplished by passing the address of a NULL pointer for the ppBuffer parameter. This code will allocate the memory required for the CRYPT_PROVIDERS structure and the associated strings. When the BCryptEnumRegisteredProviders function is used in this manner, you must free the memory when it is no longer needed by passing ppBuffer to the BCryptFreeBuffer function.
1. The first is to have the BCryptEnumRegisteredProviders function allocate the memory. This is accomplished by passing the address of a NULL pointer for the ppBuffer parameter. This code will allocate the memory required for the CRYPT_PROVIDERS structure and the associated strings. When the BCryptEnumRegisteredProviders function is used in this manner, you must free the memory when it is no longer needed by passing ppBuffer to the BCryptFreeBuffer function.
The first issue which I do not understand is 'PCRYPT_PROVIDERS pBuffer = NULL;'. I understand the need but do not know how to accomplish it.
The second issue is that the assignment given to the buffer which BCryptEnumRegisteredProviders will allocate for us is the same as the assignment used for the CRYPT_PROVIDERS structure.
It seems to me that we do not have a structure on the one hand and a separate buffer on the other but a structure base which gets redefined, the first Dword being cProviders, the number of providers, and then a 'bunch' of rgpszProviders each one pointing to the individual providers.
Whatever interpretation I have used I always get cbBuffer = 334 but I have had many values for cProviders ranging from 4 to 400,000 when I should be getting about 33.
Don't laugh becuase I have been at this a couple of days. It is an aspect that I have never seen before and although always a struggle for me to convert MSDN's C++ to PB I usually get there in the end.
Can anyone shed any light on what the MSDN code is actually doing?
Comment