Problem with CryptAcquireContext

"Nuria M.A." <[email protected]> Wed, 8 Nov 2006 06:57:30 -0500
Newsgroups gmane.comp.windows.devel.jawin
Message-ID <LISTSERV%[email protected]>
Hi all! I'm a little bit desesperated.

I'm trying to use the function CryptAcquireContext implemented in ADVAPI32.D=
LL:

BOOL WINAPI CryptAcquireContext( HCRYPTPROV* phProv, LPCTSTR pszContainer,
LPCTSTR pszProvider, DWORD dwProvType,DWORD dwFlags )

And my code is:


....
FuncPtr primer=3D new FuncPtr("ADVAPI32.DLL", "CryptAcquireContextW");
....
NakedByteStream nbs =3D new NakedByteStream();
LittleEndianOutputStream leos =3D new LittleEndianOutputStream(nbs);

leos.writeInt(0);

//a default key container name is used.
leos.writeStringUnicode(null);

//the user default provider is used.
leos.writeStringUnicode(null);

//1 =3D PROV_RSA_FULL type of provider
leos.writeInt(1);

leos.writeInt(0);

byte[] result=3Dprimer.invoke("IGGII:I:I",20,nbs,null,ReturnFlags.CHECK_FALS=
E);
...



This code throws a COMException: 80070057 (Parameter is incorrect). It seems=

the dll doesn't understand null's. I have also tried to do this:



....
....
leos.writeInt(0);
leos.writeStringUnicode(null);

// =93Microsoft Strong Cryptographic Provider=94 is an available cryptograph=
ic
//provider
String str=3Dnew String("Microsoft Strong Cryptographic
Provider".getBytes(),"UTF-16");
//or String str=3Dnew String("\0".getBytes(),"UTF-16");

leos.writeInt(1);
leos.writeInt(0);

byte[] result =3D primer.invoke("IGGII:I:I",20,nbs,null,ReturnFlags.CHECK_FA=
LSE);
....


But then it trows a 80090019 error (The key container specified by
pszContainer does not exist, or the requested provider does not exist). That=

is, the function doesn't understand the string I have passed to it. The
parameters are checked in inverse order, then the function fails when It
checks the provider name.



I have tested the microsoft example
(http://msdn.microsoft.com/library/default.asp?url=3D/library/en-us/seccrypt=
o/security/example_c_program_using_cryptacquirecontext.asp)
and it has worked.

I have tested the jawin example for DLL Entry Points:

...
msgBox =3D new FuncPtr("USER32.DLL", "MessageBoxW");
NakedByteStream nbs =3D new NakedByteStream();
LittleEndianOutputStream leos =3D new LittleEndianOutputStream(nbs);

leos.writeInt(0);
leos.writeStringUnicode("Generic Hello From a DLL");
leos.writeStringUnicode("From Jawin");
leos.writeInt(0);

msgBox.invoke("IGGI:I:", 16, nbs, null, ReturnFlags.CHECK_FALSE);
....



And it works too.
Have anyone any idea?

Thanks in advance,
Nuria.