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->(); }


In reply to C# calling Perl calling C# by rich41

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.