Jump to content

How to receive Unicode string returned from DllCall?


Recommended Posts

I've recently been working with AudioGenie.dll for reading and writing MP3 tags. One of the podcasts I'm working with has title, album and other field encoded in Unicode, and I'm not able to read them properly. I think I need to be doing something differently for Unicode, but have limited experience with AutoIt's DLL call/structs.

Here's an example of the DLL call to read an ID3V2 tag. $frame_id is an integer representation of the tag type (TIT2, TALB, etc.) for the tag (title, album, etc.):

$foo = _AGID3V2GetTextFrameW($ID3F_TALB) ; Get the ID3v2 Album name

Func _AGID3V2GetTextFrameW($frame_id)
    Local $rc = DllCall($_AudioGenie3DLLHandle, "wstr", "ID3V2GetTextFrameW", "uint", $frame_id)
    Return $rc[0]
EndFunc

The resulting string $foo ends up as a bunch of question marks. What do I need to do to receive or process Unicode string results from a DLL?

Link to comment
Share on other sites

The resulting string $foo ends up as a bunch of question marks.

How do you view the returned string?

What do I need to do to receive or process Unicode string results from a DLL?

Nothing more than what you currently do. Make sure anyway that the functions actually returns a UTF-16 encoded Unicode string. Should it return an UTF-8 encoded Unicode string, then the return parameter type is wrong and the returned string needs further processing to become an AutoIt natively manageable string (UTF-16 encoded).

Display the string received as hex to help sort that out.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Display the string received as hex to help sort that out.

I'd tried several methods to display the string, from basic ConsoleWrite() to including attempts to use AscW, but not hex. Should the following work if $bar is a properly formatted Unicode string? (As shown below, it's returning 8-bit hex for ASCII question marks.)

$foobar = _StringToHex($bar)
ConsoleWrite($foobar & @CRLF)

; Output: 3F3F3F3F3F3F203F3F3F3F3F3F3F

If that should have worked, then I'll contact the DLL author to find out what's going on. It's supposed to support Unicode and the author is in Germany, so I can't imagine it not working. The tags in the MP3 are 16-bit, but maybe there's something otherwise screwy with the file. The whole reason I've created this script is to fix inconsistencies in the way different podcasters tag their stuff.

Hex view of the tag directly in the MP3 file:

00 49 00 6E 00 73 00 69 00 64 00 65 00 20      I n s i d e 
00 45 00 75 00 72 00 6F 00 70 00 65            E u r o p e
Link to comment
Share on other sites

Let's see. First Consolwrite doesn't display anything else than ANSI. You can still make SciTE display Unicode but it requires changing the output characterset from ANSI to Unicode and also use this function:

; this version will hapily display Unicode, if you switch Scite to Unicode display
; to do so, open Global Properties and apply this change:
;# Internationalisation
;# Japanese input code page 932 and ShiftJIS character set 128
;#code.page=932
;#character.set=128
;# Unicode
;code.page=65001    <<--
;#code.page=0   <<--
Func _ConsoleWrite($sText)
    Local $aResult = DllCall("kernel32.dll", "int", "WideCharToMultiByte", "uint", 65001, "dword", 0, "wstr", $sText, "int", -1, _
                                "ptr", 0, "int", 0, "ptr", 0, "ptr", 0)
    Local $tText = DllStructCreate("char[" & $aResult[0] & "]")
    DllCall("Kernel32.dll", "int", "WideCharToMultiByte", "uint", 65001, "dword", 0, "wstr", $sText, "int", -1, _
                            "ptr", DllStructGetPtr($tText), "int", $aResult[0], "ptr", 0, "ptr", 0)
    ConsoleWrite(DllStructGetData($tText, 1))
EndFunc ;==>_ConsoleWrite

MsgBox does not have the same problem since it displays Unicode natively. But it won't display just any character in the Unicode set, because no single font has all glyphs. The default font has some characters available. Use CharMap.exe to see which range. One of the largest (most characters available) standard font (under XP at least) is MS Arial Unicode. AFAIK no font offer even all characters in the Unicode plane 0 under any OS.

_StringToHex will convert your string to ANSI first, so it will destroy any Unicode character which is not within your codepage and replace it by a question mark.

This function, and _HexToString, need fixing IMHO.

The dump you provides is UTF-16 so if the function you call returns that part, it will display correctly whatever function you use for string display. And given that the extract posted contains only simple ASCII characters, it will display that part correctly. AutoIt uses the same encoding internally for strings.

Just an idea, possibly dumb: check that the function call doesn't fil by itself: test @error right after the call.

Are you sure you use the latest AutoIt release (3.3.6.1)?

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Thanks. I am on the latest AU3 build and the DLL doesn't trigger an @error.

I'm beginning to suspect there's something odd about those particular files or original tags themselves. One MP3 program can read the tags (Winamp) and another one can't (MediaMonkey) and both are supposed to support Unicode. The AudioGenie DLL works fine for everything else I've thrown at it, though I don't think any of the other ones I've worked with have Unicode tags.

Instead of using "wstr" as the return type for the DllCall, is it possible to use a DllStruct that's just a binary buffer as the return type so I can see exactly what the DLL is returning? Maybe that's barking up the wrong tree.

Link to comment
Share on other sites

Try this:

$foo = _AGID3V2GetTextFrameW($ID3F_TALB) ; Get the ID3v2 Album name

Func _AGID3V2GetTextFrameW($frame_id)
    Local $rc = DllCall($_AudioGenie3DLLHandle, "wstr:cdecl", "ID3V2GetTextFrameW", "uint", $frame_id)
    MsgBox(0, "", $rc[0])
    Return $rc[0]
EndFunc

Using the right calling convention can help :idea:

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Try this:

$foo = _AGID3V2GetTextFrameW($ID3F_TALB) ; Get the ID3v2 Album name

Func _AGID3V2GetTextFrameW($frame_id)
    Local $rc = DllCall($_AudioGenie3DLLHandle, "wstr:cdecl", "ID3V2GetTextFrameW", "uint", $frame_id)
    MsgBox(0, "", $rc[0])
    Return $rc[0]
EndFunc

Using the right calling convention can help :idea:

I must have scrolled past that option dozens of times in the Help file and never noticed it. Changing the calling method causes the script to bomb out immediately after the call without executing the next line, unfortunately. :)

Digging through the AudioGenie docs, I see that the return result is apparently being passed as a BSTR instead of LPCWSTR, and that might be an issue, though I would think it would fail outright rather than working with ANSI, which it does. But I'm really inexperienced with DLLs and internal variable formatting.

BSTR       a basic string starting with 4 bytes length info followed by a sequence of 16bit Unicode chars (UTF-16)
LPCWSTR    a pointer to a sequence of 16bit Unicode chars (UTF-16), terminated with $0 $0
Edited by mlowery
Link to comment
Share on other sites

Hamburg to the rescue! Good catch KaFu.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Yup, that's what I started from. GavinCampbell did a great job setting up the basics. I just renamed some functions to match the native DLL calls. Unless there's something unique about my system, his UDF may have the same issues with Unicode. ANSI works great, though.

The AGID3V2GetTextFrameW() function I implemented was based on his DLL calls (I'm still learning about them, so needed a starting point).

I really think there's something weird about these particular files[*], so I'm going to follow up with the DLL author and see what his input is. When I get a response I'll update.

Thanks for the help so far!

[*]Tag weirdness, is as I mentioned upthread, is the whole reason for the script. Podcasts are tagged really inconsistently, even from episode to episode. The script I'm working on normalizes them at the tag and audio level.

Edited by mlowery
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...