Previous Topic

Next Topic

Book Contents

Book Index

MS Visual C++ Sample Code

A sample Visual C++ file is included with the installation. We encourage users to reference that file.

One easy way to use the COM interface in a Visual C++ application is as follows:

  1. Create a new dialog based MFC application.
  2. Using ClassWizard, choose Add Class... From a Type Library.
  3. Browse to find the Random Numbers.tlb file in the program folder.
  4. Select the Generator class and header file for import.
  5. Use code similar to the following:

void CMSVC6SampleDlg::OnTest()

{

IGenerator Generator;

long lResult;

float fResult;

double dResult;

VARIANT vResult;

unsigned char HUGEP* pData;

// Create object.

Generator.CreateDispatch("RandomNumbers.Generator");

// Call functions.

lResult = Generator.BufferedData();

fResult = Generator.BufferStatus();

// Call random number functions.

lResult = Generator.RandomInteger(1000);

dResult = Generator.RandomDouble();

// Test for errors:

if (lResult != -1 && dResult != -1)

{// Error }

// Retrieve 10 random bytes.

vResult = Generator.RandomData(10);

// Test result.

if (vResult.vt == (VT_ARRAY | VT_UI1)) {

// Get upper bound.

if (SafeArrayGetUBound(vResult.parray, 1, &lResult) == S_OK) {

// Check upper bound.

if (lResult >= 9) {

// Access data in array.

if (SafeArrayAccessData(vResult.parray, (void HUGEP* FAR*)&pData) == S_OK) {

// Retrieve elements.

// pData[0], pData[1], pData[2], etc.

// Release array

SafeArrayUnaccessData(vResult.parray);

} } } }

// Clear variant to free memory.

VariantClear(&vResult);

// Release object.

Generator.ReleaseDispatch();

}

See Also

Using the COM Interface

MS Visual Basic Sample Code

MS Excel VBA Sample Code

VBScript Web Password Sample Code