I'm trying to extend and experiment with the ffi_test example, which
demonstrates how to create a managed instance within Rotor from unmanaged
code; I've reproduced the .cpp code here[1] for reference.
So I take the first section of code, to do the ClrCreateManagedInstance(),
and I change it to do one of my own types:
hr = ClrCreateManagedInstance(
L"Personal.Hello,Hello,PublicKeyToken=2087f203b8e16004",
//L"System.Object,mscorlib,PublicKeyToken=b03f5f7f11d50a3a",
//L"System.Random,mscorlib,PublicKeyToken=b03f5f7f11d50a3a",
IID_IManagedInstanceWrapper,
(void**)&pWrap);
if (FAILED(hr)) {
fprintf(stderr, "ClrCreateManagedInstance failed with hr=0x%08x\n",
hr);
return 2;
}
When I execute this code using the Personal.Hello type as the target (where
Personal.Hello is a strongly-named assembly installed in the GAC), I trip an
assertion failure:
C:\Prg\Rotor\sscli\tests\dev\host_test>rotor_x86\host.exe
Assert failure(PID 2296 [0x000008f8], Thread: 2308 [0x904]):
!m_fPreemptiveGCDisabled
File: r:\rotor\19jun2002\sscli\clr\src\vm\threads.h, Line: 899 Image:
C:\Prg\Rotor\sscli\tests\dev\host_test\rotor_x86\host.exe
Whereas running with the Microsoft types (System.Object or System.Random)
offers up no such assertion. Anybody got any ideas what's going on here, or
what I'm doing wrong? (I'm hoping this is a quick-answer--I've not dug into
the ClrCreateInstance impl to try and debug it.)
Ted Neward
{.NET || Java} Course Author & Instructor, DevelopMentor
(http://www.develop.com)
http://www.javageeks.com/tneward
http://www.clrgeeks.com/tneward
[1] from sscli/tests/dev/ffi_test/ffitest.cpp
#include "windows.h"
#include "stdlib.h"
#include "cor.h"
#include "mscoree.h"
#include "corffi.h"
int __cdecl main(int argc, char ** argv, char ** envp)
{
IManagedInstanceWrapper *pWrap;
VARIANT RetVal;
HRESULT hr;
if (!PAL_RegisterLibrary(L"rotor_palrt") ||
!PAL_RegisterLibrary(L"sscoree")) {
fprintf(stderr, "PAL_RegisterLibraryW failed\n");
return 1;
}
hr = ClrCreateManagedInstance(
L"System.Random,mscorlib,PublicKeyToken=b03f5f7f11d50a3a",
IID_IManagedInstanceWrapper,
(void**)&pWrap);
if (FAILED(hr)) {
fprintf(stderr, "ClrCreateManagedInstance failed with hr=0x%08x\n",
hr);
return 2;
}
VariantClear(&RetVal);
hr = pWrap->InvokeByName(
L"Next", CorFFIInvokeMethod, 0, NULL, &RetVal);
if (FAILED(hr)) {
fprintf(stderr, "InvokeMethodByName failed with hr=0x%08x\n", hr);
return 3;
}
if (V_VT(&RetVal) != VT_I4) {
fprintf(stderr, "Invalid return type (%d)\n", V_VT(&RetVal));
return 3;
}
printf("System.Random.Next() returned %d\n", V_I4(&RetVal));
pWrap->Release();
return 0;
}