Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: calling a function from a dll

by Corion (Patriarch)
on Dec 31, 2012 at 17:02 UTC ( [id://1011079]=note: print w/replies, xml ) Need Help??


in reply to calling a function from a dll

I never could get the "C type parser" of Win32::API to work. I always had more luck manually constructing the parameter string using the P and I chars, and then using unpack for strings passed in and out.

In your case, maybe the following construct works:

$function = Win32::API->new('NSLClientLibrary', 'XYZDecrypt', 'PPP', ' +I'); sub call_function { my ($value, $key, $encrypted) = @_; # Here we pad $value to the expected length, to avoid a buffer ove +rrun # I guess that the result is at most as long as the encrypted stri +ng $value = ' ' x length $encrypted; $function->( $value, $key, $encrypted ); $value =~ s!\0.*!!; # we assume that the first \0 returned indicat +es end of the string return $value };

Replies are listed 'Best First'.
Re^2: calling a function from a dll
by BrowserUk (Patriarch) on Dec 31, 2012 at 20:02 UTC
    $value =~ s!\0.*!!; # we assume that the first \0 returned indicates end of the string

    That is a pretty bad assumption. BSTRs are fixed length arrays of unsigned shorts with a prefix length and can contain multiple nulls.

    See http://blogs.msdn.com/b/ericlippert/archive/2003/09/12/52976.aspx


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      what is interesting is that the prefix length is a byte count and not a character count,although BSTR is always Unicode

      So to find the end of the string you have to get to the stored length which is stored in the first 2 bytes

      the example at : BSTR layout shows the byte count and the terminated nulls,both required by BSTR

Re^2: calling a function from a dll
by nmeijer (Initiate) on Dec 31, 2012 at 19:03 UTC

    Am I perhaps calling the function wrong or something? as I get a
    Not a CODE reference at xx.pl line 14
    which is where the $function is called in the sub routine

    #!/usr/bin/perl use Win32::API; my $function = Win32::API->new('C:\XYZClientLibrary\x86\XYZClientLibra +ry.dll', 'XYZDecrypt', 'PPP', 'I'); print call_function('9','8B3A06617FEC4276A5711FBA4861B467', '[000001]C +0808FC9B3062D91E7B9B1D9A7B79216'); sub call_function { my ($value, $key, $encrypted) = @_; # Here we pad $value to the expected length, to avoid a buffer ove +rrun # I guess that the result is at most as long as the encrypted stri +ng $value = ' ' x length($encrypted); print (" $value, $key, $encrypted\n"); $value = $function->( $value, $key, $encrypted ); $value =~ s!\0.*!!; # we assume that the first \0 returned indicat +es end of the string return $value };

      nmeijer:

      Looking at the Win32::API docs, it looks like line 14 should be:

      $value = $function->Call( $value, $key, $encrypted );

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 12:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found