我很久以前就一直在寻找答案。所以,这就是我所拥有的:
维克斯代码:
<CustomAction Id="ImportCer.Props" Property="ImportCer" Value="[INSTALLDIR]ca\root.cer" />
<CustomAction Id="ImportCer" Execute="deferred" FileKey="hsminst.dll" DllEntry="ImportCer" />
<CustomAction Id="ImportPfx.Props" Property="ImportPfx" Value="[INSTALLDIR]ca\super.pfx" />
<CustomAction Id="ImportPfx" Execute="deferred" FileKey="hsminst.dll" DllEntry="ImportPfx" />
C++ 代码:
extern "C" __declspec(dllexport) UINT __stdcall ImportCer(MSIHANDLE hInstall)
{
char szPath[MAX_PATH];
GetModuleFileNameA(NULL, szPath, MAX_PATH);
char certFilePath[MAX_PATH] = {0};
DWORD certFilePathLen = MAX_PATH;
MsiGetProperty (
hInstall,
"CustomActionData",
certFilePath,
&certFilePathLen);
wchar_t certFilePathW[MAX_PATH];
MultiByteToWideChar(
CP_ACP,
0,
certFilePath,
-1,
certFilePathW,
MAX_PATH);
PCCERT_CONTEXT pCertCtx = NULL;
if (CryptQueryObject (
CERT_QUERY_OBJECT_FILE,
certFilePathW,
CERT_QUERY_CONTENT_FLAG_ALL,
CERT_QUERY_FORMAT_FLAG_ALL,
0,
NULL,
NULL,
NULL,
NULL,
NULL,
(const void **)&pCertCtx) != 0)
{
HCERTSTORE hCertStore = CertOpenStore (
CERT_STORE_PROV_SYSTEM,
0,
0,
CERT_STORE_OPEN_EXISTING_FLAG |
CERT_SYSTEM_STORE_LOCAL_MACHINE,
L"root");
if (hCertStore != NULL)
{
if (!CertAddCertificateContextToStore (
hCertStore,
pCertCtx,
CERT_STORE_ADD_ALWAYS,
NULL))
{
return -2;
}
if (!CertCloseStore (hCertStore, 0))
{
return -3;
}
}
else
{
return -1;
}
if (pCertCtx)
{
CertFreeCertificateContext (pCertCtx);
}
}
return 0;
}
extern "C" __declspec(dllexport) UINT __stdcall ImportPfx(MSIHANDLE hInstall)
{
char certFilePath[MAX_PATH] = {0};
DWORD certFilePathLen = MAX_PATH;
MsiGetProperty (
hInstall,
"CustomActionData",
certFilePath,
&certFilePathLen);
wchar_t certFilePathW[MAX_PATH];
MultiByteToWideChar(
CP_ACP,
0,
certFilePath,
-1,
certFilePathW,
MAX_PATH);
CRYPTUI_WIZ_IMPORT_SRC_INFO importSrc;
memset(
&importSrc,
0,
sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO));
importSrc.dwSize = sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
importSrc.dwSubjectChoice = CRYPTUI_WIZ_IMPORT_SUBJECT_FILE;
importSrc.pwszFileName = certFilePathW;
importSrc.pwszPassword = L"111111";
importSrc.dwFlags = CRYPT_EXPORTABLE;
HCERTSTORE serviceStore = CertOpenStore(
CERT_STORE_PROV_SYSTEM,
0,
0,
CERT_STORE_OPEN_EXISTING_FLAG |
CERT_SYSTEM_STORE_CURRENT_USER,
L"my");
if (CryptUIWizImport(
CRYPTUI_WIZ_NO_UI ,
NULL,
NULL,
&importSrc,
serviceStore
) == 0)
{
return -1;
}
return 0;
}
希望能帮助你