Csharp .Net Tutorial: Importing Dll Libraries

James Cloud
Dynamically Linked Libraries and commonly used in CSharp applications as a means to give the project more extension in its reach and functionality. Importing Dynamically Linked Libraries into your project will allow you to use the functions of third party libraries compiled as .DLL files. It will also allow you to take advantage of handy API functions included in your system Kernel and WIN32.

The DLLImport Command

If you would like to import a Dynamically Linked Library into your project you will first need to include a reference to the System Runtume Interop Services. To do this, simply place the following code above your namespaces declaraction:

using System.Runtime.InteropServices;

Now that you have referenced the proper runtime services you can import your Library. To get an idea on how to use the DLLImport function for importing DLLs review this example source code:

[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

The above code imports the MessageBox function from the user32.dll system file, allowing you to show a simple message box. To execute the newly imported code all you have to do address the function like so:

MessageBox(new IntPtr(0), "This Message was imported from the user32.dll Library", "A Message Box", 0);

This code will show a message box displaying the text, "This Message was imported from the user32.dll Library". This is just an example of one of the API functions included in your operating system. There are literally hundreds more that you might find useful for use in your application. All of them are made available by the simple DLLImport function.

Conclusion

You can import any third party CSharp library into your project using the DLLImport method, allowing you to execute any of it's functions that wish to use. You can even use some of the older API functions that are commonly used in Visual Basic 6, but are missing the newer .Net frameworks.

Published by James Cloud

I like to program and do basically anything that has to do with technology and computers.  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.