-1

I am working on my own scripting language using C# and ANTLR, and I've been able to implement almost everything I wanted.

I know that one can't make a perfect language on themselves, so I wanna build in a way to import functions from C# scripts. For that, i've researched about DLLImport anc calling functions from that, but i just cant seem to get that to work.

I am currently stuck at an EntryPointNotFoundException, however, my system uses object instead of strictly defined types, which threw a PInvoke: cannot return variants exception. Here's some code i tried:

Program.cs

[DLLImport("mydll.dll", EntryPoint = "main", Charset = Charset.Unicode)]
static extern object main(object[] args)
main(Array.empty<object>())

C# class library used for creatng the dll

public class Test
{
   public static object main(object[] args)
   {
      Console.WriteLine("Test sucessful!");
      return 0;
   }
}

Be forgiving if i am just overthinking this or don't know something obvious, I am still a pretty inexperienced developer.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 5
    `[DllImport]` is for importing native library functions. For importing managed libraries, use `Assembly.Load()` or `Assembly.LoadFrom()` instead. – PMF Apr 10 '22 at 16:05
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Apr 10 '22 at 16:15
  • [What is the difference between a managed and unmanaged DLL](https://stackoverflow.com/questions/4943970/what-is-the-difference-between-managed-and-unmanaged-dll#:~:text=The%20term%20%22managed%20code%22%20usually,means%20C%20or%20C%2B%2B.)? – John Wu Apr 10 '22 at 17:23

1 Answers1

0

For everyone who tries to acheive the same thing, here is the solution based on @PMF's comment:

var asm = Assembly.LoadFrom("YourDLL.dll");
var type = asm.GetType("YourNamespace.YourClass");
var method = type.GetMethod("YourMethod");
object[] args = new object[0];
method.Invoke(Activator.CreateInstance(type, Array.Empty<object>()), args);