Simple DLL giving me weird compile errors
I am creating my 1st DLL. I have just a singleton class & a LRESULT CALLBACK function that I will create in the DLL & import into one of my projects. My MSVC++ project architecture consists of the DLLMain.cpp file(unaltered), a header file that defines the singleton class & LRESULT function & a cpp file that implements the LRESULT function.
My Problem: the project is not compiling. I have 2 compile errors that I dont understand whats exactly wrong & how to fix it.
1>c:\users\testcreatedll\dlltest.h(15): error C2059: syntax error : '__declspec(dllexport)'
1>c:\users\testcreatedll\dlltest.h(39): error C2065: 'TestWndProc' : undeclared identifier
My header file:
#ifndef DLLTEST_H
#define DLLTEST_H
#include <windows.h>
// This is from a tutorial I am following
#ifdef _CLASSINDLL
#define CLASSINDLL_CLASS_DECL __declspec(dllexport)
#else
#define CLASSINDLL_CLASS_DECL __declspec(dllimport)
#endif
namespace MyTest
{
LRESULT CALLBACK CLASSINDLL_CLASS_DECL TestWndProc( HWND hwnd, UINT msg, LPARAM lParam, WPARAM wParam );
class CLASSINDLL_CLASS_DECL TestClass
{
// Singleton class
public:
static bool testStaticVar;
static TestClass* getInstance()
{
if ( instance == NULL ) { instance = new TestClass(); }
return instance;
}
void add()
{
myMember++;
}
private:
static TestClass* instance;
WNDPROC myProc;
int myMember;
TestClass() : myMember(0) { myProc = (WNDPROC)&TestWndProc; }
~TestClass() {}
};
}
#endif // DLLTEST_H
My cpp file:
#include "stdafx.h"
#include "DLLTest.h"
namespace MyTest
{
// Create/Initialise? Class Static variables
bool TestClass::testStaticVar = false;
TestClass* TestClass::instance 开发者_运维技巧= NULL;
LRESULT CALLBACK TestWndProc( HWND hwnd, UINT msg, LPARAM lParam, WPARAM wParam )
{
switch (msg)
{
case WM_CREATE:
{
}
break;
default:
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}
C++ compilers can be very picky about the order in which you declare the calling convention and storage-class information(export visibily with __declspec
). AFAIK, VC++ needs the calling convention to appear after the storage-class. For example:
namespace MyTest
{
LRESULT CLASSINDLL_CLASS_DECL CALLBACK TestWndProc( HWND hwnd, UINT msg, LPARAM lParam, WPARAM wParam );
// ...
}
C++ Builder 2007 and MinGW-GCC-4.5.2, on the other-hand, doesn't care about this -- both forms are accepted.
精彩评论