rich41 has asked for the wisdom of the Perl Monks concerning the following question:
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 | |
by rich41 (Initiate) on Oct 16, 2008 at 22:41 UTC |