Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / January 2005

Tip: Looking for answers? Try searching our database.

Trying to call DLL from C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ken Beauchesne - 04 Jan 2005 17:32 GMT
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());
> > >  }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.