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

I am reuseing come code that has been developed and I have a general question. When I issue
$InitTelnetClients = new Win32::API('Telnet', 'InitTelnetClients', +'N','I'); $error = $InitTelnetClients->Call($session);
i get Can't call method "Call" on an undefined value at C:/Perl/site/lib/miAutoUtil.pm line 237, <DATA> line 1. as an error. Now $session is set to 1. Can anyone help me with this, or point me in the right direction. Thanks

Replies are listed 'Best First'.
Re: Win32::API Question
by ikegami (Patriarch) on Nov 24, 2005 at 18:04 UTC

    It's $InitTelnetClients that's undefined. Looks like your call to new. failed.

    Win32::API's new sets $! on error (according to the documentation), so you could do something like:

    $InitTelnetClients = Win32::API->new('Telnet', 'InitTelnetClients', 'N +', 'I') or die("Unable to load telnet DLL: $!\n");

    By the way, the indirect calling syntax (method class args) is fragile and/or unreliable. You should used the direct calling syntax (class->method(args)) as shown.

Re: Win32::API Question
by blazar (Canon) on Nov 24, 2005 at 18:05 UTC

    I'm not familiar with Win32::API, but chances are that the constructor may fail, and if so, then you should take care of that. What does the documentation have to say about this? Check it: it's just as easy as perldoc Win32::API.

    Also, you said that you're resusing someone else's code. This is not that bad, if you avoid cargo culting it. Try to understand it instead!

    Also, it may seem to you that this doesn't have to do Win32::API, and indeed it doesn't, but evidence is that you're not using strict (and thus probably warnings ): please (do a favour to yourself and) do!!

    Last, there shouldn't be any problem with the above, but to be sure avoid indirect object syntax and write

    my $InitTelnetClients=Win32::API->new(@args);
    instead.

Re: Win32::API Question
by Anonymous Monk on Nov 24, 2005 at 18:22 UTC
    figured out one problem, didnt have the telnet.dll file in my directory. I thought that it would use the windows one. sorry bout that...
      I thought that it would use the windows one.
      It would have, if it had found it.

      See Dynamic-Link Library Search Order for detailed info on where it looks, and in what order.