Hi all,
i have a managed c++ dll using both managed and unmanaged code and an
unmanaged application that calls the unmanaged code of the managed dll.
I did follow http://support.microsoft.com/kb/814472/en-us but still the
application crashs before the first call of the unmanaged code.
MainFrame.cpp: (unmanaged application)
bool WillUseManagedWrapper()
{
// Snippet 1
typedef void (__stdcall *MixedModeBrokenInit)();
typedef void (__stdcall *pfnEnsureInit)(void);
// ... initialization code
HMODULE hDll = ::GetModuleHandle("ManagedWrapper.dll");
if(hDll)
{
MixedModeBrokenInit pfnDll =
(MixedModeBrokenInit)::GetProcAddress(hDll, "EnsureInitManagedWrapper");
if(pfnDll)
{
pfnDll();
}
else
return false;
}
else
{
DWORD errCode = ::GetLastError();
return false;
}
// ... more initialization code
return true;
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
WillUseManagedWrapper();
Unmanaged::JustAClass cppWrapp("mönötsched",54);
....
}
MixedModeIsBroken.h: (from the mixed mode dll)
#ifndef PANDATEST_MIXEDMODEISBROKEN_H_INC_
#define PANDATEST_MIXEDMODEISBROKEN_H_INC_
#pragma once
#ifdef MANAGEDWRAPPER_SELF
//AFX_CLASS_EXPORT
#define MANAGEDWRAPPER_API __declspec(dllexport)
#else
//AFX_CLASS_IMPORT
#define MANAGEDWRAPPER_API __declspec(dllimport)
#endif
MANAGEDWRAPPER_API void __cdecl EnsureInitManagedWrapper();
MANAGEDWRAPPER_API void __cdecl ForceTermManagedWrapper();
#endif
MixedModeIsBroken.cpp:
// This is the main DLL file.
#include "stdafx.h"
#define MANAGEDWRAPPER_SELF
#include "MixedModeIsBroken.h"
#include <windows.h>
#include <_vcclrit.h>
// Call this function before you call anything in this DLL.
// It is safe to call from multiple threads; it is not reference
// counted; and is reentrancy safe.
void __cdecl EnsureInitManagedWrapper()
{
// Do nothing else here. If you need extra initialization steps,
// create static objects with constructors that perform
initialization.
__crt_dll_initialize();
// Do nothing else here.
}
// Call this function after this whole process is totally done
// calling anything in this DLL. It is safe to call from multiple
// threads; is not reference counted; and is reentrancy safe.
// First call will terminate.
void __cdecl ForceTermManagedWrapper(void)
{
// Do nothing else here. If you need extra terminate steps,
// use atexit.
__crt_dll_terminate();
// Do nothing else here.
}
ManagedWrapper.h (from the mixed mode dll)
#include <string>
#include "MixedModeIsBroken.h"
namespace Unmanaged
{
class MANAGEDWRAPPER_API JustAClass
{
public:
JustAClass(std::string const& s, int i);
};
}
ManagedWrapper.cpp
// This is the main DLL file.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
#define MANAGEDWRAPPER_SELF
#include "ManagedWrapper.h"
namespace ManagedWrapper{
public __gc class JustAClass
{
public:
JustAClass(String* s, Int32 i);
};
}
ManagedWrapper::JustAClass::JustAClass(String* s, Int32 i)
{
}
Unmanaged::JustAClass::JustAClass(std::string const& s, int i)
{
}
Regards, Arne