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 WillUseManagedW rapper()
{
// Snippet 1
typedef void (__stdcall *MixedModeBroke nInit)();
typedef void (__stdcall *pfnEnsureInit) (void);
// ... initialization code
HMODULE hDll = ::GetModuleHand le("ManagedWrap per.dll");
if(hDll)
{
MixedModeBroken Init pfnDll =
(MixedModeBroke nInit)::GetProc Address(hDll, "EnsureInitMana gedWrapper");
if(pfnDll)
{
pfnDll();
}
else
return false;
}
else
{
DWORD errCode = ::GetLastError( );
return false;
}
// ... more initialization code
return true;
}
int CMainFrame::OnC reate(LPCREATES TRUCT lpCreateStruct)
{
WillUseManagedW rapper();
Unmanaged::Just AClass cppWrapp("mönöt sched",54);
....
}
MixedModeIsBrok en.h: (from the mixed mode dll)
#ifndef PANDATEST_MIXED MODEISBROKEN_H_ INC_
#define PANDATEST_MIXED MODEISBROKEN_H_ INC_
#pragma once
#ifdef MANAGEDWRAPPER_ SELF
//AFX_CLASS_EXPOR T
#define MANAGEDWRAPPER_ API __declspec(dlle xport)
#else
//AFX_CLASS_IMPOR T
#define MANAGEDWRAPPER_ API __declspec(dlli mport)
#endif
MANAGEDWRAPPER_ API void __cdecl EnsureInitManag edWrapper();
MANAGEDWRAPPER_ API void __cdecl ForceTermManage dWrapper();
#endif
MixedModeIsBrok en.cpp:
// This is the main DLL file.
#include "stdafx.h"
#define MANAGEDWRAPPER_ SELF
#include "MixedModeIsBro ken.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 EnsureInitManag edWrapper()
{
// Do nothing else here. If you need extra initialization steps,
// create static objects with constructors that perform
initialization.
__crt_dll_initi alize();
// 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 ForceTermManage dWrapper(void)
{
// Do nothing else here. If you need extra terminate steps,
// use atexit.
__crt_dll_termi nate();
// Do nothing else here.
}
ManagedWrapper. h (from the mixed mode dll)
#include <string>
#include "MixedModeIsBro ken.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(Stri ng* s, Int32 i);
};
}
ManagedWrapper: :JustAClass::Ju stAClass(String * s, Int32 i)
{
}
Unmanaged::Just AClass::JustACl ass(std::string const& s, int i)
{
}
Regards, Arne