The nightmare of importing C# DLL files

 

Hy everyone i have a bit of a problem, I program mostly in python, and it's been a while actually since i coded in C# and i code in mql every now and again. My problem is i built a C# dll file that has a couple of simple functions:

[DllExport("debugger", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static string debugger()
{
        return "Send help!! SOS";
}

[DllExport("debugger2", CallingConvention = CallingConvention.StdCall)]
public static int debugger2()
{
        return "12345";
}

This is my simple code from my C# file, it's all inside a public static class and the Solution platform is x64. Code compiles just fine and i find no issues. Now this is how my MetaEditor code looks:

#import "simpleFunctions.dll"
        string debugger();

        int debugger2();
#import

int OnInit()
{
        //Print a statement just to show where code crashes
        Print("We are definitly in it :D");
        
        Print(debugger);
}

From what the Journal tell me everything runs just fine, the file is added from libraries and the print statement at the beginning of the OnInit function runs as well. The problem is calling the first debugger function from my dll file, just below my Print state in the journal i get the following errors:

- Cannot find 'debugger', in 'simpleFunctions.dll'
- unresolved import function call
- OnInit critical error
- tester stopped because OnInit failed

I have searched all over the internet to my dissatisfaction of finding that no one really answers this question in a practical way. I suspect the error lies maybe in the C# sharp with regard to data type differences but i really don't know where continue looking at this point. Any help would be greatly appreciated.

 
Thembekile Thembekile:

I suspect the error lies maybe in the C# sharp with regard to data type differences

The "cannot find..." error message suggests otherwise: either that (a) the DLL can't be found, or (b) can't be loaded, or (c) doesn't export the function.

I'd use something like Dependency Walker to check the DLL and make sure that the function appears on the DLL's exports list.

The most common problem is not selecting x64 (or x86) as the "Platform target", rather than leaving it set to AnyCPU. But it sounds as though you've already checked this.