cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
ejesjh
Level I

Load .NET dll and call a function in JSL

Hi I am very beginner of JSL and am trying to create a JMP application for our team.

To do this, I realized that I have to load c# dll to the JSL and call a function from there. 

I have read the part of DLLs in JMP scripting guide but I am not so sure this can be applied for .NET dll.

Does anyone have an experience of loading .NET dll and calling a function from there in the JSL?

1 REPLY 1
jschroedl
Staff

Re: Load .NET dll and call a function in JSL

Hi @ejesjh, I'll try to help you out here.

 

The first thing to know is that calling from JMP into a .NET DLL (Assembly) is a bit trickier than calling into native DLLs because .NET assemblies do not normally export their entry points.

 

Internally, JMP uses the GetProcAddress Windows API to locate functions to call. So, the key to having JMP call into your code is having the entry point properly exported.

 

I can recommend two possible approaches you can use to call from JMP into a .NET assembly:

1. Use one of the publicly-available Nuget packages which allow marking methods so that they are exported. One such package is DllExport (https://github.com/3F/DllExport) Using that particular package requires a bit of extra work as it will modify your csproj file to add the necessary code to enable a [DllExport] attribute.

 

To test this approach, I created a WinForms (.NET Framework) Class Library. First I had to create a x64 configuration as the default AnyCPU config does not generate a 64-bit DLL. Then, I used the batch file provided with that package to add the DllExport machinery. Then, I exported a method:

 

 

    public static class MyExportedClass
    {
        [DllExport("MyMethod")]
        public static void MyMethod()
        {
            MessageBox.Show("Called into MyMethod");
        }
    }

 

I built the assembly and finally used the following JSL to call it.

myDLL = LoadDLL("C:\MyLibrary\bin\x64\Debug\MyLibrary.dll");
myDLL << Declare Function( "MyMethod" );
myDLL << MyMethod();

 

2. The other option I can recommend is to create a C++ DLL which is an intermediary between JMP and your C# code. The DLL would export functions for JMP to call and within the DLL, you would call your C# code. To allow C++ code to call into C#, you use the C++/CLI language feature. This means the C++ code is built with the /clr option and references .NET assemblies. You would add a reference to your C# assembly and make calls into your C# code. 

 

To get started in this direction, I would use Visual Studio and create a new project using the CLR Class Library (.NET Framework) template. If you get stuck on this approach, post what you've tried and perhaps I can offer more guidance.

 

Hope this helps,

John