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

I would like to mount on a Windows machine a remote share with unicode character as HIRAGANA LETTER A, "\xe3 \x81 \x82" in UTF8 or "\x{3042}" Unicode.
I have try the function Win32::NetResource::AddConnection(), this one doesn't work.
Looking to network trace, the mapping of the drive is done using the UTF8 format (sequence e3 81 82) for the share name or according CIFS protocol, the share name needs to be UNICODE (30 42) inside the network message.
Do any one has mount a drive on remote unicode share using this function ?

Replies are listed 'Best First'.
Re: Win32 mounting Unicode share
by almut (Canon) on Jun 27, 2008 at 15:25 UTC

    Looking at the source code reveals that, under the hood of Win32::NetResource::AddConnection(), the WNetAddConnection2A Win32 function is being used. This is the ANSI version of the two available WNetAddConnection2 functions — the other version, WNetAddConnection2W, is for Unicode.

    Normally, the ANSI versions simply handle / pass through data as a byte string without any special interpretation, so it should probably work if you encode the share name appropriately.

    If I'm understanding you correctly, the required CIFS encoding in this case is \x30 \x42, so you could try

    my $sharename = Encode::encode("UTF-16BE", $sharename_utf8);

    and then use the encoded binary string $sharename in the AddConnection() call. Or, in case that should turn out to be the wrong byte order, try "UTF-16LE".  ($sharename_utf8 is the name as a Perl character string, as it would be returned from $sharename_utf8 = "...\x{3042}...".)

    If all else fails, you might also try patching Win32::NetResource to use the "wide API" call WNetAddConnection2W, but that would mean having to rebuild the module... My guess would be that - in this particular case - this would neither be necessary, nor provide any particular benefit.  HTH.

    ___

    BTW, you can always easily get a hexdump of a string like this:

    sub hexdump { print join(" ", unpack("(H2)*", shift)), "\n"; } use Encode; my $sharename_utf8 = "\x{3042}"; my $sharename = encode("UTF-16BE", $sharename_utf8); hexdump($sharename_utf8); # e3 81 82 hexdump($sharename); # 30 42

    Update: fixed U --> W in wide-API function name WNetAddConnection2W — thanks monarch!

Re: Win32 mounting Unicode share
by nikosv (Deacon) on Jul 01, 2008 at 14:47 UTC
    Try using the WSH object model which uses Unicode inherently (UTF16) and abstracts the api. Create an instance of Wscript.Network class and then call its MapNetworkDrive method
Re: Win32 mounting Unicode share
by ririparis (Initiate) on Jul 01, 2008 at 07:45 UTC
    I cannot succeed to map a Unicode share, using the suggested methods: encode() or calling AddConnectionW() I get this answer from the author of the module http://rt.cpan.org/Public/Bug/Display.html?id=37237 thanks for all