an exe (generated by pp) then using ShellExecute to catch the error

M

Mav

Hi, all
how do I return error from a pl script(convert into .exe) when it
is called by ShellExecute.

I have a small helloworld.pl program that I used pp(Par-Packer) to
convert that become an helloworld.exe
helloworld.pl is very simple, just like below:

use Win32::Registry;
my $name;
my $error = 0;
$! = 0; #first clear $! from previous error code
$::HKEY_LOCAL_MACHINE->Open("SOFTWARE\\XYZ\\Configurationn",
$config);

#check see if the XYZ software is install.
if ($!) {
$error = 100
return fail; } #Is that how to return error to another program on
perl???
else
{ #continue doing something
}

Now on VC++ my progam call app.exe
When I tried to call my helloworld.exe

#this is app.exe
ShellExecute(NULL,"open","helloworld.exe", "","", SW_SHOW );

how to check if any error code get return by helloworld.exe (if it
got error how can I get the error code 100)?

Thanks,
Mav
 
B

Ben Morrow

Quoth Mav said:
Hi, all
how do I return error from a pl script(convert into .exe) when it
is called by ShellExecute.

I have a small helloworld.pl program that I used pp(Par-Packer) to
convert that become an helloworld.exe
helloworld.pl is very simple, just like below:

use Win32::Registry;

Where is

use strict;
use warnings;
my $name;
my $error = 0;
$! = 0; #first clear $! from previous error code
$::HKEY_LOCAL_MACHINE->Open("SOFTWARE\\XYZ\\Configurationn",
$config);

#check see if the XYZ software is install.
if ($!) {
$error = 100
return fail; } #Is that how to return error to another program on
perl???

Err... no. Where did you get that idea from? Did you even try it?
else
{ #continue doing something
}

Now on VC++ my progam call app.exe
When I tried to call my helloworld.exe

#this is app.exe
ShellExecute(NULL,"open","helloworld.exe", "","", SW_SHOW );

how to check if any error code get return by helloworld.exe (if it
got error how can I get the error code 100)?

You can set helloworld's exit status with the exit Perl builtin. How you
would then go about retrieving this from ShellExecute is rather beyond
me... Is there a good reason you can't use system() or CreateProcess()
from C++?

Ben
 
M

Mav

Where is

use strict;
use warnings;



Err... no. Where did you get that idea from? Did you even try it?





You can set helloworld's exit status with the exit Perl builtin. How you
would then go about retrieving this from ShellExecute is rather beyond
me... Is there a good reason you can't use system() or CreateProcess()
from C++?

Ben

Hi, Ben
Thanks for you answer again. Yea...I think at the end I decide to
use the exit code on the perl and the CreateProcess on C++. That is
working. However, when my perl script is abnormal terminal like:
....
my $ssh = new Net::SSH::W32Perl($host,
protocol => '2,1',

#debug=>1,
);
....
where if $host name is invalid, I could not get the exit code from C++
side, the pl script just got terminated. Any better idea? Or do you
know when I can read some more about this?

thanks,
Mav
 
B

Ben Morrow

Quoth Mav said:
Quoth Mav said:
Now on VC++ my progam call app.exe [helloworld.exe is a pp-packed Perl program]
When I tried to call my helloworld.exe
#this is app.exe
ShellExecute(NULL,"open","helloworld.exe", "","", SW_SHOW );
how to check if any error code get return by helloworld.exe (if it
got error how can I get the error code 100)?

You can set helloworld's exit status with the exit Perl builtin. How you
would then go about retrieving this from ShellExecute is rather beyond
me... Is there a good reason you can't use system() or CreateProcess()
from C++?

Thanks for you answer again. Yea...I think at the end I decide to
use the exit code on the perl and the CreateProcess on C++. That is
working. However, when my perl script is abnormal terminal like:
...
my $ssh = new Net::SSH::W32Perl($host,

This syntax (new Foo ()) is considered a bad idea nowadays. I can see
the attraction if you're coming from C++ (that's why it was added to
Perl in the first place), but under the wrong circumstances it can do
unexpected things. I would write that

my $ssh = Net::SSH::W32Perl->new($host, ...);
protocol => '2,1',

#debug=>1,
);
...
where if $host name is invalid, I could not get the exit code from C++
side, the pl script just got terminated. Any better idea? Or do you
know when I can read some more about this?

Net::SSH::perl->new (which is what Net::SSH::W32Perl->new calls) ends up
calling 'die' if it can't connect. This is not actually documented: I
had to poke through the source to make sure, so I don't blame you for
not realising :).

When a Perl program calls 'die', normally perl prints a message to
STDERR and exits with status 255; if you don't ever normally exit with
this value you can use this to determine something went wrong. To find
out more, you can wrap the call to new (or, indeed, the whole body of
your program) in an eval block: eval and die are Perl's exception
mechanism, like try and catch in C++. The message (or object, but in
this case it's a simple message) passed to 'die' shows up in $@ (which
is always the null string if there was no error), so you can do
something like

eval {
my $ssh = Net::SSH::W32Perl->new(...);
}

if ($@) { # there was an exception

if ($@ =~ /
^Can't\ connect\ to\ (.*),\ port\ (.*)
:\ (.*)\ at\ $0\ line\ \d+.$
/x) {
# the exception was from Net::SSH::perl

my ($host, $port, $error) = ($1, $2, $3);
warn "SSH connect to $host:$port failed: $!";

# your calling program knows that exit code 3 means 'I
# couldn't connect'.
exit 3;
}

die $@; # otherwise propagate the error
}

It's not the neatest way ever to handle exceptions, but it's usually
workable... If you want to do something more sensible with the failure
than simply reporting it to the user, you will need to communicate the
details of what went wrong back to C++ somehow. This will having some
connection open with your Perl program when you run it; one simple way
would be to redirect perl's STDERR to a file, and see what's in there
after it exits. If you do that there's no point bothering with the eval
stuff, of course.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top