Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reload dll functions from another dll, C#

Tags:

c#

dll

I have 2 unmanaged dlls which have exactly same set of function (but slightly different logic).

How can I switch between these 2 ddls during runtime?

now I have:

[DllImport("one.dll")]
public static extern string _test_mdl(string s);
like image 634
Vladimir Keleshev Avatar asked Dec 13 '22 21:12

Vladimir Keleshev


2 Answers

Expanding on the existing answers here. You comment that you don't want to change existing code. You don't have to do that.

[DllImport("one.dll", EntryPoint = "_test_mdl")]
public static extern string _test_mdl1(string s);

[DllImport("two.dll", EntryPoint = "_test_mdl")]
public static extern string _test_mdl2(string s);

public static string _test_mdl(string s)
{
    if (condition)
        return _test_mdl1(s);
    else
        return _test_mdl2(s);
}

You keep using _test_mdl in your existing code, and just place the if-statement in a new version of that method, calling the correct underlying method.

like image 72
Lasse V. Karlsen Avatar answered Jan 14 '23 21:01

Lasse V. Karlsen


Define them in different C# classes?

static class OneInterop
{
 [DllImport("one.dll")]
 public static extern string _test_mdl(string s);
}

static class TwoInterop
{
 [DllImport("two.dll")]
 public static extern string _test_mdl(string s);
}
like image 36
DaveShaw Avatar answered Jan 14 '23 22:01

DaveShaw