Click here to Skip to main content
12,354,630 members (59,926 online)
Click here to Skip to main content
Articles » Languages » C# » PInvoke » Downloads

Stats

520.3K views
5.2K downloads
97 bookmarked
Posted

Late binding on native DLLs with C#

, 12 Nov 2001 BSD
Delaying which DLL export to call until runtime is not possible with C#. This article shows you how to make it possible.
article_src
makefile
using System;
using System.Runtime.InteropServices;

namespace DynamicInvocationTest
{
	class Class1{
		[DllImport("kernel32")]
		public extern static int LoadLibrary(string lpLibFileName);
		[DllImport("kernel32")]
		public extern static bool FreeLibrary(int hLibModule);
		[DllImport("kernel32", CharSet=CharSet.Ansi)]
		public extern static int GetProcAddress(int hModule, string lpProcName);
/*
		[DllImport("msjava", CharSet=CharSet.Unicode)]
		public extern static int call(int funcptr, int hwnd, string message, string title, int flags);
*/
		[DllImport("Invoke", CharSet=CharSet.Unicode)]
		public extern static int InvokeFunc(int funcptr, int hwnd, string message, string title, int flags);


		static void Main(string[] args)	{
			int hmod=LoadLibrary("User32");
			int funcaddr=GetProcAddress(hmod, "MessageBoxW");
			int result=InvokeFunc(funcaddr, 0, "Hello World", ".Net dynamic export invocation", 1 /*MB_OKCANCEL*/);
			Console.WriteLine("Result of invocation is " + result);
			
			FreeLibrary(hmod);

			Console.WriteLine("Press any key to continue...");
			Console.ReadLine();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License

Share

About the Author

Richard Birkby
Web Developer
United Kingdom United Kingdom
Richard Birkby is a software engineer from London, UK, specializing in .Net. Richard has coded for many different sized companies from small venture-capital funded start-ups, to multi-national corporations (ie Microsoft). When he's not programming, he enjoys driving his sports car or eating curry (although never at the same time!).

Richard helps run CurryPages.com and has several other covert ventures in development. Stay tuned!

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 13 Nov 2001
Article Copyright 2001 by Richard Birkby
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid