Embedding Perl In a Visual C++ App (Console and GUI)

 

Environment: VC6, SP4

This is mostly taken from the perlembed document, but with slight modifications that should clarify embedding a perl interpreter in a VC++ app. It is assumed that you have perl installed on your machine. You may want to get the source code too.

The screenshot above is what the included GUI project should look like. The app is in no way useful. It just demonstrates how to embed and use a perl interpreter in your VC++ application. Take a look at the event handler method for the Try It button click in the demo project below.

The following steps show how to create a console application that embeds a perl interpreter.

  1. Run 'perl -MExtUtils::Embed -e xsinit' from the command line to generate the perlxsi.c file.
  2. In Visual C++, create a new "Win32 Console Application" project and name it interp. (If you select "Win32 Application", it won't build properly).
  3. Insert the following files into your project by selecting Projet -> Add To Project -> Files...:
    • perlxsi.c
    • perl56.lib (yes, actually insert it into the project)
    • interp.c (the first example app from perlembed)

    Typically you'll find perl.lib in C:\perl\lib\CORE, if not, you should see the CORE directory relative to perl -V:archlib.

    The studio will also need this path so it knows where to find Perl include files. This path can be added via the Tools -> Options -> Directories tab.

  4. Finally, select Build -> Build interp.exe and you're ready to go.

The problems I've run into were apparently just environment related, so your results may vary. I am not a VC++ expert, so it took me a bit to figure it out. I was also able to build a Win32 Windows executable (See sample project) that works. Here's the steps involved to at least add the interpreter to a GUI application. You'll have to add the perl parsing code yourself. See the demo project for more details.

  1. Run 'perl -MExtUtils::Embed -e xsinit' from the command line to generate the perlxsi.c file.
  2. Rename perlxsi.c to perlxsi.cpp
  3. Create a new "MFC AppWizard (exe)" project and name it perldlg. Select "Dialog Based Application" and Finish.
  4. Copy perlxsi.cpp to your main project directory
  5. Insert the following files into your project by selecting Projet -> Add To Project -> Files...:
    • perlxsi.cpp
    • perl56.lib

  6. Place the following line in the bottom of perldlgDlg.h:
              static PerlInterpreter *my_perl;
    


  7. Add the following includes to the top of perldlgDlg.h:
              #include <EXTERN.h>
              #include <perl.h>
    

  8. Place the following code at the bottom of your perlxsi.cpp file. IT MUST BE PLACED AT THE BOTTOM OF THE FILE AFTER EVERYTHING ELSE OR IT WON'T BUILD.
            #include "stdafx.h"
    

  9. Use the perl interpreter pointer declared earlier (*my_perl) in perldlgDlg.h to access perl functionality.
  10. Build perldlg.exe by selecting Build -> Build perldlg.exe

You may get some compiler warnings about EXTERN_C being redefined. I just removed the second definition. While this may not be desirable, it does allow the application to compile, link, and run.

Please understand that I am not certain as to why a lot of this was needed or how it all works, but I was successful in getting it to build. If you have questions, I'll do my best to help. Good luck!!

Downloads

Download demo project - 32.6 Kb


Comments

  • Dynamically Loading the Perl Modules

    Posted by Legacy on 11/12/2003 12:00am

    Originally posted by: Shivsun

    The article was very good and compiled a nd built a dialog based application which calls the perl Interpreter.When I call simple perl programs, my code is able to execute the perl script properly.But When I include some perl modules in the script, for example the following modules
    
    use URI::URL;
    use LWP::UserAgent;
    use HTTP::Request;
    use HTTP::Request::Common;
    use HTTP::Request::Form;
    use HTML::TreeBuilder 3.0;
    @ISA=("LWP::UserAgent","URI::URL","HTTP::Request";,"HTTP::Request::Common";,"HTTP::Request::Form";,"HTML::TreeBuilder 3.0","HTTP::Cookies";);
    use strict;

    I am getting the following error, when the script is executed.

    (Not Implemented) Can't load module IO, dynamic loading not available in this perl.
    (You may need to build a new perl executable which either supports
    dynamic loading or has the IO module statically linked into it.)Compilation failed in require at D:/Perl/lib/IO/Handle.pm line 241, line 1.
    BEGIN failed--compilation aborted at D:/Perl/lib/IO/Handle.pm line 241, line 1.
    Compilation failed in require at D:/Perl/lib/IO/Socket.pm line 11, line 1.
    BEGIN failed--compilation aborted at D:/Perl/lib/IO/Socket.pm line 11, line 1.
    Compilation failed in require at D:/Perl/site/lib/LWP/Protocol/http.pm line 11, line 1.
    Compilation failed in require at (eval 5) line 3, <LOGINURLS> line 1.


    Please Help me in solving this problem....:-(
    Thanks in advance....

    Regards,
    ShivSun

    Reply
  • NO EXTERN.h NO PERLh

    Posted by Legacy on 01/23/2002 12:00am

    Originally posted by: Karsten Schulz

    wo are the header PERL.h and EXTERN.h ?

    .....

    Reply
  • Sharing Memory/Variables

    Posted by Legacy on 01/18/2002 12:00am

    Originally posted by: Gerard J. Nicol

    How do you share storage between your perl program, and your calling C++ program?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Do you know where your data is? Consumer cloud-based file sharing services store your sensitive company data on servers outside of your control, outside of your policy and regulatory guidelines – maybe even outside your country – and not managed by you. The potential for data leakage, security breaches, and harm to your business is enormous. Download this white paper to learn about file sync and share alternatives that allow you to manage and protect your sensitive data while integrating and …

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds