Hoping some brighter minds than mine can shed some light on this. Here is the situation:
MyApp.exe is a C console app that loads a CPP DLL: MyDll.dll (via LoadLibrary). MyApp then calls a routine in MyDll that then loads and calls a C#.Net Assembly: MyAssembly.dll.
It all works just fine when MyApp and MYDll are built as Win32 (MyAssembly is built as Any). However when MyApp and Mydll are built as x64, MyDll gets exception 0xE0434F4D when trying to load MyAssembly. I even tried building MyAssembly as x64 with the same result.
I am using Visual Studio 2010. The code is below. Any suggestions would be appreciated.
Code for Myll.cpp
// MyDdll.CPP
#pragma once
#include <windows.h>
#include <stdio.h>
using namespace System;
using namespace System::IO;
using namespace System::Reflection;
namespace MyNamespace
{
PWCHAR asmbly = L"C:\\mydir\\myassembly.dll";
#ifdef _WIN64
#pragma comment(linker,"/EXPORT:MyFunction=MyFunction")
#else
#pragma comment(linker,"/EXPORT:MyFunction=_MyFunction@8")
#endif
extern "C" int __stdcall MyFunction(PCHAR stringParameter, INT intParameter)
{
__try
{
// ===> next line generates exception 0xE0434F4D but only in x64 mode
Assembly^ a = Assembly::LoadFrom(gcnew String(asmbly));
Type^ type = a->GetType("MyNamespace.MyClass");
MethodInfo^ method = type->GetMethod("MyFunction");
Object^ obj = Activator::CreateInstance(type);
array<Object^>^ params = gcnew array<Object^>(2)
{
gcnew String(stringParameter),
intParameter
};
Object^ ret = method->Invoke(obj, params);
errnum = (int)ret;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
errnum = GetExceptionCode();
}
return (errnum);
}
}
Code for MyAssembly.CS
// MyAssembly.CS
using System;
using System.Collections.Generic;
namespace MyNamespace
{
public class MyClass
{
public int MyFunction(string stringParam, int? intParam)
{
int result = 0;
return result;
}
}
}
User contributions licensed under CC BY-SA 3.0