Reputation:
I wan to import a DLL and use its functions but i want to assign the dll to a value so i dont end up overiding methods
Example of how I want to do it
mydll = [DllImport("MyDll.dll")]
mydll.SayHi();
// So I don't override this:
public void SayHi() { Console.WriteLine("Hello!!!"); }
Any way to acheive this?
Upvotes: 0
Views: 217
Reputation: 6659
Import the DLL in a wrapper class.
using System;
using System.Runtime.InteropServices;
// Namespace is optional but generally recommended.
namespace MyDLLImports
{
static class MyDLLWrapper
{
// Use DllImport to import the SayHi() function.
[DllImport("mydll.dll", CharSet = CharSet.Unicode)]
public static extern void SayHi();
}
}
class Program
{
static void SayHi()
{
// whatever...
}
static void Main()
{
// Call the different SayHi() functions.
MyImports.MyDLLWrapper.SayHi();
SayHi();
}
}
See the MSDN docs on the DLLImport attribute for further details. You may also need to declare calling conventions, different character encodings, the DLL's main entry point, etc.
Upvotes: 4