C#: Call External DLL Library

person Jason Huangfolder_open.NET, C#, Codelocal_offer, , access_time September 16, 2009

To import external DLL library file and be able to use the function in the library.
(That is the way to make use of old windows dll libraries. To use a .NET assembly you would simply add the reference to .NET assembly and add the using clause.)

You need to:

  • Add the using System.Runtime.InteropServices (this .NET class handles interoperation between libraries).
  • Before the function you declare, use [DllImport(“library.dll file name”)].
  • Then declare the function explicitly (declare the function in the library that you want to call with parameters etc).

In the below simple example. I want to use the a function called InternetGetConnectedState from the dll file wininet.dll.

using System;
using System.Collections.Generic;
using System.Text;
 
// DllImport
using System.Runtime.InteropServices; 
 
namespace DatabaseFunctions
{
	class ConnectionManager
	{
		[DllImport("wininet.dll")]
                //define the external function
		private extern static bool InternetGetConnectedState( out int connectionDescription, int reservedValue ) ;
		private bool bIsOnLine = false;
 
		public ConnectionManager()
		{
		}
 
		private bool IsOnLine()
		{
			this.bIsOnLine = queryConnetionStatus();
			return this.bIsOnLine;
		}
 
		private bool queryConnetionStatus()
		{
			int connectionDescription = 0;
			return InternetGetConnectedState(out connectionDescription, 0);
		}
 
	}
}

Comments

  1. Initially you can focus on the top 10 search results (some of them
    will be individual profiles; others will lead you to a category of professionals).
    Although, kids can be very resilient, it is evident
    that our experiences during these times can have
    life-long effects. Ixquick also includes HTTPS, which you can even use the proxy over.

    Here is my web blog search people online

  2. * Go through customer testimonials on tanning lotion websites, and ask others for their feedback, for instance, on how effective Sun Labs products are.
    Relentless was now full of “old” players who weren’t interested in playing,
    but were interested in keeping the power. “The Blacklist” also stars Diego Klattenhoff, Harry Lennix, Ryan Eggold and Ilfenesh Hadera.

    Feel free to visit my weblog: spray tanning salons In harlingen tx

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>