I am trying to create a simple DLL and call it from C#, I have not created a
dll before
and this one gives me a missing method exception when the Message function
is called
in C#. I can invoke system dll's no problem but I need to create some
custom ones so
could someone give me a simple example of what to put in the c++ side
Thanks
Ken B
#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
extern "C" __declspec(dllexport) void __stdcall Message(LPCTSTR p_szMessage)
{
MessageBox(NULL,LPCTSTR(p_szMessage),LPCTSTR("Mesage from DLL"),MB_OK);
}
C#-----------------------
[DllImport("MyDll.dll")]
static extern void Message(string msg);
try
{
Message("Hello DLL WORLD");
}
catch(DllNotFoundException g)
{
Console.WriteLine(g.ToString());
}
catch(EntryPointNotFoundException g)
{
Console.WriteLine(g.ToString());
}
Phil Wilson - 04 Jan 2005 21:52 GMT
It's probably a name mangling thing - exporting the C++ function with a .DEF
file should resolve it.

Signature
Phil Wilson [MVP Windows Installer]
----
> I am trying to create a simple DLL and call it from C#, I have not created a
> dll before
[quoted text clipped - 40 lines]
> Console.WriteLine(g.ToString());
> }
Ken Beauchesne - 05 Jan 2005 15:38 GMT
using dumpbin the export shows 1 function with the name of Message so its
not a name
thing, it must be something more simple.
Ken
> It's probably a name mangling thing - exporting the C++ function with a .DEF
> file should resolve it.
[quoted text clipped - 44 lines]
> > Console.WriteLine(g.ToString());
> > }
Phil Wilson - 05 Jan 2005 20:44 GMT
Assuming you've got the called DLL in the folder with your C# program, it
seems to work. This code works for me:
using System;
using System.Runtime.InteropServices;
namespace CallDll
{
public class SomeDll
{
[DllImport("Calling.dll")]
public static extern void Message(string msg);
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
SomeDll.Message("Hello DLL WORLD");
}
catch(DllNotFoundException g)
{
Console.WriteLine(g.ToString());
}
catch(EntryPointNotFoundException g)
{
Console.WriteLine(g.ToString());
}
}
}
}

Signature
Phil Wilson [MVP Windows Installer]
----
> using dumpbin the export shows 1 function with the name of Message so its
> not a name
[quoted text clipped - 53 lines]
> > > Console.WriteLine(g.ToString());
> > > }