Instant Protection PLUS 3 DLL C++ CLR (Late Binding) Sample

This is a guide which shows you the steps taken to create the C++ CLR Late Binding (also known as Explicit Linking) example for the Instant Protection PLUS 3 DLL. If you are unsure about the differences between early and late binding, please view the Samples Overview for an overview of the differences.

Step 1 - Creating a new Project

For integration with an existing project you may skip this step and proceed to Step 2.

Beginning with Visual Studio 2013, the CLR Windows Forms project templates have been removed from the project wizard, therefore some extra steps are shown in this tutorial to create a Windows Forms application in C++ CLR.

Open Visual Studio, click File, then New, and then Project... Select the Visual C++ CLR project type on the left, and then select CLR Empty Project on the right.

Add a Windows Form to the project by selecting Project/Add New Item from the menu.

In the Add New Item dialog select Visual C++/UI from the left tree and select Windows Form from the center.

Once Visual Studio adds the new files you will probably see an error if viewing the new MyForm.h file in Design mode. This is normal, and will be fixed in the following steps.

An entry point for the application will need to be added. For simplicity we will add this entry point class to the MyForm WinForm class already created.

In Solution Explorer right-click on MyForm.h and select View Code from the pop-up menu.

In the MyForm.h code add the following code after the MyForm class and before the final bracket so it is still in the namespace.

C/C++
ref class Program
{
public:

Program::Program() {}

static void Program::Main()
{
System::Windows::Forms::Application::EnableVisualStyles();
System::Windows::Forms::Application::Run(gcnew MyForm);
}

};

Your code should look similar to the code below.

Tell Visual Studio what type of SubSytem to use by selecting from the menu Project/Project Properties.

Select Linker/System on the left . Click on the SubSystem editbox on the right and click the drop-down arrow. Select Windows (/SUBSYSTEM:WINDOWS) from the list.

Leave the properties dialog open and select Linker/Advanced on the left. On the right side in the Entry Point field enter Program::Main and click OK.

Now build the application and make sure it runs. It should show a blank Window.

Step 2 - Configuring Project Settings

Some project settings need to be changed so that required files may be found. Open the project settings by clicking the Project menu and clicking Properties as shown below.

Select "All Configurations" from the drop-down menu at the top of the project settings dialog. Then, on the left, select the Configuration Properties > C/C++ > General section.

Add the include directory so that Visual C++ may find the required include file for the Instant Protection PLUS 3 DLL. In the installation directory (which is C:\Program Files\SoftwareKey by default), this is located in the "Instant PLUS\samples\IP2Lib DLL\Include" subdirectory. Add the appropriate path to "Additional Include Directories" as shown below.

There is no need to set up the import library as Late Binding does not require this.

Step 3 - Source code modifications

The first source code change required is to include the required header files in the appropriate source file. This change should be implemented in the file which either has the source code for starting your application up or your application's main dialog. For this example, the MyForm.h file is where we add the changes. At the top of the file, add the following includes statements after the #pragma once statement if present:

C/C++
#include<Windows.h>
#include "IP2Lib.h"

There are two more namespaces to add. Add the following right after the current list of namespaces being used.

C/C++
using namespace System::IO;
using namespace System::Runtime::InteropServices;

The code we will be integrating will be added to the MyForm class and executed at initialization in our window's Load event. We will need to add this event to the WinForm class.

In the Solution Explorer double-click the MyForm.h file or right-click and select View Designer from the menu.

In the designer window right-click on the MyForm window and select Properties from the menu.

This will bring up the properties for the MyForm window. In the Properties window at the top click the even button (small lightening bolt) to show the available events.

In the list of events shown, double-click on the Load event name. Visual Studio will generate the Load event code for us. In the MyForm.h code will be a new method to handle the Load event called MyForm_Load.

You will now need to generate the code to use with your application. You can obtain the CPP CLR code shown below with the return key/return code and Decryption Key specific to your .ipp file already in place by using the Instant Protection PLUS 3 DLL Integration Assistant. To use this method simply: Run Instant Protection PLUS 3 DLL Integration Assistant. Select the language that your application is coded in (in this sample we are using CPP CLR-LateBinding). Select your applications .ipp file. Click "Generate", select all the text in the code window then click "Copy" to save the code to your clipboard. Here is a sample of what the interface should look like.

The next step is to add the code into the body of the MyForm_Load method we added earlier.

Once you have this code in place you may proceed to step 4.

As an alternative to using the Instant Protection PLUS 3 DLL integrationAssistant you can copy the code below and paste it into the dialog's OnInitDialog method as shown above. If you choose to copy the sample code below be sure to follow the important steps to make the appropriate changes.

C/C++
//Beginning of Instant Protection PLUS 3 Sample code
Boolean successful = false;
INT result = 0;
PCALLIPEX p;
HMODULE hLib = NULL;
IntPtr path = IntPtr::Zero;
const char *key = [key];

do
{
//Initialize the path
path = Marshal::StringToHGlobalAnsi(Path::Combine(Path::GetDirectoryName(Application::ExecutablePath), [xmlName]));
if (IntPtr::Zero == path)
{
MessageBox::Show(this, "Unable to allocate memory.", "Error", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
this->Close();
break;
}

//Load the Instant Protection PLUS 3 DLL
if (IntPtr::Size == 4)
hLib = LoadLibrary(L"IP2Lib32.dll");
else if (IntPtr::Size == 8)
hLib = LoadLibrary(L"IP2Lib64.dll");
if (NULL == hLib)
{
MessageBox::Show(this, "Unable to load a required dependency.", "Error", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
this->Close();
break;
}

//Get the pointer to the function for calling Instant Protection PLUS 3
p = (PCALLIPEX)GetProcAddress(hLib, "CallIpEx");
if (NULL == p)
{
//if we didn't get it, free the library and show an error message
MessageBox::Show(this, "Unable to load a required dependency.", "Error", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
break;
}

result = (*(PCALLIPEX)p)((PPHWND)static_cast<void*>(this->Handle.ToPointer()), IP2LIB_FLAGS_NONE, (LPCSTR)key, static_cast<char*>(path.ToPointer()), NULL, NULL, NULL, 0, 0, 0);

//if the XML file was not found, show an error message
if (IP2LIB_RESULT_FILE_NOT_FOUND == result)
{
MessageBox::Show(this, "The application encountered an error during start-up. This error occurred because a file the program requires to run was not present. Please contact technical support for assistance.", "Start-up Error", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
break;
}
else if (IP2LIB32_ERROR_INCOMPATIBLE_FILE_VERSION == result)
{
MessageBox::Show(this, "Incompatible XML file version", "Start-up Error", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
break;
}

successful = ([exp] == result);
} while (false);

//Free unmanaged/native resources...
if (IntPtr::Zero != path)
{
Marshal::FreeHGlobal(path);
path = IntPtr::Zero;
}
if (NULL != hLib)
{
FreeLibrary(hLib);
hLib = NULL;
}

//Close if there was a problem or if Instant Protection PLUS 3 returned false.
if (false == successful)
this->Close();
Important Steps

Step 4 - Compile and Run

The source code modifications have been made, the required project property changes have been made, and the only thing left to do is to compile and run. If this is the first time you are building your project, you can build the project first. Once you have successfully built, make sure you copy the XML file you saved from the Instant Protection PLUS 3 wizard's File Output step to the appropriate build directory. For example, if you are compiling the Debug build, you will need to copy the XML file to the Debug directory where the compiled program is located. Likewise, you will also need to do this for your Release build and any other build configurations you might create. Once the XML file is in the same place as your program, you can run it and test.

Step 5 - Deploy

The application must be properly deployed in order to function correctly on a 'clean' machine that has not previously had Instant Protection PLUS 3 installed. The Instant Protection PLUS 3 DLL library files must be installed to the system directory along with any custom splash screen or product logo images. It is recommended to use a helper executable to initialize the license files to avoid permissions issues on Windows Vista and later. These requirements are described in detail in the deployment topic.