Click here to get back home

Calling External DLL functions in Perl using Inline

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Calling External DLL functions in Perl using Inline Sankaran 07-11-2005
Posted by Sisyphus on July 22, 2005, 7:13 pm
Please log in for more thread options



>
>
> Hi All.
>
> I'm just wondering how this problem resolved for you, Sankaran. I have
> the (almost) exact same problem, though my environment is cygwin+gcc.

Brian McCauley suggested the specific problem to which you allude was the
result of trying to process C++ source code with the compiler in C mode. If
that's so (and I've seen a couple of other posts suggesting the same thing)
then I think the solution would be to either convert the C++ source to C, or
use Inline::CPP.

With gcc, you need to be compiling C++ code with g++, not gcc - so if,
during the installation of Inline::CPP, you're prompted for which compiler
to use, make sure that g++ is specified.

So ..... I guess the advice is for you to use Inline::CPP if the source is
C++. If the source is C and you're still getting those errors, then let us
know.

> The user32.dll example from the Inline docs works flawlessly for me as
> well.

That's C code, so one would expect no problem there.

>Also, for my application, any conceivable permutation of the INC
> or LIBS directives (and windows/cygwin LIB/INC/PATH environment
> variables) always results in this:
>
> Note (probably harmless): No library found for '-lMyLib'
>

Sometimes that warning actually *is* harmless - I see it from time to time,
but I only ever take notice of it whenever 'MyLib' is not found.

If the lib is not being found, have you tried:
LIBS => '-L/full_path/to_MyLib -lMyLib',

If you specify '-lMyLib', will gcc find a file called 'MyLib.a', or will it
find only a file named 'libMyLib.a' ? (I think it will find either, but you
might check on that in case there's a problem there.)

Let us know how you get on with this.

Cheers,
Rob




Posted by ben.sommer on July 22, 2005, 9:01 am
Please log in for more thread options


Sisyphus wrote:
> >
> >
> > Hi All.
> >
> > I'm just wondering how this problem resolved for you, Sankaran. I have
> > the (almost) exact same problem, though my environment is cygwin+gcc.
>
> Brian McCauley suggested the specific problem to which you allude was the
> result of trying to process C++ source code with the compiler in C mode. If
> that's so (and I've seen a couple of other posts suggesting the same thing)
> then I think the solution would be to either convert the C++ source to C, or
> use Inline::CPP.
>
> With gcc, you need to be compiling C++ code with g++, not gcc - so if,
> during the installation of Inline::CPP, you're prompted for which compiler
> to use, make sure that g++ is specified.
>
> So ..... I guess the advice is for you to use Inline::CPP if the source is
> C++. If the source is C and you're still getting those errors, then let us
> know.
>
> > The user32.dll example from the Inline docs works flawlessly for me as
> > well.
>
> That's C code, so one would expect no problem there.
>
> >Also, for my application, any conceivable permutation of the INC
> > or LIBS directives (and windows/cygwin LIB/INC/PATH environment
> > variables) always results in this:
> >
> > Note (probably harmless): No library found for '-lMyLib'
> >
>
> Sometimes that warning actually *is* harmless - I see it from time to time,
> but I only ever take notice of it whenever 'MyLib' is not found.
>
> If the lib is not being found, have you tried:
> LIBS => '-L/full_path/to_MyLib -lMyLib',

Oh yes. Like I said, I've tried every concveivable permutation.

> If you specify '-lMyLib', will gcc find a file called 'MyLib.a', or will it
> find only a file named 'libMyLib.a' ? (I think it will find either, but you
> might check on that in case there's a problem there.)
>
> Let us know how you get on with this.

A gracious response Rob. Many thanks.

Well, you pointed out an inconsistency: my .lib and .dll files don't
have .a extension. I gratuitously added one and the "No library found"
error dissapeared. How annoying. I grokked the gcc man page and see
what you mean about library file extensions (and prepensions). I'll
send a note to the Inline::C author asking him to include a caveat in
the section of his docs that talks about DLLs.

Cygwin is sometimes confusing, kinda windows, kinda unix. Sometimes
case sensitive, sometimes not.

Best,

--
Ben Sommer
Senior Technology Officer
Eastern Nazarene College
23 East Elm Ave
Quincy, MA 02170



Posted by ben.sommer on July 22, 2005, 1:31 pm
Please log in for more thread options


ben.sommer@enc.edu wrote:
> Sisyphus wrote:
> > >
> > >
> > > Hi All.
> > >
> > > I'm just wondering how this problem resolved for you, Sankaran. I have
> > > the (almost) exact same problem, though my environment is cygwin+gcc.
> >
> > Brian McCauley suggested the specific problem to which you allude was the
> > result of trying to process C++ source code with the compiler in C mode. If
> > that's so (and I've seen a couple of other posts suggesting the same thing)
> > then I think the solution would be to either convert the C++ source to C, or
> > use Inline::CPP.
> >
> > With gcc, you need to be compiling C++ code with g++, not gcc - so if,
> > during the installation of Inline::CPP, you're prompted for which compiler
> > to use, make sure that g++ is specified.
> >
> > So ..... I guess the advice is for you to use Inline::CPP if the source is
> > C++. If the source is C and you're still getting those errors, then let us
> > know.
> >
> > > The user32.dll example from the Inline docs works flawlessly for me as
> > > well.
> >
> > That's C code, so one would expect no problem there.
> >
> > >Also, for my application, any conceivable permutation of the INC
> > > or LIBS directives (and windows/cygwin LIB/INC/PATH environment
> > > variables) always results in this:
> > >
> > > Note (probably harmless): No library found for '-lMyLib'
> > >
> >
> > Sometimes that warning actually *is* harmless - I see it from time to time,
> > but I only ever take notice of it whenever 'MyLib' is not found.
> >
> > If the lib is not being found, have you tried:
> > LIBS => '-L/full_path/to_MyLib -lMyLib',
>
> Oh yes. Like I said, I've tried every concveivable permutation.
>
> > If you specify '-lMyLib', will gcc find a file called 'MyLib.a', or will it
> > find only a file named 'libMyLib.a' ? (I think it will find either, but you
> > might check on that in case there's a problem there.)
> >
> > Let us know how you get on with this.
>
> A gracious response Rob. Many thanks.
>
> Well, you pointed out an inconsistency: my .lib and .dll files don't
> have .a extension. I gratuitously added one and the "No library found"
> error dissapeared. How annoying. I grokked the gcc man page and see
> what you mean about library file extensions (and prepensions). I'll
> send a note to the Inline::C author asking him to include a caveat in
> the section of his docs that talks about DLLs.
>
> Cygwin is sometimes confusing, kinda windows, kinda unix. Sometimes
> case sensitive, sometimes not.

Well, cygwin is the problem, though I might also fault the Inline::C
documentation. Taking that DLL example from the Inline::C cookbook...

http://search.cpan.org/~ingy/Inline-0.44/C/C-Cookbook.pod#Win32

use Inline C => DATA =>
LIBS => '-luser32';

$text = "@ARGV" || 'Inline.pm works with MSWin32. Scary...';

WinBox('Inline Text Box', $text);

__END__
__C__

#include <windows.h>

int WinBox(char* Caption, char* Text) {
return MessageBoxA(0, Text, Caption, 0);
}


That config directive "LIBS => '-luser32'" looks pretty key right? It
says 'go call "user32.dll", right? Wrong. It does nothing. Remove it
and the code compiles and runs the same. Its the windows.h include file
that makes this example - and probably all Win32 dlls - work under
cygwin, thanks to all the hand-rolled header files in
/usr/include/w32api. I'd love to hear from people using Inline::C under
cygwin with gcc and a _custom_ DLL file. How do you manage to link it.
How do are you able to include your own header files? Please prove me
wrong! I'd even listen to people having success with ActiveState perl
and Visual Studio (ugh). Off to post to the cygwin list...

Best,
--
Ben Sommer
Senior Technology Officer
Eastern Nazarene College
23 East Elm Ave
Quincy, MA 02170



Posted by Sisyphus on July 23, 2005, 10:20 am
Please log in for more thread options




> Taking that DLL example from the Inline::C cookbook...
>
> http://search.cpan.org/~ingy/Inline-0.44/C/C-Cookbook.pod#Win32
>
> use Inline C => DATA =>
> LIBS => '-luser32';
>
> $text = "@ARGV" || 'Inline.pm works with MSWin32. Scary...';
>
> WinBox('Inline Text Box', $text);
>
> __END__
> __C__
>
> #include <windows.h>
>
> int WinBox(char* Caption, char* Text) {
> return MessageBoxA(0, Text, Caption, 0);
> }
>
>
> That config directive "LIBS => '-luser32'" looks pretty key right? It
> says 'go call "user32.dll", right? Wrong. It does nothing. Remove it
> and the code compiles and runs the same.

No - I think that LIBS directive is saying "link to libuser32.a" - which you
should find in Cygwin's gcc/lib folder.
I think that script builds ok when you remove the LIBS directive because
libuser32.a is linked to automatically. That automatic linking is built in
by the ExtUtils modules. To verify, insert the following at the top of the
script:

use Inline C => Config =>
BUILD_NOISY => 1;

Then make some change to the C code in that script (to force it to rebuild -
add or delete some white space is sufficient).

Then re-run the script. Somewhere in the output you should see 'libuser32.a'
mentioned (along with a heap of other libs).

> Its the windows.h include file
> that makes this example - and probably all Win32 dlls - work under
> cygwin, thanks to all the hand-rolled header files in
> /usr/include/w32api.

You'll possibly find you don't even have to specifically include windows.h
either. Your Inline::C code will automatically include 'perl.h' and that
might lead to the inclusion of 'windows.h'. On my perls that's the way it
is, anyway. 'perl.h' includes 'win32.h' which includes 'windows.h'. It might
be different under Cygwin.

> I'd love to hear from people using Inline::C under
> cygwin with gcc and a _custom_ DLL file.
> How do you manage to link it.

To link directly to a dll (which is possible with gcc, but not with
Microsoft compilers) you specify the 'MYEXTLIB' config directive - eg:

MYEXTLIB => "/full_path/to_dll/MyDLL.dll",

> How do are you able to include your own header files?

You just list them as you would in normal C code. You can use the 'INC'
config directive to specify the directory that they are in (if they're not
already in some standard location):

INC => "-I/full_path/to_folder/that_contains/my_includes",

> Please prove me
> wrong! I'd even listen to people having success with ActiveState perl
> and Visual Studio (ugh).

I use gcc (MinGW) most of the time - but that's using 'gcc' in a native
Win32 environment - not in a Cygwin environment. I also use MSVC++ 7.0
(.NET) which is a very good compiler - though, for me, a little cumbersome
to use. I prefer to use MinGW as I find I'm a lot more comfortable with it.
I would never use Cygwin - for various reasons (most of which don't apply to
everybody .... or do I mean "anybody" ? :-).

>Off to post to the cygwin list...
>

For Inline::C questions, the best forum is the Inline mailing list. (See
http://lists.perl.org/index.cgi :-)

Cheers,
Rob




Posted by ben.sommer on July 28, 2005, 8:14 am
Please log in for more thread options




Sisyphus wrote:
>
> > Taking that DLL example from the Inline::C cookbook...
> >
> > http://search.cpan.org/~ingy/Inline-0.44/C/C-Cookbook.pod#Win32
> >
> > use Inline C => DATA =>
> > LIBS => '-luser32';
> >
> > $text = "@ARGV" || 'Inline.pm works with MSWin32. Scary...';
> >
> > WinBox('Inline Text Box', $text);
> >
> > __END__
> > __C__
> >
> > #include <windows.h>
> >
> > int WinBox(char* Caption, char* Text) {
> > return MessageBoxA(0, Text, Caption, 0);
> > }
> >
> >
> > That config directive "LIBS => '-luser32'" looks pretty key right? It
> > says 'go call "user32.dll", right? Wrong. It does nothing. Remove it
> > and the code compiles and runs the same.
>
> No - I think that LIBS directive is saying "link to libuser32.a" - which you
> should find in Cygwin's gcc/lib folder.
> I think that script builds ok when you remove the LIBS directive because
> libuser32.a is linked to automatically. That automatic linking is built in
> by the ExtUtils modules. To verify, insert the following at the top of the
> script:
>
> use Inline C => Config =>
> BUILD_NOISY => 1;
>
> Then make some change to the C code in that script (to force it to rebuild -
> add or delete some white space is sufficient).
>
> Then re-run the script. Somewhere in the output you should see 'libuser32.a'
> mentioned (along with a heap of other libs).
>
> > Its the windows.h include file
> > that makes this example - and probably all Win32 dlls - work under
> > cygwin, thanks to all the hand-rolled header files in
> > /usr/include/w32api.
>
> You'll possibly find you don't even have to specifically include windows.h
> either. Your Inline::C code will automatically include 'perl.h' and that
> might lead to the inclusion of 'windows.h'. On my perls that's the way it
> is, anyway. 'perl.h' includes 'win32.h' which includes 'windows.h'. It might
> be different under Cygwin.
>
> > I'd love to hear from people using Inline::C under
> > cygwin with gcc and a _custom_ DLL file.
> > How do you manage to link it.
>
> To link directly to a dll (which is possible with gcc, but not with
> Microsoft compilers) you specify the 'MYEXTLIB' config directive - eg:
>
> MYEXTLIB => "/full_path/to_dll/MyDLL.dll",
>
> > How do are you able to include your own header files?
>
> You just list them as you would in normal C code. You can use the 'INC'
> config directive to specify the directory that they are in (if they're not
> already in some standard location):
>
> INC => "-I/full_path/to_folder/that_contains/my_includes",
>
> > Please prove me
> > wrong! I'd even listen to people having success with ActiveState perl
> > and Visual Studio (ugh).
>
> I use gcc (MinGW) most of the time - but that's using 'gcc' in a native
> Win32 environment - not in a Cygwin environment. I also use MSVC++ 7.0
> (.NET) which is a very good compiler - though, for me, a little cumbersome
> to use. I prefer to use MinGW as I find I'm a lot more comfortable with it.
> I would never use Cygwin - for various reasons (most of which don't apply to
> everybody .... or do I mean "anybody" ? :-).
>
> >Off to post to the cygwin list...
> >
>
> For Inline::C questions, the best forum is the Inline mailing list. (See
> http://lists.perl.org/index.cgi :-)
>
> Cheers,
> Rob

Thanks so much, Rob. I'll be dilligent in trying all your suggestions.

Best,
~Ben Sommer



Similar ThreadsPosted
Calling external dll from inline perl using gcc on XP May 19, 2006, 2:48 am
Getting Path of an external DLL in Perl: Inline C. October 2, 2005, 8:41 pm
Calling C++ / MFC / SDKs usinf Inline July 16, 2005, 12:36 am
How can I call MFC functions from Perl July 19, 2005, 9:46 pm
Using external REXX function libraries with PERL October 1, 2007, 2:40 pm
Module for Orchestrating Perl Functions? May 9, 2008, 9:30 pm
Perl interface to direct port I/O functions (inb(), outb() and friends) February 19, 2005, 9:00 pm
PLJava - Perl embeded into Java (calling Perl from Java) - 1sr release - call for tests and review, please. July 13, 2004, 4:06 am
Error calling Win32::Printer from a perl script hosted in IIS on Win2k3 June 3, 2008, 8:53 am
Error calling Win32::Printer from a perl script hosted in IIS on Win2k3 June 3, 2008, 8:57 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap