// A program to use a run-time loaded DLL.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;
//Creates a new type of variable which is a function pounter called, cryptFunPointer.
typedef void (*cryptFunPointer) (int key , char inString, char outString, int length);
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hinstLib;
const int SIZE = 20;
char theString [SIZE]; // entered by the user
char theEncryptedString [SIZE]; // after encryption
char theDecryptedString [SIZE]; // after decryption – should be the same as theString
cryptFunPointer encryptFunPointer, decryptFunPointer;//Declares two variables of the type cryptFunPointer
cout << "Enter a string: ";
cin.getline(theString,SIZE);
// Get a handle to the DLL module.
hinstLib = LoadLibrary(TEXT("dynamicLib.DLL"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL){
encryptFunPointer = (cryptFunPointer) GetProcAddress(hinstLib, "encrypt");
decryptFunPointer = (cryptFunPointer) GetProcAddress(hinstLib, "decrypt");
}
else{
cout<<"Error DLL not found" <<endl;
}
// If the function address is valid, call the function.
if (encryptFunPointer != NULL){
(encryptFunPointer) (3, theString[SIZE], theEncryptedString[SIZE], SIZE);
// cout << endl << "Encrypted: " << theEncryptedString;
}
else{
cout<<"Error NULL pointer"<<endl;
}
system ("PAUSE");
return 0;
}
The program compiles and runs just fine but i get the Error NULL pointer out put on the screen.
It's basically a program that takes a string passes it to a run time loaded dll that then carrys out a very basic encryption.
I think GetProcAddress for encryptFunPointer isn't finding the encrypt functions address in the DLL. This is probably to do with how I define the typedef as it's supposed to take two char arrays as an argument.
Thanks for any help in advance.