tags:

views:

37

answers:

1

I want to have a static global std::unordered_map in the cpp of my entry point for my COM server.

relevant header code:

typedef unordered_map<HWND,IMyInterface*> MyMapType;

relevant body:

static MyMapType MyMap;

void MyFunction(HWND hWnd, IMyInterface* pObj){
    MyMap[hWnd] = pObj;
}

HINSTANCE g_hInstModule = NULL;
BOOL WINAPI DllMain ( __in HINSTANCE hInstDLL, __in DWORD fdwReason, __in LPVOID lpvReserved )
{
    if( fdwReason == DLL_PROCESS_ATTACH )
    {
        g_hInstModule = hInstDLL;
        return true;
    }
    else if( fdwReason == DLL_PROCESS_DETACH )
    {
        return true;
    }
    return false;
}

MyCoClass::MyCoClass()
{
    DRM_Refcount = 1;
}

HRESULT STDMETHODCALLTYPE MyCoClass::InitMyCoClass()
{
    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
    //replace with make window code
    MyFunction(hWnd,ISomeInterface);
    return S_OK;
}

The only way I can get this to work is be making a map_type pointer and creating an instance of map_type on the heap and pointing at it with the global pointer. :/

WHY?

A: 

You need to modify DllMain to explicitly initialize the C runtime: http://support.microsoft.com/kb/814472

Search for "To Modify COM-based DLL"

Static objects with constructors usually get initialized through the CRT entry point which for .exes then calls your program's main function. With DLLs, you have to the work yourself.

MSN
Thank you .....
PrettyFlower