tags:

views:

68

answers:

4

Hi, I have 2 unmanaged dlls wich have exactly same set of function (but slighly 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);
+4  A: 

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);
}
DaveShaw
Yes, you would need two classes.
sbenderli
You can set the `EntryPoint` property in the `DllImport` attribute if you wanted to keep them in the same class.
Stephen Cleary
i thought about it, but he problem is that I use _test_mdl() alot in the project, and if I make it like in your example, I will need to change code and use if-statement for every call of _test_mdl
Halst
+4  A: 

I haven't ever had to use this, but I think the EntryPoint can be specified in the declaration. Try this:

    [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);

DllImportAttribute.EntryPoint Field

Eric Dahlvang
+4  A: 

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.

Lasse V. Karlsen
that should kinda work, but I'll need to wrap around all the functions form dlls :(
Halst
+1  A: 

You could still use dynamic loading and call LoadLibraryEx (P/Invoke), GetProcAddress (P/Invoke) and Marshal.GetDelegateForFunctionPointer (System.Runtime.InterOpServices).

;)