Downloads Tutorial Mac OS X
  Read White Papers, Case Studies and Articles
 
Google
Web
infomosaic.net






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.

/* Defining COBJMACROS allows you to call the MIDL generated wrappers for SecureXML methods and properties.
* It makes using SecureXML from C a little easier.
*/
#define COBJMACROS

#include <stdio.h>
/* The following two files are generated by Visual Studio provided IDL compiler: MIDL */
#include "XMLSign.h"
#include "XMLSign_i.c"

void main()
{
HRESULT hr;
IUnknown *pUnknown;
ISignature *pSig;
BSTR tmpFileName;
BSTR inputData;
LONG status;
int signatureCount;
int certificateCount;
int referenceCount;
BSTR signerName;
BSTR certIssuer;
BSTR docURI;
BSTR sigId;
int i,j;

/* 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 = ISignature_QueryInterface(pUnknown, &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 */
ISignature_Release(pUnknown);

/* Lets invoke some methods and set a few properties to make sure we can use it */
hr = ISignature_ReadAll(pSig, L"TwoSignature.xml", &inputData);
if (FAILED(hr)) {
printf("%s","ReadAll failed.\n");
exit(0);
}

/* Verify the signature from the above file */
hr = ISignature_VerifyXMLStr(pSig, inputData, &status);
if (status) {
printf("Signature Verified Successfully\n");
ISignature_get_SignatureCount(pSig, &signatureCount);
printf("Signature Count = %d\n", signatureCount);
for (i=0; i < signatureCount; i++) {
ISignature_get_SignatureID(pSig, i, &sigId);
ISignature_get_SignerSubject(pSig, sigId, &signerName);
ISignature_get_CertIssuer(pSig, sigId, &certIssuer);
printf("Signature #%d, Signer Name = %ws\n\n", i, signerName);
printf("Signature #%d, Certificate Issued By %ws\n\n", i, certIssuer);
ISignature_get_TotalUriCount(pSig, &referenceCount);
for (j=0; j < referenceCount; j++) {
ISignature_get_DocumentURI(pSig, i, j, &docURI);
printf("Signature #%d Document URI #%d = %ws\n", i, j, docURI);
/* Release memory allocated by SecureXML */
SysFreeString(docURI);
}
/* Release memory allocated by SecureXML */
SysFreeString(sigId);
SysFreeString(signerName);
SysFreeString(certIssuer);
}
}

hr = ISignature_get_CertificateCount(pSig, &certificateCount);
printf("You have %d PKI certificates installed in your computer\n", certificateCount);
if (status == 0) {
printf("Signature verification failed\n");
}

/* Lets create a signature */
hr = ISignature_put_SignatureID(pSig, 0, L"MySignature");
hr = ISignature_put_EnvelopingFlag(pSig, 2); /* Create Enveloped Signature */
hr = ISignature_put_PhysicalSignatureUsage(pSig, 2); /* Lets capture a live signature image */
hr = ISignature_Sign(pSig, L"catalog.xml", &tmpFileName); /* Sign the XML contained in catalog.xml file */

hr = ISignature_put_OverwriteFile(pSig, 1); /* Force file overwrite */
hr = ISignature_SaveXMLSignature(pSig, L"catalogSigned.xml");
printf("Signature saved as catalogSigned.xml\n");

/* Lets verify the signature we just created */
hr = ISignature_Verify(pSig, L"catalogSigned.xml", &status);
if (status) {
ISignature_get_SignatureCount(pSig, &status);
printf("Signature Verified Successfully\n");
printf("Signature Count = %d\n", status);
}
/* Release memory allocated by SecureXML */
SysFreeString(inputData);
SysFreeString(tmpFileName);
ISignature_Release(pSig); /* We are done */
CoUninitialize();
}

All contents are Copyright © 2000--2008 Infomosaic Corporation. All rights reserved.
Page last updated on Tuesday, July 1, 2008

Success Stories

Google
Web
infomosaic.net