| Using
SecureXML
from Microsoft Visual C++ 6.0
Before compiling any C++ programs, please make sure that XMLSign.idl
file is included in your project and it is part of the build process.
You may have to manually specify the build command for the idl file
if one is not added automatically by Visual Studio. You can do this
by specifying midl $(InputPath) for the custom build command and
an output directory for XMLSign.idl file by right clicking XMLSign.idl
in the FileView and choosing Settings.
#include "stdafx.h"
/* The following two files are generated by Visual Studio provided
IDL compiler: MIDL */
#include "XMLSign.h"
#include "XMLSign_i.c"
int main(int argc, char* argv[])
{
HRESULT hr;
IUnknown *pUnknown;
ISignature *pSig;
BSTR verifyR;
BSTR inputData;
/* Initialize Windows COM Infrastructure */
hr = CoInitialize(NULL);
if (FAILED(hr)) {
printf("%s","CoInitialize failed.\n");
exit(0);
}
/* Create an instance of SecureXML Digital Signature */
hr = CoCreateInstance(CLSID_Signature, NULL, CLSCTX_INPROC_SERVER,
IID_IUnknown, (void **) &pUnknown);
if (FAILED(hr)) {
printf("%s","CoCreateInstance failed.\n");
exit(0);
}
/* Create a XMLSign.Signature Object and obtain it's handle */
hr = pUnknown->QueryInterface(IID_ISignature, (void **) &pSig);
if (FAILED(hr)) {
printf("%s","QueryInterface failed.\n");
exit(0);
}
/* Now pSig is a pointer to a XMLSign.Signature Object */
/* Since we don't want any more Signature objects, release the handle
to SecureXML */
pUnknown->Release();
/* Lets invoke some methods and set a few properties to make sure
we can use it */
hr = pSig->ReadAll(L"TwoSignature.xml", &inputData);
printf("After ReadAll\n");
if (hr != S_OK) {
printf("%s","ReadAll failed.\n");
exit(0);
}
hr = pSig->SecureXMLVerify(inputData, &verifyR);
printf("After SecureXMLVerify\n");
if (hr != S_OK) {
printf("%s","SecureXMLVerify failed.\n");
exit(0);
}
printf("%ws", verifyR);
/* Release memory allocated by SecureXML */
SysFreeString(inputData);
SysFreeString(verifyR);
/* Now lets create a new signature */
hr = pSig->put_SignatureID(0, L"MySignature");
BSTR tmpSignedXMLFileName;
hr = pSig->Sign(L"catalog.xml", &tmpSignedXMLFileName);
hr = pSig->put_OverwriteFile(1);
hr = pSig->SaveXMLSignature(L"catalogSigned.xml");
/* catalogSigned.xml now contains the signed xml */
/* Release memory allocated by SecureXML */
SysFreeString(tmpSignedXMLFileName);
pSig->Release();
CoUninitialize();
}
|