Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello

As a follow-up to the above, here's how you can wrapper .NET code and expose it to COM, making it available to Perl. The following example wrappers the .NET System.Console.Write and System.Console.WriteLine methods.

First, the .NET wrapper in C#. This is for .NET 2005
using System; using System.Runtime.InteropServices; // visible to COM using System.Collections.Generic; using System.Text; namespace WrapConsole { // directive to tell COM to see this. [ComVisible(true)] /* interface that our class will implement. Some examples use lead +ing "_", some use "I" * eg _WrapConsole or IWrapConsole */ public interface _WrapConsole { // items to return [DispId(1)] void WriteLine( string line); [DispId(2)] void Write( string line); } [ComVisible(true), ClassInterface(ClassInterfaceType.None), ProgId("WrapConsole")] // our class implements the interface defined above (after the ":" +) public class WrapConsole : _WrapConsole { // COM needs a parameter-less constructor public WrapConsole() { } // method to call WriteLine; [ComVisible(true)] public void WriteLine(string line) { System.Console.WriteLine(line); } // method to call Write; [ComVisible(true)] public void Write(string line) { System.Console.Write(line); } } }
At build time, through the VS 2005 IDE, check "Register for COM interop" in the Project Properties/Build section. From the commandline, you'd have to run 'regasm' to register the assembly into the Registry.

Next, the Perl code

use Win32::OLE; my $wc = Win32::OLE->new('WrapConsole') or die "oops\n"; $wc->WriteLine( 'this is a test, should have a new line'); $wc->Write( 'this should not have a new line '); $wc->Write( "This should follow immediately, with a new line\n");
Output is
>perl pWrapConsole.pl this is a test, should have a new line this should not have a new line This should follow immediately, with a + new line
HTH, j

In reply to Re^2: Access .net object from PERL by jimbojones
in thread Access .net object from PERL by Shaune

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-24 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found