Hi,
I have a DLL which has a class containing static members. In the
implementation file of the class I have done proper initialization of
the static data. These static data are public thus I want to modify
these public static data from another application using this DLL.

Somehow the static initialization is not happening if it is done in the
implementation file and while accessing the storage for static data my
VC7 compiler complains that I am trying to write to an illegal memory.
But if I do the static initialization in the header file itself then I
get no run time error. As far as I know static data initialization
should not be done in header file. So really my question is why is this
happening in the first place and what I need to do to fix it?

DLL CODE:
///////////////////////////////Header
file//////////////////////////////////////////
typedef void (*fptr)(void);

class DllMethods
{
public:
__declspec(dllexport) static int data;
__declspec(dllexport) static fptr function;
};

/////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////Implementation
file//////////////////////////////
#include "DllMethods.h"

//IF THIS INITIALIZATION IS DONE IN THE HEADER FILE FOR THE DLL
//THEN I GET NO ERROR WHEN DEBUGGING WITH VC7 COMPILER
fptr DllMethods::function = 0;
int DllMethods::data = 46;
///////////////////////////////////////////////////////////////////////////////////////

APPLICATION CODE:
#include "DllMethods.h"

#include
using namespace std;

static void anotherFunction(void)
{
cout << "Inside anotherFunction" << endl;

}

int main(int argc, char ** argv)
{
//I GET RUNTIME ERROR WHEN THE FOLLOWING LINE
//EXECUTES AND WHEN THE STATIC INITIALIZATION IS
//DONE IN THE IMPLEMENTATION FILE
//BUT IF THE INITIALIZATION IS DONE IN THE HEADER ITSELF
//THEN I GET NO ERROR

//ASSIGN THE FUNCTION PTR OF THE DLL TO THE FUNCTION
//IN THE APPLICAITON
DllMethods::function = anotherFunction;

DllMethods::function();

return 0;
}

Thanks,
Divick