Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Call C DLL Functions in Perl

by sraghu_rs (Initiate)
on Feb 09, 2012 at 09:27 UTC ( [id://952678]=perlquestion: print w/replies, xml ) Need Help??

sraghu_rs has asked for the wisdom of the Perl Monks concerning the following question:

Dear Friends, I am writing to call the APIs (functions) in the C DLL using perl CPAN Module Win32::API. The code works fine in the case where API expects input variable as Long. When the API expects the input as char *, i am not getting expected results. In the below, API, (1) and (2) are working fine. But API (3) has input parametes as char *. The result is signed long. In the working case, the result should return value greater than zero. but i am getting result as zero. Could you please advice me, how to pass the value as char * (C datatype) to the API as expected by API. Thanks in advance. Regards, Rags

DLL : mydll.dll

1) API Syntax: long CurrentDate();
use Win32::API; my $dec = Win32::API->new('mydll.dll', 'CurrentDate', '', 'L') or die +$^E; my $rv_bstr = $dec->Call(); ($sec,$min,$hour,$day,$month,$year) = localtime($rv_bstr); # correct the date and month for humans $year = 1900 + $year; $month++; printf "%02d/%02d/%02d %02d:%02d:%02d\n", $year, $month,$day, $hour, $ +min, $sec;
2) API Syntax: long GetVersion(char *pstrBuf, long lBuf);
use Win32::API; my $dec = Win32::API->new('mydll.dll', 'GetVersion', 'PN', 'L') or die + $^E; my $lpBuffer = " " x 64; $return = $dec->Call($lpBuffer, 64); print $lpBuffer;
3) API Syntax: long OpenConnection(char *strDataSource, char *strUser, char *strPwd);
use Win32::API; my $dec = Win32::API->new('mydll.dll', 'OpenConnection', 'PPP', 'L') o +r die $^E; $strDatabase = "test"; $strLoginUser = "test"; $strLoginPwd = "test"; # converting to C String $db = pack('a*x', $strDatabase); $user = pack('a*x', $strLoginUser); $pwd = pack('a*x', $strLoginPwd); $return = $dec->Call($db, $user, $pwd); print $return;

Replies are listed 'Best First'.
Re: Call C DLL Functions in Perl
by ikegami (Patriarch) on Feb 09, 2012 at 11:15 UTC
    By the way,
    ($sec,$min,$hour,$day,$month,$year) = localtime($rv_bstr); # correct the date and month for humans $year = 1900 + $year; $month++; printf "%02d/%02d/%02d %02d:%02d:%02d\n", $year, $month,$day, $hour, $ +min, $sec;
    can be written as
    use POSIX qw( strftime ); print strftime "%Y/%m/%d %H:%M:%S\n", localtime($rv_bstr);
Re: Call C DLL Functions in Perl
by ikegami (Patriarch) on Feb 09, 2012 at 10:19 UTC

    Just like you did for (2).

    $return = $dec->Call($strDatabase, $strLoginUser, $strLoginPwd);

    That said, what you have amounts to strings terminated with two NULs, so it should work too.

      @ikegami - Even after passing like that, it is not working.

        Maybe "test" is not a valid database/user/password?
        I said as much, but it is the proper usage based on the information you posted. Is there anything to make you think it's a problem with how you call the function?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://952678]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-23 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found