Mita Patel
1/15/2003 10:09:00 AM
Hi,
I created a Windows Forms Application in C# that uses PInvoke to call into
Windows Crypto API. Now I want to move some of that code over to a Web
Service and I get a strange error. For example, the following code works in
my Windows Forms Application but not in a Web Service WebMethod. The error
that GetLastError returns is 126 which is ERROR_MOD_NOT_FOUND. I think this
error code is irrelevant and that the problem lies in the way the web
service is calling into native code. Please help!!!
Thanks, Mita
[WebMethod]
public void TestCryptoStuff()
{
bool result = false;
UInt32 lastError = 0;
IntPtr hProv = IntPtr.Zero;
// Get the default CSP
result = CryptAcquireContext(ref hProv, null, null, PROV_RSA_FULL, 0);
if( result == false)
{
lastError = GetLastError();
}
if(hProv != IntPtr.Zero)
CryptReleaseContext(hProv, 0);
}
// Definitions - obtained from wincrypt.h
private static UInt32 PROV_RSA_FULL= 1;
/*public static extern BOOLEAN CryptAcquireContext (HCRYPTPROV *phProv,
LPCTSTR pszContainer,
LPCTSTR pszProvider,
DWORD dwProvType,
DWORD dwFlags); */
[DllImport("Advapi32.dll")]
public static extern bool CryptAcquireContext (ref IntPtr hProv,
string pszContainer,
string pszProvider,
UInt32 dwProvType,
UInt32 dwFlags);
/*
BOOL WINAPI CryptReleaseContext(
HCRYPTPROV hProv,
DWORD dwFlags
);
*/
[DllImport ("Advapi32.dll")]
public static extern bool CryptReleaseContext(IntPtr hProv, UInt32
dwFlags);
// Error handling
[DllImport ("Kernel32.dll")]
public static extern uint GetLastError();