Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Access a c++ module from Perl

Reply
Thread Tools

Access a c++ module from Perl

 
 
bg-greece
Guest
Posts: n/a
 
      10-24-2005
I am using cygwin and perl and trying to access some function from a C++
windows dll. After the first attempt produced a segmentation fault, I tried
to do it step-by-step. I created a dummy function in the dll file:

int TestDLL(int x)
{
return 1;
}


and put the following code in a perl file:

use strict;
use Win32::API:rototype;

my $TestDLL = ApiLink("mydll",'I TestDLL(I x)');
if (not defined $TestDLL) {
die "Can't import API TestDLL: $! \n";
}
print "ApiLink done\n";

my $xx = 0;
my $return = $TestDLL->Call($xx);
print $return."\n";


But even this produces a segmentation fault (core dump). Any ideas?

BG


 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      10-24-2005
"bg-greece" <(E-Mail Removed)> wrote:

> I am using cygwin and perl and trying to access some function from a
> C++ windows dll. After the first attempt produced a segmentation
> fault, I tried to do it step-by-step. I created a dummy function in
> the dll file:


....

> But even this produces a segmentation fault (core dump). Any ideas?


Just a very wild guess: you wrote Cygwin. How did you make the DLL? Inside
Cygwin, or outside it?

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com

 
Reply With Quote
 
 
 
 
bg-greece
Guest
Posts: n/a
 
      10-25-2005
the dll was created with with Microsoft Visual C++ 6.0


"John Bokma" <(E-Mail Removed)> wrote in message
news:Xns96F96BB05E958castleamber@130.133.1.4...
> Just a very wild guess: you wrote Cygwin. How did you make the DLL? Inside
> Cygwin, or outside it?
>
> --
> John Small Perl scripts: http://johnbokma.com/perl/
> Perl programmer available: http://castleamber.com/
> I ploink googlegroups.com
>



 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      10-25-2005
bg-greece wrote:

>I am using cygwin and perl and trying to access some function from a C++
>windows dll. After the first attempt produced a segmentation fault, I tried
>to do it step-by-step. I created a dummy function in the dll file:
>
>int TestDLL(int x)
>{
> return 1;
>}
>
>
>and put the following code in a perl file:
>
>use strict;
>use Win32::API:rototype;
>
>my $TestDLL = ApiLink("mydll",'I TestDLL(I x)');
> if (not defined $TestDLL) {
> die "Can't import API TestDLL: $! \n";
>}


In my experience, the prototype based stuff is very buggy. I've had
nothing but crashes with it. I would skip it and go the oldfashioned raw
way, which always *did* work. Try.

use Win32::API;
my $TestDLL = Win32::API->new('mydll', 'TestDLL', 'I', 'I')
or die "Can't load function";

HTH,
Bart.
 
Reply With Quote
 
bg-greece
Guest
Posts: n/a
 
      10-25-2005
In fact Win32::API was the first try and it also produced segmentation
fault.Other trivial examples however (with kernel32.dll for example) have
worked.

> In my experience, the prototype based stuff is very buggy. I've had
> nothing but crashes with it. I would skip it and go the oldfashioned raw
> way, which always *did* work. Try.
>
> use Win32::API;
> my $TestDLL = Win32::API->new('mydll', 'TestDLL', 'I', 'I')
> or die "Can't load function";
>
> HTH,
> Bart.



 
Reply With Quote
 
harryfmudd [AT] comcast [DOT] net
Guest
Posts: n/a
 
      10-25-2005
bg-greece wrote:
> the dll was created with with Microsoft Visual C++ 6.0


Are you using the Cygwin distribution of Perl? I think you will find
that it was _not_ made with Visual C++ V6, but with gcc.

I personally have had good luck with Win32::API, though sometimes it was
not obvious how to describe the function I wanted to import, and when
you get the description wrong, you do get segment violations. I do not
remember using the new, sexy prototype interface, just the old, clunky
one. See Win32:rocess::Info::NT for an example.

Disclaimer: I wrote the cited module. And I never said it was a _good_
example.

Tom Wyant
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      10-26-2005
"bg-greece" <(E-Mail Removed)> wrote:

> "John Bokma" <(E-Mail Removed)> wrote in message
> news:Xns96F96BB05E958castleamber@130.133.1.4...
>> Just a very wild guess: you wrote Cygwin. How did you make the DLL?
>> Inside Cygwin, or outside it?


> the dll was created with with Microsoft Visual C++ 6.0


That might cause the problem(s) you see.

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com

 
Reply With Quote
 
bg-greece
Guest
Posts: n/a
 
      10-26-2005
You mean I should create the DLL with cygwin/gcc?
Putting aside that it needs a lot of time to do the porting, it contradicts
the fact that I can perfectly call Windows system DLL's like user32,
kernel32 and so on using Win32::API.

B.

"John Bokma" <(E-Mail Removed)> wrote in message
news:Xns96FAC9D803B50castleamber@130.133.1.4...
> "bg-greece" <(E-Mail Removed)> wrote:
>
> > "John Bokma" <(E-Mail Removed)> wrote in message
> > news:Xns96F96BB05E958castleamber@130.133.1.4...
> >> Just a very wild guess: you wrote Cygwin. How did you make the DLL?
> >> Inside Cygwin, or outside it?

>
> > the dll was created with with Microsoft Visual C++ 6.0

>
> That might cause the problem(s) you see.
>
> --
> John Small Perl scripts: http://johnbokma.com/perl/
> Perl programmer available: http://castleamber.com/
> I ploink googlegroups.com
>



 
Reply With Quote
 
harryfmudd [AT] comcast [DOT] net
Guest
Posts: n/a
 
      10-26-2005
bg-greece wrote:
> You mean I should create the DLL with cygwin/gcc?
> Putting aside that it needs a lot of time to do the porting, it contradicts
> the fact that I can perfectly call Windows system DLL's like user32,
> kernel32 and so on using Win32::API.


Well, to be honest I have never done what you're trying to do FROM
CYGWIN. I also note that someone has, because Win32-Process-Info 1.006
has a "pass" from Cygwin (see
http://testers.cpan.org/show/Win32-Process-Info.html).

On the other hand, if you have the calling sequence right (as you say
you do) and if the version of C doesn't matter (as you say it doesn't),
then we just proved your code works.

But it doesn't. At this point, I generally ask myself "what is it that I
know to be true, but which is in fact not true?". The possibilities seem
to me to be:

1) the calling sequence is in fact coded incorrectly in Perl. I have no
experience with the prototype version of the interface, but the
traditional interface call in Bart Lateur's response looks right to me
given the C signature in your original note, and you say that didn't work.

2) Different compilers _can_ produce different calling sequences, so
that's what I was focusing on. You say calls to Kernel32 work, which
suggests that this isn't the problem, but doesn't prove it, since we
don't know that Kernel32 was written in Visual C++. But we _do_ know
that Visual C++ can call kernel32.dll. Have you read kernel32.h, and
made sure your signatures are specified the same way? C++ wants to
mangle function names (that's how overloading works), and it's possible
that there is more than one way to handle the call stack. A mangled
function name would probably result in a failure to import the function,
but there are probably other things that could go wrong.

3) Something else entirely. Arthur Conan Doyle was wrong when he had
Sherlock Holmes say something like "When you have eliminated everything
else, the remaining possibility, however improbable, must be the truth."
The problem with the Sherlock Holmes approach is that it assumes you
really know what every single possibility is.

Tom Wyant
 
Reply With Quote
 
Sisyphus
Guest
Posts: n/a
 
      10-29-2005

"bg-greece" <(E-Mail Removed)> wrote in message
news:djl9aa$16vn$(E-Mail Removed)...
> In fact Win32::API was the first try and it also produced segmentation
> fault.Other trivial examples however (with kernel32.dll for example) have
> worked.
>


Those dll's that are working were built using the stdcall calling
convention. I suggest building your dll using the same calling convention -
there's a command line switch that does that for you. (I think it's '/Gz' -
but check 'cl /?')

There exists somewhere a patched version of Win32::API that will work with
cdecl calling convention, but I don't think that patched version has been
ported to Cygwin.

Cheers,
Rob


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: F2PY: Access Fortran module data from multiple python module Gabriel Genellina Python 0 11-21-2008 04:22 AM
help - Module needs access to another module abcd Python 4 03-20-2007 10:10 PM
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
Converting a perl module to a python module would it be worth it? Mothra Python 5 04-15-2005 06:09 AM
Using JYTHON inside ANT : access os module -> "ImportError: no module named javaos" eric_bellard Python 1 10-07-2004 05:41 AM



Advertisments