rich41 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I do not know if the following is possible, but if anyone has done something similar I would appreciate any help they could offer.

I am trying to writing a C# program that calls Perl subs, that in turn call functions in the C# program. I found an example of something similar online (OpenHoldemBot, line 399), but I have been unable to call the function pointer without causing an exception.

Any Ideas?

MyClass.cs:

using PERLEZHANDLE = System.IntPtr; class MyClass { [DllImport("PerlEz.dll")] private static extern PERLEZHANDLE PerlEzCreateOpt(string lpFileName +, string lpOptions, string lpScriptOpts); [DllImport("PerlEz.dll")] private static extern int PerlEzCall1(PERLEZHANDLE hHandle, string l +pFunction, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lpBuffer, U +Int32 dwBufSize, string lpFormat, IntPtr IntPtr1); // Callback Function public delegate string Callback(); static public string foo() { return "Hello C# World."; } [STAThread] static void Main(string[] args) { PERLEZHANDLE PerlHandle = PerlEzCreateOpt("test.pl", null, null); // Create a function pointer to the callback Callback cb = new Callback(foo); IntPtr fp = System.Runtime.InteropServices.Marshal.GetFunctionPo +interForDelegate(cb); // Invoke the perl function StringBuilder Response = new StringBuilder(1024); int ReturnCode = PerlEzCall1(PerlHandle, "test", Response, System. +Convert.ToUInt32(Response.Capacity), "i", fp); if(ReturnCode == 5) Console.WriteLine("Exception!"); else Console.WriteLine("Response: " + Response.ToString()); } }

test.pl:

#use strict; require DynaLoader; sub test { my ($a, $b, $c, $d) = @_; # Convert the SV into a CV (...I hope) my $code_ref = DynaLoader::dl_install_xsub("main::foo", $a); # An exception occurs on the next line. return "Hello Perl World." . $code_ref->(); }

Replies are listed 'Best First'.
Re: C# calling Perl calling C#
by jand (Friar) on Oct 15, 2008 at 22:30 UTC
    You cannot implement a CV in C#; the CV assumes certain calling conventions that are specific to the Perl internals.

    You may get this stuff working by turning the callback entry into a Win32::API object. I haven't checked if this would be supported directly, or if you would need to fake up some Win32::API internals first.

    A commercial solution to your problem would be to use PerlNET from the ActiveState Perl Dev Kit. Check out the documentation for what it does.

      After many more hours of searching I came to the same conclusion. The callback example that I referenced was implemented as a DLL and invoked using the Win32::API.

      Unfortunately, using PerlNET is not an option for me; although I do agree it would make things a lot simpler.

      Currently I am trying to implement a work around by defining pure-Perl intermediary functions using PerlEzEvalString, then having those functions modify magic scalars to pass data back to C. At first glance this approach appeared promising; however, providing the correct return type format for the PerlEzSetMagicScalarFunctions function pointers is proving to be rather difficult (but that is more a C problem than a Perl issue).

      I was only able to find one code example relating to magic scalars with PerlEZ (PerlEzSetMagicScalarFunctions), and the documentation at ActiveState appears to be a little thin. Do you know of any other examples?