NewLisp.dll and C#

Machine-specific discussion
Unix, Linux, OS X, OS/2, Windows, ..?
Locked
prio
Posts: 4
Joined: Tue Mar 21, 2006 3:52 pm

NewLisp.dll and C#

Post by prio »

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 "4\n") to work but I keep getting AccessViolationExceptions.

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

Code: Select all

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:

Code: Select all

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.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

prio
Posts: 4
Joined: Tue Mar 21, 2006 3:52 pm

Post by prio »

Hi Lutz,

Changing to StdCall seems to have fixed it. Thanks.

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

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.
Hans-Peter

Locked