I've put together a pure C++ dll which requires a DEF file as listed below. It does the same exact processing as the previous C sample. Thus, the OP can test both the C and C++ versions. It is highly recommended that the OP overload one of the functions in the C++ version. Also, through experimentation, you can validate the statements made in this thread.
Code:
LIBRARY myclass.dll
EXPORTS
CreateMyClass @2 PRIVATE
DeleteMyClass @3 PRIVATE
GetClassMethod @4 PRIVATE
Code:
// File: myclass.h
#ifndef __MYDYNCLASS_H
#define __MYDYNCLASS_H
#include <windows.h>
#ifdef _DLL
#define _DYNAMICLINK __declspec( dllexport)
#else
#define _DYNAMICLINK __declspec( dllimport)
#endif
class _DYNAMICLINK MyClassinDLL
{
public:
MyClassinDLL ();
virtual ~MyClassinDLL();
void MyFunction(char * pIn, unsigned char * pOut );
};
typedef void ( MyClassinDLL::*PMYCLASSSYAHELLOMETHOD)(char * pIn, unsigned char * pOut);
#ifndef _DLL
typedef MyClassinDLL* ( *PFNINITIALIZEMYDLLCLASS )();
#else
_DYNAMICLINK MyClassinDLL* CreateMyClass()
{
return (new MyClassinDLL());
}
#endif
#ifndef _DLL
typedef void (*PFNDELETEMYDLLCLASS)( MyClassinDLL*);
#else
_DYNAMICLINK void DeleteMyClass (MyClassinDLL* pObj)
{
delete pObj;
}
#endif
#ifndef _DLL
typedef PMYCLASSSYAHELLOMETHOD (PFNGETCLASSMETHOD)(char * pIn, unsigned char * pOut);
#else
_DYNAMICLINK PMYCLASSSYAHELLOMETHOD GetClassMethod (char * pIn, unsigned char * pOut)
{
return (&MyClassinDLL::MyFunction);
}
#endif
#endif
Code:
// File: myclass.cpp
// Compile: cl.exe /LD /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_DLL" /D "_WINDLL" /FD /EHsc /MTd /W3 myclass.cpp /link /DLL /DEF:"myclass.def"
#include <windows.h>
#include <iostream>
#include "myclass.h"
MyClassinDLL::MyClassinDLL ()
{
std::cout << "Class Created" << std::endl;
}
MyClassinDLL::~MyClassinDLL()
{
std::cout << "Class Deleted" << std::endl;
}
void MyClassinDLL::MyFunction(char *pIn, unsigned char * pOut)
{
for(int iIndex = 0; iIndex < strlen(pIn); iIndex++)
{
pOut[iIndex] = pIn[iIndex];
pOut[iIndex]++;
}
}
Code:
// File: myapp.cpp
// Compile: cl.exe /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /EHsc /MLd myapp.cpp
#include <windows.h>
#include <iostream>
#include "myclass.h"
int main( void )
{
int iIndex;
char Input[] = {"Testing"};
unsigned char Output[128] = {0};
PFNINITIALIZEMYDLLCLASS pfnInitializeMyClass = NULL;
PFNDELETEMYDLLCLASS pfnDeleteMyClass = NULL;
PFNGETCLASSMETHOD *pfnGetClassMethod = NULL;
PMYCLASSSYAHELLOMETHOD pfnMyFunction = NULL;
MyClassinDLL* pMyClass = NULL;
HMODULE hDll = (HMODULE) INVALID_HANDLE_VALUE;
hDll = LoadLibrary( "myclass.dll" );
if( INVALID_HANDLE_VALUE == hDll )
{
std::cout << "LoadLibrary failed" << std::endl;
returnf -1;
}
pfnInitializeMyClass = ( PFNINITIALIZEMYDLLCLASS ) GetProcAddress( hDll, "CreateMyClass" );
pfnDeleteMyClass = ( PFNDELETEMYDLLCLASS ) GetProcAddress( hDll, "DeleteMyClass" );
pfnGetClassMethod =( PFNGETCLASSMETHOD * )GetProcAddress( hDll, "GetClassMethod" );
pMyClass = ( pfnInitializeMyClass ) ();
pfnMyFunction =( pfnGetClassMethod(Input, Output ));
( pMyClass->*pfnMyFunction )(Input, Output);
for(iIndex = 0; iIndex < strlen(Input); iIndex++)
std::cout << Input[iIndex] << " " << Output[iIndex] << std::endl;
( pfnDeleteMyClass( pMyClass ));
FreeLibrary ( hDll );
return 0;
}