InteropTest.h
extern "C" __declspec(dllexport) char * HelloFromDll();
InteropTest.cpp
#include "stdafx.h"
#include "InteropTest.h"
extern "C" __declspec(dllexport) char * HelloFromDll()
{
char *result;
result = "Hello from my DLL";
return result;
}
And finally my C# code (.net 4.5 console program):
Program.cs
class Program
{
[DllImport(@"InteropTest.dll")]
[return:MarshalAsAttribute(UnmanagedType.LPWStr)]
private static extern string HelloFromDll();
//[return:System.Runtime.InteropServices.MarshalAsAttribute(UnmanagedType.LPWStr)]
static void Main(string[] args)
{
try
{
string strRetVal = HelloFromDll();
Console.WriteLine("Returned string: {0}", strRetVal);
}
catch
{
}
Console.ReadLine();
}
}
The program fails on the dll call. It throws an accessviolationexception but hangs forever on that.






