Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / C#

Reusing Legacy DLLs in C#

Rate me:
Please Sign up or sign in to vote.
4.48/5 (28 votes)
18 Nov 20026 min read 193.6K   2.2K   74  
This article gives you a way to reuse existing code, without rewriting it to .NET
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class MyDLL {
		[DllImport("legacy",EntryPoint = "MyAppend",ExactSpelling = true,CharSet = CharSet.Ansi,CallingConvention = CallingConvention.Cdecl)]
		public static extern unsafe IntPtr Append(
			IntPtr ptr,
			[MarshalAs(UnmanagedType.LPStr)]
			string arg1,
			[MarshalAs(UnmanagedType.LPStr)]
			string arg2,
			[MarshalAs(UnmanagedType.LPStr)]
			string arg3, bool isLast);

		[DllImport("legacy",EntryPoint = "KillBuffer",ExactSpelling = true,CharSet = CharSet.Ansi,CallingConvention = CallingConvention.Cdecl)]
		public static extern unsafe void StringDispose(IntPtr ptr);

	}
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			//
			// TODO: Add code to start application here
			//
			IntPtr target = IntPtr.Zero;
			target = MyDLL.Append(target,"xx","yy","zzz",false);
			target = MyDLL.Append(target,"xx","yy","zzz",false);
			target = MyDLL.Append(target,"xx","yy","zzz",true);
			Console.WriteLine("Returned: {0}",Marshal.PtrToStringAnsi(target));
			MyDLL.StringDispose(target);
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Romania Romania
Student in last year at Faculty of Automatic Control and Computers of Polytechnical University Bucharest.
I like programming a lot, working with many languages like C++, VB, VB.Net, C#.
Now I'm working at United Management Technologies Romania in C++.

Comments and Discussions