I've create a simple MFC dll to wrap some more complex C++ code that was
supplied to me. I want to call the methods in my MFC wrapper dll from my C#
app.
I have a very simple case where this problem happens.
Here is the C++ code:
Code:
class CBTPreviewLibApp : public CWinApp
{
public:
CBTPreviewLibApp();
virtual BOOL InitInstance();
__declspec(dllexport) HANDLE GetPreview();
DECLARE_MESSAGE_MAP()
};
__declspec(dllexport) HANDLE CBTPreviewLibApp::GetPreview()
{
return (HANDLE)9;
}
Here is the c# code:
Code:
[DllImport("BTPreviewLib.dll")]
public static extern int GetPreview();
try
{
int handle = GetPreview();
}
catch(Exception ee)
{
Console.WriteLine(e.ToString());
}
The code executes without any errors, but when I exit the c# application I
get the following error:
---------------------------
BTPreviewDemo.vshost.exe - Application Error
---------------------------
The instruction at "0x045d2c00" referenced memory at "0x04a82290". The
memory could not be "read".
I have no idea what could be causing this, I'm not *doing* anything in the
DLL with this function call, just returning 9. I've never tried to call
unmanaged code from C# before, so this is all very new. I'm also unable to
step into the call to GetPreview() even though both the C++ dll and c#
WinForms app are in the same solution.
Anyone have any ideas for me? Anything I can try? I have checked the
exports of the MFC dll and the names aren't mangled (I'm using a DEF file)
Thanks for any help or suggestions,
Steve