in reply to Call a function from DLL

my $return = $function1;

This doesn't work. $function1 is a Win32::API-Object. If you want to execute the actual function you need to use

my $return = $function1->Call();


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^2: Call a function from DLL
by Cleware (Novice) on Jul 15, 2019 at 10:19 UTC

    Thank you for your suggestion

    I just tried to use it but I got

    "Can't call method "Call" without a package or object reference at script.pl line 9."

    #!usr/bin/perl use strict; use warnings; use Win32::API; my $function=Win32::API->Import( 'D:\Cleware\USBaccess.dll', 'int FCWInitObject()', ); my $return = $function->Call();
      I just downloaded the DLL from here and the following code works for me now. I noticed there are 32 and 64-Bit versions of the DLL. Make sure you are using the one that matches your Perl (64 bit most likely, find out by perl -v).
      use strict; use warnings; use Win32::API; my $FCWInitObject = Win32::API->Import( 'D:\ENV\USBaccess.dll', 'int FCWInitObject()', ); die "Error: ".( Win32::FormatMessage( Win32::GetLastError() ) ) if !$F +CWInitObject; my $FCWOpenCleware = Win32::API->Import( 'D:\ENV\USBaccess.dll', 'int FCWOpenCleware(int a)', ); die "Error: ".( Win32::FormatMessage( Win32::GetLastError() ) ) if !$F +CWOpenCleware; my $pointer = $FCWInitObject->Call(); print "Pointer $pointer\n"; my $success = $FCWOpenCleware->Call( $pointer ); print "Success $success\n";
      Works in the sense that it outputs the following and does not throw an error:
      Pointer 8350080 Success 0
      That makes kinda sense since I don't have a device connected that could be "opened" but why does it then return a pointer in the first place?


      holli

      You can lead your users to water, but alas, you cannot drown them.
      That's because your call to Import doesn't work. Apparently it returns an empty string. You should check for errors as Discipulus said above.


      holli

      You can lead your users to water, but alas, you cannot drown them.