Search code examples
c#c++dlldllimportmanaged

C# DLLImport for C++ dll


I have a .dll built in C++/CLI and .NET. Therefore, it is targeted to .NET applications. The API is a set of wrappers that uses managed types so it is NET native. I have imported the .dll and added a function like:

[DllImport(@"maplib.dll")]
public static extern bool initialize(string a);

When I call this function in my C# code, it works fine, but if I want to add another function, like..

[DllImport(@"maplib.dll")]
public static extern bool initialize(string a);
public static extern bool runfile(string a, string b);

I get this error when I run my program. It is related to the second function:

"Could not load type 'myapp.main' from assembly 'myapp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'runfile' has no implementation (no RVA)."

Why does this error occur and how can I fix it?


Solution

  • You must add the DllImport attribute twice if you want two functions:

    [DllImport(@"maplib.dll")]
    public static extern bool initialize(string a);
    [DllImport(@"maplib.dll")]
    public static extern bool runfile(string a, string b);
    

    But if your dll is .NET then why not just add a regular reference to it and use it like you would use C# code?