newLISP Fan Club

Forum => newLISP and the O.S. => Topic started by: prio on October 16, 2006, 07:08:37 AM

Title: NewLisp.dll and C#
Post by: prio on October 16, 2006, 07:08:37 AM
Hi,



I was wondering has anyone tried calling newlispEvalStr from a C# application? Do you have some code to show me how or can you point me in the right direction?



I have tried using dllImport and it seems (i.e. in debug view I can see (+ 2 2) returns "4n")  to work but I keep getting AccessViolationExceptions.



Class which import newlisp.dll (NewLispDLL.cs) is:



using System;
using System.Text;
using Microsoft.CSharp;
using System.Runtime.InteropServices;

class NewLispDLL
{
    [DllImport("C:\Program Files\newlisp\newLisp.dll", EntryPoint = "newlispEvalStr", CallingConvention = CallingConvention.Cdecl)]
    private static extern System.Text.StringBuilder newlispEvalStr(string txt);

    public static string Eval(string LispExpression)
    {
        System.Text.StringBuilder ResultObject;
        ResultObject = newlispEvalStr(LispExpression);
        return ResultObject.ToString();
    }
}


Code that calls it (Program.cs) is:



using System;
using System.Collections.Generic;
using System.Text;

namespace NewLispTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string strCmd = "(+ 2 2)";
            string txt = "";
            txt = NewLispDLL.Eval(strCmd);        
            Console.WriteLine(txt);
            Console.ReadLine();
        }
    }
}


Thanks.
Title:
Post by: Lutz on October 16, 2006, 07:25:10 AM
Hi Prio,



two points:



(1) newlisp.dll has to be called with 'stdcall' calling conventions, not 'cdecl'. Only on UNIX newlisp.so or newlisp.dylib is imported with 'cdecl'.



'stdcall' is the standard Win32 calling convention for most Win32 DLLs.



(2) 'newlispEvalStr' returns a pointer to a string, you seem to import that already this way? (I am not familiar with C#).



Perhaps if you just change the calling convention spec, it will work.



Let us know,



Lutz
Title:
Post by: prio on October 16, 2006, 10:20:29 AM
Hi Lutz,



Changing to StdCall seems to have fixed it. Thanks.
Title:
Post by: HPW on October 16, 2006, 11:03:10 AM
Do you use the Mingw compiled DLL?

I had tried it in the past from VB.net and got it only working with the borland compiled flavour. But maybe things changed.