📄 autoreg.cpp
字号:
/*++
Copyright (C) Microsoft Corporation, 1998 - 1999
Module Name:
autoreg
Abstract:
This module provides autoregistration capabilities to a CSP. It allows
regsvr32 to call the DLL directly to add and remove Registry settings.
Author:
Doug Barlow (dbarlow) 3/11/1998
Environment:
Win32
Notes:
Look for "?vendor?" tags and edit appropriately.
--*/
// ?vendor? If this is a smart card CSP, uncomment the next line.
// #define SCARD_CSP
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifdef _AFXDLL
#include "stdafx.h"
#else
#include <windows.h>
#endif
#ifndef WINVER
#define WINVER 0x0400
#endif
#include <wincrypt.h>
#ifdef SCARD_CSP
#include <winscard.h>
#endif
#include <tchar.h>
#include <cspdk.h>
static HMODULE
GetInstanceHandle(
void);
#ifdef SCARD_CSP
static const TCHAR
l_szCardName[]
= TEXT("?vendor? <Add your Smart Card Friendly Name Here>");
static const GUID // Optional
l_guidPrimaryProv
//?vendor? Add your Primary Provider GUID here
= { /* 00000000-0000-0000-0000-000000000000 */
0x00000000,
0x0000,
0x0000,
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
};
static const BYTE
l_rgbATR[] = { /* ?vendor? Add your ATR here */ 0x3b, 0x00 },
l_rgbATRMask[] = { /* ?vendor? Add your ATR Mask here */ 0xff, 0xff };
#endif
static const TCHAR
l_szProviderName[]
= TEXT("?vendor? <Add your Provider Name Here>");
static const DWORD
l_dwCspType
// ?vendor? Change this to match your CSP capabilities
= PROV_RSA_FULL;
#ifdef SCARD_CSP
typedef DWORD
(*LPSETCARDTYPEPROVIDERNAME)(
IN SCARDCONTEXT hContext,
IN LPCTSTR szCardName,
IN DWORD dwProviderId,
IN LPCTSTR szProvider);
#endif
/*++
DllUnregisterServer:
This service removes the registry entries associated with this CSP.
Arguments:
None
Return Value:
Status code as an HRESULT.
Author:
Doug Barlow (dbarlow) 3/11/1998
--*/
STDAPI
DllUnregisterServer(
void)
{
LONG nStatus;
DWORD dwDisp;
HRESULT hReturnStatus = NO_ERROR;
HKEY hProviders = NULL;
#ifdef SCARD_CSP
SCARDCONTEXT hCtx = NULL;
#endif
#ifdef _AFXDLL
AFX_MANAGE_STATE(AfxGetStaticModuleState());
#endif
//
// Delete the Registry key for this CSP.
//
nStatus = RegCreateKeyEx(
HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider"),
0,
TEXT(""),
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hProviders,
&dwDisp);
if (ERROR_SUCCESS == nStatus)
{
RegDeleteKey(hProviders, l_szProviderName);
RegCloseKey(hProviders);
hProviders = NULL;
}
#ifdef SCARD_CSP
//
// Forget the card type.
//
hCtx = NULL;
SCardEstablishContext(SCARD_SCOPE_SYSTEM, 0, 0, &hCtx);
SCardForgetCardType(hCtx, l_szCardName);
if (NULL != hCtx)
{
SCardReleaseContext(hCtx);
hCtx = NULL;
}
#endif
//
// ?vendor?
// Delete vendor specific registry entries.
//
//
// All done!
//
return hReturnStatus;
}
/*++
DllRegisterServer:
This function installs the proper registry entries to enable this CSP.
Arguments:
None
Return Value:
Status code as an HRESULT.
Author:
Doug Barlow (dbarlow) 3/11/1998
--*/
STDAPI
DllRegisterServer(
void)
{
TCHAR szModulePath[MAX_PATH];
BYTE pbSignature[136]; // Room for a 1024 bit signature, with padding.
OSVERSIONINFO osVer;
LPTSTR szFileName, szFileExt;
HINSTANCE hThisDll;
HRSRC hSigResource;
DWORD dwStatus;
LONG nStatus;
BOOL fStatus;
DWORD dwDisp;
DWORD dwIndex;
DWORD dwSigLength;
HRESULT hReturnStatus = NO_ERROR;
HKEY hProviders = NULL;
HKEY hMyCsp = NULL;
HKEY hCalais = NULL;
HKEY hVendor = NULL;
BOOL fSignatureFound = FALSE;
HANDLE hSigFile = INVALID_HANDLE_VALUE;
#ifdef SCARD_CSP
BOOL fCardIntroduced = FALSE;
SCARDCONTEXT hCtx = NULL;
#endif
#ifdef _AFXDLL
AFX_MANAGE_STATE(AfxGetStaticModuleState());
#endif
//
// Figure out the file name and path.
//
hThisDll = GetInstanceHandle();
if (NULL == hThisDll)
{
hReturnStatus = HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE);
goto ErrorExit;
}
dwStatus = GetModuleFileName(
hThisDll,
szModulePath,
sizeof(szModulePath) / sizeof(TCHAR));
if (0 == dwStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(GetLastError());
goto ErrorExit;
}
szFileName = _tcsrchr(szModulePath, TEXT('\\'));
if (NULL == szFileName)
szFileName = szModulePath;
else
szFileName += 1;
szFileExt = _tcsrchr(szFileName, TEXT('.'));
if (NULL == szFileExt)
{
hReturnStatus = HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
goto ErrorExit;
}
else
szFileExt += 1;
//
// Create the Registry key for this CSP.
//
nStatus = RegCreateKeyEx(
HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider"),
0,
TEXT(""),
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hProviders,
&dwDisp);
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
nStatus = RegCreateKeyEx(
hProviders,
l_szProviderName,
0,
TEXT(""),
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hMyCsp,
&dwDisp);
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
nStatus = RegCloseKey(hProviders);
hProviders = NULL;
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
//
// Install the trivial registry values.
//
nStatus = RegSetValueEx(
hMyCsp,
TEXT("Image Path"),
0,
REG_SZ,
(LPBYTE)szModulePath,
(_tcslen(szModulePath) + 1) * sizeof(TCHAR));
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
nStatus = RegSetValueEx(
hMyCsp,
TEXT("Type"),
0,
REG_DWORD,
(LPBYTE)&l_dwCspType,
sizeof(DWORD));
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
//
// See if we're self-signed. On NT5, CSP images can carry their own
// signatures.
//
hSigResource = FindResource(
hThisDll,
MAKEINTRESOURCE(CRYPT_SIG_RESOURCE_NUMBER),
RT_RCDATA);
//
// Install the file signature.
//
ZeroMemory(&osVer, sizeof(OSVERSIONINFO));
osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
fStatus = GetVersionEx(&osVer);
// ?BUGBUG? -- This works on Windows Millenium, too.
if (fStatus
&& (VER_PLATFORM_WIN32_NT == osVer.dwPlatformId)
&& (5 <= osVer.dwMajorVersion)
&& (NULL != hSigResource))
{
//
// Signature in file flag is sufficient.
//
dwStatus = 0;
nStatus = RegSetValueEx(
hMyCsp,
TEXT("SigInFile"),
0,
REG_DWORD,
(LPBYTE)&dwStatus,
sizeof(DWORD));
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
}
else
{
//
// We have to install a signature entry.
// Try various techniques until one works.
//
for (dwIndex = 0; !fSignatureFound; dwIndex += 1)
{
switch (dwIndex)
{
//
// Look for an external *.sig file and load that into the registry.
//
case 0:
_tcscpy(szFileExt, TEXT("sig"));
hSigFile = CreateFile(
szModulePath,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (INVALID_HANDLE_VALUE == hSigFile)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -