Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Win32::API : Creating a LPCWSTR type input parameter from a string

by ajose (Acolyte)
on Aug 23, 2011 at 17:37 UTC ( [id://921950]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I am just beginner in using the Perl module Win32::API. I could successfully invoke the Win32 API's and parse the return values using this module. But now I am stuck at a point where I have to invoke the API with a parameter of data type LPCWSTR as an input parameter. I tried following method to achieve the same.

$profileName = encode("UTF-16LE", $profileName); $profileName = pack ('p', $profileName); $API{ api}->Call($handle, $interface, $profileName, 0, $profileXml +, 0, 0));

API declaration:

DWORD WINAPI WlanGetProfile( __in HANDLE hClientHandle, __in const GUID *pInterfaceGuid, __in LPCWSTR strProfileName, __reserved PVOID pReserved, __out LPWSTR *pstrProfileXml, __inout_opt DWORD *pdwFlags, __out_opt PDWORD pdwGrantedAccess );

Output is:

Argument "t\0e\0s\0t\0\0\0\0\0\0\0\0\0\0\0\0\0..." isn't numeric in su +broutine entry at ...

Please help me by suggesting any work around to convert the string to LPCWSTR format before invoking the API. Also output of this API call is also a pointer type(LPWSTR). Hence please help me here also.

Replies are listed 'Best First'.
Re: Win32::API : Creating a LPCWSTR type input parameter from a string
by ikegami (Patriarch) on Aug 23, 2011 at 20:11 UTC

    Get rid of

    $profileName = pack ('p', $profileName);

    Win32::API recognises LPCWSTR and should handle it properly.

    I could see it passing "t" instead of "test", though. The prototype interface is... troublesome. If that's the case, I'd avoid the prototype interface, define strProfileName as an "N", and pass unpack('J', pack('p', $profileName)).

      Thanks for the reply. Did you mean API declaration as the prototype interface?. If not, I have included the parameter declaration which is used in my script. I believe 't' is being passed since I used "encode("UTF-16LE", $profileName)" in the code. So I removed that also.

      Following is the code snippet after making the above changes. Error message is also mentioned below:

      ['WlanGetProfile' => 'IPNIPPI' => 'I'], $profileName = unpack('J', pack('p', $profileName)); $API{ api}->Call($handle, $interface, $profileName, 0, $profileXml, 0, + 0));
      An attempt was made to reference a token that does not exist at .....
Re: Win32::API : Creating a LPCWSTR type input parameter from a string
by AllBackJack (Initiate) on Jul 17, 2012 at 23:30 UTC

    Did you ever get this to work correctly?

    I'm having a similar problem with FindFirstFileNameW and FindNextFileNameW. I've never used LPCWSTR in a call to Win32::API and it's driving me nuts. I've tried 'P' as well using the standard type of call (i.e. no automatic Win32 definition conversion).

    I've traced similar Win32::API calls through (e.g. CreateHardLinkA) and if it's a LPTSTR type argument, it translates it fine to a type 'p' to the call to pack (I think it's the 'type' pm that's doing it). But with the explicit LPCWSTR it treats it as 'S' to pack.

    Anyway, if you got it working, I'd appreciate seeing the code the works. On the other hand, if someone just has a working wrapper to FindFirstFileNameW/FindNextFileNameW that would be even easier...

Re: Win32::API : Creating a LPCWSTR type input parameter from a string
by sundialsvc4 (Abbot) on Aug 24, 2011 at 12:39 UTC

    IIRC, the interface layer wants to do the necessary byte-twiddling (encoding & decoding) for you, so you therefore do not want to (re-)do it yourself.

    “Okay, she’s a string.” – with apologies to Bill Murray in Ghostbusters.

      Hi, I have done some modifications to my code as per suggestions given earlier. Please find the latest code structure for your reference.

      ['WlanGetProfile' => 'IPNIPPI' => 'I'], $profileName = unpack('J', pack('p', $profileName)); $API{ api}->Call($handle, $interface, $profileName, 0, $profileXml, 0, + 0));

      Error message:

      An attempt was made to reference a token that does not exist at .....

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-18 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found