In a Flutter application, I’m trying to use the function AssocQueryStringA
to query some info about a file extension. By dart:ffi I’m using the following code to declare and load the function:
final _shlwapi = DynamicLibrary.open('shlwapi.dll');
static int assocQueryString(
int flags,
int str,
Pointer<Utf16> pszAssoc,
Pointer<Utf16> pszExtra,
Pointer<Utf16> pszOut,
Pointer<Uint32> pcchOut) =>
_assocQueryString(flags, str, pszAssoc, pszExtra, pszOut, pcchOut);
static final _assocQueryString = _shlwapi.lookupFunction<
Uint32 Function(
IntPtr flags,
IntPtr str,
Pointer<Utf16> pszAssoc,
Pointer<Utf16> pszExtra,
Pointer<Utf16> pszOut,
Pointer<Uint32> pcchOut),
int Function(
int flags,
int str,
Pointer<Utf16> pszAssoc,
Pointer<Utf16> pszExtra,
Pointer<Utf16> pszOut,
Pointer<Uint32> pcchOut)>('AssocQueryStringA');
Then I call the function with this code, I passed ASSOCSTR_FRIENDLYAPPNAME
with the value of 4
in the second argument:
Pointer<Utf16> temp = wsalloc(MAX_PATH);
Pointer<Uint32> tempSize = malloc<DWORD>();
assocQueryString(0, 4, TEXT(".xlsx"), nullptr, temp, tempSize);
var tt = temp.toDartString();
but the value returned in the tt
variable is not readable as the following image show:
Is there something I’m doing wrong?
You’re calling the “ANSI” version of the function (
AssocQueryStringA
) but are passingPointer<Utf16>
. “ANSI” versions of Windows functions use strings with 1-byte code units encoded in the system’s local encoding. If you want to use UTF-16, you should be calling the “wide” version instead (AssocQueryStringW
).