1
$\begingroup$

I am not getting the answers back from my DLL calls. Here are the simple C# functions. Not sure why I am not getting the correct answers. I can DLL import kernel32d.dll and can call getTickCount() just fine. Just not my home grown DLL. Any insight would be appreciated.

Thanks,

Phil

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace wpa1
{
    public class Class1
    {
        public string func1(string strIn)
        {
            System.Console.WriteLine(strIn);
            char[] letters = { 'A', 'B', 'C' };
            string alphabet = new string(letters);
            return alphabet;
        }
        public float add(float a, float b)
        {
            return a+b;
        }
    }
}

Here is the Mathematica

code:
Needs["NETLink`"];
ReinstallNET["Force32Bit" -> True];
fullDllPath = 
  "c:\\users\\owner\\documents\\visual studio \
2015\\Projects\\wpa1\\wpa1\\bin\\Debug\\wpa1.dll";
LoadNETAssembly["wpa1.dll", 
  "c:\\users\\owner\\documents\\visual studio \
2015\\Projects\\wpa1\\wpa1\\bin\\Debug"];
add1 = DefineDLLFunction["add", fullDllPath, 
   "float", {"float", "float"}];
func1 = DefineDLLFunction["func1", fullDllPath, 
   "float", {"float", "float"}];
obj = NETNew["wpa1.Class1"];
NETObjectQ[obj]
stback = obj.func1 ("hi there")
OwnValues[stback]
a = obj.add ({1.2, 2.1})

Here is the output in Mathematica:

True

"hi there" (NETLink`Objects`NETObject$192170795466753).Function[Null, 
   If[NETLink`DLL`Private`checkArgCount["func1", {##1}, 2], 
    Wolfram`NETLink`DynamicDLLNamespace`DLLWrapper2`func1[##1], \
$Failed], {HoldAll}]

{HoldPattern[stback] :> 
  "hi there" (NETLink`Objects`NETObject$192170795466753).Function[
     Null, If[NETLink`DLL`Private`checkArgCount["func1", {##1}, 2], 
      Wolfram`NETLink`DynamicDLLNamespace`DLLWrapper2`func1[##1], \
$Failed], {HoldAll}]}

{1.2 (NETLink`Objects`NETObject$192170795466753).add, 
 2.1 (NETLink`Objects`NETObject$192170795466753).add}
$\endgroup$
4
$\begingroup$

There are some minor errors that need to be corrected:

  1. The main problem is that C# methods must be called using the syntax obj@Method[args]. The C#-looking expression obj.func1("hi there") will be interpreted as the (nonsensical) Mathematica expression Dot[obj, func1 * "hi there"].
  2. The arguments to add must be passed as individual arguments instead of as a list.
  3. There is no need to use DefineDLLFunction at all. LoadNETAssembly automatically defines all assembly types along with their members. DefineDLLFunction is used to define entrypoints in unmanaged DLLs, e.g. from C or C++. (Incidentally, but irrelevant to the present example: the argument and return type declarations of func1 were incorrect and the definition of the C# function add was being assigned to the symbol add1 instead of the symbol add which was used later.)

The following code applies all of these corrections:

LoadNETAssembly[fullDllPath];

obj = NETNew["wpa1.Class1"];

obj@func1["hi there"]
(* "ABC" *)

obj@add[1.2, 2.1]
(* 3.3 *)
$\endgroup$
  • $\begingroup$ Thanks for that help works like a charm. Thumbs up WReach!! $\endgroup$ – Phil Neumiller Jan 15 '17 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.