📄 passwordapi.cpp
字号:
if (!LoadWNet())
return ERROR_INTERNET_INTERNAL_ERROR;
// Store credentials.
dwError = (*g_pfWNetCachePassword) (pszKey, (WORD) cbKey, pszCred, (WORD) cbCred, PCE_WWW_BASIC, 0);
return dwError;
}
/*-----------------------------------------------------------------------------
PWLGetCachedCredentials
---------------------------------------------------------------------------*/
DWORD PWLGetCachedCredentials (LPCSTR pszKey, DWORD cbKey,
LPSTR pszCred, LPDWORD pcbCred)
{
DWORD dwError;
// Load WNet.
if (!LoadWNet())
return ERROR_INTERNET_INTERNAL_ERROR;
// Retrieve credentials.
dwError = (*g_pfWNetGetCachedPassword) (pszKey, (WORD) cbKey, pszCred,
(LPWORD) pcbCred, PCE_WWW_BASIC);
return dwError;
}
/*-----------------------------------------------------------------------------
PWLRemoveCachedCredentials
---------------------------------------------------------------------------*/
DWORD PWLRemoveCachedCredentials (LPCSTR pszKey, DWORD cbKey)
{
DWORD dwError;
// Load WNet.
if (!LoadWNet())
return ERROR_INTERNET_INTERNAL_ERROR;
dwError = (*g_pfWNetRemoveCachedPassword) (pszKey, (WORD) cbKey, PCE_WWW_BASIC);
return dwError;
}
// PWL utility functions.
/*-----------------------------------------------------------------------------
LoadWNet
---------------------------------------------------------------------------*/
BOOL LoadWNet(VOID)
{
BOOL fReturn;
// MPR.DLL already loaded.
if (MhmodWNET)
{
fReturn = TRUE;
goto quit;
}
// Load MPR.DLL
MhmodWNET = LoadLibrary(WNETDLL_MODULE);
// Fail if not loaded.
if (MhmodWNET)
{
fReturn = TRUE;
}
else
{
fReturn = FALSE;
goto quit;
}
g_pfWNetGetCachedPassword = (PFWNETGETCACHEDPASSWORD) GetProcAddress(MhmodWNET, WNETGETCACHEDPASS);
g_pfWNetCachePassword = (PFWNETCACHEPASSWORD) GetProcAddress(MhmodWNET, WNETCACHEPASS);
g_pfWNetRemoveCachedPassword = (PFWNETREMOVECACHEDPASSWORD) GetProcAddress(MhmodWNET, WNETREMOVECACHEDPASS);
// Ensure we have all function pointers.
if (!(g_pfWNetGetCachedPassword
&& g_pfWNetCachePassword
&& g_pfWNetRemoveCachedPassword))
{
fReturn = FALSE;
}
quit:
return fReturn;
}
/*------------------------- PStore Functions -------------------------------*/
/*-----------------------------------------------------------------------------
PStoreSetCachedCredentials
---------------------------------------------------------------------------*/
DWORD PStoreSetCachedCredentials(LPCWSTR pszKey, LPCWSTR pszCred, DWORD cbCred, BOOL fRemove)
{
ASSERT(s_pPStoreCreateInstance);
HRESULT hr;
DWORD dwError;
PST_TYPEINFO typeInfo;
PST_PROMPTINFO promptInfo = {0};
GUID itemType = GUID_PStoreType;
GUID itemSubtype = GUID_NULL;
IPStore * pStore = NULL;
// PST_TYPEINFO data.
typeInfo.cbSize = sizeof(typeInfo);
typeInfo.szDisplayName = STR_FTP_CACHE_CREDENTIALS;
// PST_PROMPTINFO data (no prompting desired).
promptInfo.cbSize = sizeof(promptInfo);
promptInfo.dwPromptFlags = 0;
promptInfo.hwndApp = NULL;
promptInfo.szPrompt = NULL;
// Create a PStore interface.
hr = CreatePStore(&pStore);
if (!SUCCEEDED(hr))
goto quit;
ASSERT(pStore != NULL);
// Create a type in HKCU.
hr = pStore->CreateType(PST_KEY_CURRENT_USER, &itemType, &typeInfo, 0);
if (!((SUCCEEDED(hr)) || (hr == PST_E_TYPE_EXISTS)))
goto quit;
// Create subtype.
hr = pStore->CreateSubtype(PST_KEY_CURRENT_USER, &itemType,
&itemSubtype, &typeInfo, NULL, 0);
if (!((SUCCEEDED(hr)) || (hr == PST_E_TYPE_EXISTS)))
goto quit;
// Valid credentials are written; No credentials imples
// that the key and credentials are to be deleted.
if (pszCred && cbCred && !fRemove)
{
// Write key and credentials to PStore.
hr = pStore->WriteItem(PST_KEY_CURRENT_USER,
&itemType,
&itemSubtype,
pszKey,
cbCred,
(LPBYTE) pszCred,
&promptInfo,
PST_CF_NONE,
0);
}
else
{
// Delete key and credentials from PStore.
hr = pStore->DeleteItem(PST_KEY_CURRENT_USER,
&itemType,
&itemSubtype,
pszKey,
&promptInfo,
0);
}
quit:
// Release the interface, convert error and return.
ReleasePStore(pStore);
if (SUCCEEDED(hr))
dwError = ERROR_SUCCESS;
else
dwError = ERROR_INTERNET_INTERNAL_ERROR;
return dwError;
}
/*-----------------------------------------------------------------------------
PStoreGetCachedCredentials
---------------------------------------------------------------------------*/
DWORD PStoreGetCachedCredentials(LPCWSTR pszKey, LPWSTR pszCred, LPDWORD pcbCred)
{
ASSERT(s_pPStoreCreateInstance);
HRESULT hr ;
DWORD dwError;
LPBYTE pbData;
PST_PROMPTINFO promptInfo = {0};
GUID itemType = GUID_PStoreType;
GUID itemSubtype = GUID_NULL;
IPStore* pStore = NULL;
// PST_PROMPTINFO data (no prompting desired).
promptInfo.cbSize = sizeof(promptInfo);
promptInfo.dwPromptFlags = 0;
promptInfo.hwndApp = NULL;
promptInfo.szPrompt = NULL;
// Create a PStore interface.
hr = CreatePStore(&pStore);
if (!SUCCEEDED(hr))
goto quit;
ASSERT(pStore != NULL);
// Read the credentials from PStore.
hr = pStore->ReadItem(PST_KEY_CURRENT_USER,
&itemType,
&itemSubtype,
pszKey,
pcbCred,
(LPBYTE*) &pbData,
&promptInfo,
0);
// Copy credentials and free buffer allocated by ReadItem.
if (SUCCEEDED(hr))
{
memcpy(pszCred, pbData, *pcbCred);
CoTaskMemFree(pbData);
//hr = S_OK;
}
quit:
// Release the interface, convert error and return.
ReleasePStore(pStore);
if (SUCCEEDED(hr))
dwError = ERROR_SUCCESS;
else
dwError = ERROR_INTERNET_INTERNAL_ERROR;
return dwError;
}
/*-----------------------------------------------------------------------------
PStoreRemoveCachedCredentials
---------------------------------------------------------------------------*/
DWORD PStoreRemoveCachedCredentials(LPCWSTR pszKey)
{
// Pass in TRUE to remove credentials.
return PStoreSetCachedCredentials(pszKey, NULL, 0, TRUE);
}
// PStore utility functions
/*-----------------------------------------------------------------------------
CreatePStore
---------------------------------------------------------------------------*/
HRESULT CreatePStore(IPStore **ppIPStore)
{
return s_pPStoreCreateInstance (ppIPStore, NULL, NULL, 0);
}
/*-----------------------------------------------------------------------------
ReleasePStore
---------------------------------------------------------------------------*/
STDAPI ReleasePStore(IPStore *pIPStore)
{
HRESULT hr;
if (pIPStore)
{
pIPStore->Release();
hr = S_OK;
}
else
{
hr = E_POINTER;
}
return hr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -