Module Win32::API and DLL with Windows Visual C++

J

jc

I am having trouble getting my own DLLs to work with the above module
(ver 0.55).
I can get the examples to work from the write up that involve the DLLs
from kernal32,
but not from my own little DLL project file.

I fear I need a simple example DLL project for MS Visual C++ 6 that
does work.
Can anyone help here?

The current fault I have is;
"Can't call method "Call" on an undefined value at Win32API_Testing.pl
line 44."

With the following lines of PERL code:
use Win32::API;
$function = Win32::API->new(
'C:\\User_JC\\Perl\\Module_Win32\\DLL\\test\\Debug\\test.dll',
'int ANSIchar(int a, int b)'
);
$return = $function->Call( 1, 2 ) ; # This is line 44

I have change this around but $function is always undefined.
I am not sure if Win32::API is the appropriate tool for accessing user
DLLs. ??

Regards JC....
 
S

sisyphus

Quoth jc <[email protected]>:

Well, there are several alternatives. The most long-winded and most
likely to work successfully is to write a proper XS module that wraps
the DLL. If you find XS too complicated, you could use Inline::C to do
the grunt-work for you, though IME it brings its own list of
complications I prefer to do without.

For mine, Win32::API is *not* the ideal way to access user DLLs
unless:
1) you don't have a C compiler
or
2) your C compiler cannot link to DLLs directly (ie needs to link to
an import lib) and you don't have an import library for that DLL.

With Strawberry Perl, neither 1) nor 2) will be the case (as
Strawberry Perl comes with the MinGW port of the gcc compiler - which
is a C compiler that can link directly to a DLL if need be).
With ActiveState Perl neither 1) nor 2) will be the case if you first
'ppm install MinGW'.

Ben's advice is, as always, sound. But don't let his recommendations
deter you from using Inline::C which is a powerful and flexible tool
that allows you to take the XS solution without having to learn how to
write an XS file. (You may need to become familiar with the perl API,
however - 'perldoc perlapi'.)

And feel free to use Inline::C with windows system DLLs too:

use Inline C => DATA =>
MYEXTLIB => 'C:/windows/system32/user32.dll';

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

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

__END__
__C__

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

That's based on an example in the Inline::C-Cookbook documentation -
it links directly to user32.dll. To use that script with a Microsoft
Compiler you'd have to replace:

MYEXTLIB => 'C:/windows/system32/user32.dll';
with
LIBS => '-luser32';

With MinGW, both of those 2 variations work (assuming user32.dll is in
C:/windows/system32, of course).

Once your Inline::C solution has been written you can always convert
to XS (and avoid the dependency upon Inline) using
<plug>InlineX::C2XS</plug>, again without having to go to the trouble
of writing the XS file yourself.

Cheers,
Rob
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top