The basis of the code ends up running like the following:
...
Result =
CryptExportKey(KeyToExport,KeyToEncryptWith,SIMPLEBLOB,0,NULL,&DataSize);
if (Result != TRUE) return;
ptrData = malloc(DataSize);
if (ptrData == NULL) return;
memset(ptrData,0,DataSize);
Result =
CryptExportKey(KeyToExport,KeyToEncryptWith,SIMPLEBLOB,0,ptrData,&DataSize);
if (Result != TRUE)
{
DWORD Last_Error = GetLastError();
return;
}
...
That's basically how it is working. As I said before it is working for
other keys (Exchange, Signature, DH_EPHEM, DH Pregen, etc.) and works
for both the RSA Public/Private, DH Public Private, and has support for
the Simple Blobs...
However, it is the simple blob that is giving me trouble in the code
above. The first call to CryptExportKey works fine, and I get back a
size of 0x0000008c (140 decimal) bytes. I then allocate the memory,
which is there, and then call again, only to get a failure with
GetLastError() returning 0x00000008 (ERROR_NOT_ENOUGH_MEMORY).
At first, my system was reporting low virtual memory - so I rebooted
(to clean it up) and tried again, and I still got the same error. Has
anyone seen this before? Any ideas as to what might cause it? I
verified that it is not just my machine (with 256MB RAM) that gets the
error - another developered (with 512MB RAM) tested and got the same
error - both are Win2k SP4. Resources are generally cleaned up - and
the systems have plenty of memory to spare.
FYI - At the time this code gets run, there are 3 contexts in use - a
fourth was created and destroyed before it got run, but there was an
export of a PRIVATEKEYBLOB shortly before this. Since VS uses so much
memory during debug mode, it is hard to say how it is doing for memory,
but there is no run-away memory chunks - everything is very procedural,
and the last step that was performed did clean up its memory; so we
should have plenty of memory left over.
Also - total memory used by the program does not exceed 3 MB; handles
at the end of the program are 37, and are mostly event handles (which I
don't use or even create)
Any suggestions welcome...don't even know where to start looking on
this one.
TIA,
Ben