A Big Integer Package for Use in Visual Basic Written in Visual C++

WEBINAR:On-Demand

Introduction

This article is simple a 'How To' in creating a COM object in Visual C++ for use in COM-aware languages such as Visual Basic. In addition, the library that is created is a Large Integer package. The underlying cryptographic code is Wei Dia's Crypto++ (www.cryptopp.com).

Crypto++ has been NIST certified for those who are interested in authoring FIPS 140-2 conformant software. To enjoy the Certification, one must use the DLL version of the Crypto++ Library.

Using the ATL Wizard to Create the COM DLL

Open Visual C++ and select New ATL COM AppWizard Project.

Next, click 'OK'. At the final step, select a DLL, and allow merging of the Proxy/Stub code. At this point, the Wizard has created the framework of the COM object. If you want familiar Visual C++ favorites such as CString, check the 'Support MFC' box.

A Big Integer Package for Use in Visual Basic Written in Visual C++

WEBINAR:On-Demand

The Code

The ATL project now has two separate entities: an interface (that the COM client will use), and a class file (that will hold various functions and a member variable). Methods and Properties will be added to the Interface (IBigInteger), and Functions and Variables will be added to the class file (CBigInteger).

The files of interest that will now be used the most are BigInteger.h, BigInteger.cpp, and tt>Integer.idl. The MIDL compiler will use Integer.idl to create the export table of the object (similar to a self-contained DEF file).

This widget requires JavaScript to run. Visit Site for more...
Add a private member variable to the CBigInteger class: Select the 'ClassView' tab in the Visual C++ editor, and then right-click on the CBigInteger and Select 'Add Member Variable'.

[add_member.gif]

Add the private m_Integer as shown below. If you are unable to add it as a CryptoPP::Integer, add it as an INT, and then edit the type in the BigInteger.h header file. Note that, when working with interfaces, you will not be able to access this member directly.

[integer_member.gif]

Next, add a Property to the interface. Right-click the IBigInteger interface and select 'Add Property'.

[add_property.gif]

The hidden property _Value will be used to access the underlying BigInteger. This is the same property that Visual Basic will call to get the tool tip for displaying the value of the BigInteger in the Visual Basic editor when hovering the mouse over a variable.

[add_property_dialog.gif]

Note the ID number (36 in the above example). If this property is not 0, edit the Integer.idl file so that it becomes ID 0. Simply sequentially renumber other IDs as required. The remaining Property and Method IDs are not of much significance.

A final note on the put/get property _Value: It is the default property of the object. This works extremely well for the BigInteger (and VB TextBoxes, for that matter). It is hidden in the Visual Basic Object Browser due to the leading underscore. Visual C++ will decorate this property to put__Value(...) and get__Value(...) in the CBigInteger class. The get__Value(...) returns a decimal string (BSTR) representation of the underlying BigInteger. The corresponding put__Value(...) accepts the decimal representation of a big integer, and sets the m_Integer member to that value.

The effect of the previous is that a programmer can perform the following:

Dim i As BigInteger
Set i = New BigInteger
' VB will set the _Value property
' VC++ will have the put__Value( BSTR ) function invoked
i = "123456789123456789123456789123456789"
' VB will get the _Value property
' VC++ will have the get__Value( ) function invoked
Debug.Print i
Set i = Nothing

Before _Value is coded, a helper function is needed to create the string. Add it as a protected member function of CBigInteger. Code it as follows. In keeping with the COM model, it will return an HRESULT. pbszValue will receive the string, and pInteger is the CryproPP::Integer that will be transformed into a human-readable digit string. Note also the call to the helper function ReverseString(...).

For example, Modulo(...) would be coded as follows. Note the [out, retval] attributes added to the method from above. out informs the MIDL compiler that the interface will be written to, and the retval attribute informs the MIDL compiler this is the return value of the function. A out value type must be of the type IBigInteger**—an in value type will be of the type IBigInteger*.

The above code is typical. Note that all functions catch throws and simply return a COM error. Never let a throw go uncaught into the COM client. The BigInteger Library will catch all errors, and return the error as an HRESULT which will be one of the following:

The Value then is written back as a BSTR using put__Value(...). Never attempt to access the m_Integer member through the interface (for example, by attempting to return a pointer to m_Integer through a method). Imagine a distributed situation where the class is instantiated on a separate machine. The CBigInteger will simply receive a bad address.

A Big Integer Package for Use in Visual Basic Written in Visual C++

WEBINAR:On-Demand

Most wrappings are straightforward from C++ function overloading. However, there are a few where one must use functions from the Crypto++ nbtheory.h and algebra.h. For example, multiplicative inverse and exponentiation. The functions are provided so the reader does not have to grep the source files with a number theoretic background:

About the Author

Jeffrey Walton

In the past, I have worked as an IT consultant for County Government (Anne Arundel County), the Nuclear Energy Institute, the Treasury Department, and Social Security Administration as a Network Engineer and System Administrator. Primary Administration experience includes Microsoft Windows and Novell Netware, with additional exposure and familiarity with Mac and Linux OSes.
Previous to the US government, I was a programmer for a small business using Microsoft Visual Languages (Basic 5.0, 6.0, and C++ 5.0, 6.0) and Scripting Languages.
An undergraduate degree (BS in Computer Science) was obtained from University of Maryland, Baltimore County. Graduate work includes a Masters of Science (Computer Science) from Johns Hopkins University (expected before 2009).
Training and Certifications include Microsoft, Checkpoint, and Cisco.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.

Thanks for your registration, follow us on our social networks to keep up-to-date