3

I have a AutoIt script which uses a undocumented gdi32 function (GetFontResourceInfoW) (Autoit: _WinAPI_GetFontResourceInfo).

It returns the name of a font file (.fon, .ttf, .ttc, etc. installed or not) The script works perfect. I want to recode it now in Powershell. The function prototype (from GetFontResourceInfo) is:

BOOL GetFontResourceInfo(LPCTSTR lpszFilename, // font file name
                         LPDWORD cbBuffer,     // size of buffer for resouce information
                         LPVOID lpBuffer,      // buffer for returned resouce information
                         DWORD dwQueryType,    // resouce information query type
                        );

I tried the following, but it does not return the fontname.

$code=@'
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

public static class  FontUtil{

    [DllImport("gdi32.dll")] 
        public static extern bool GetFontResourceInfoW(string lpszFilename, ref UInt32 cbBuffer, out IntPtr lpBuffer, UInt32 dwQueryType); 
}
'@
Add-Type $code

[string]$fn = 'c:\windows\fonts\arial.ttf'
[Uint32]$b = 260
[IntPtr]$LocalStructPtr = Runtime.InteropServices.Marshal]::AllocHGlobal(260)
$ret=[fontutil]::GetFontResourceInfoW($fn, [ref] $b, [ref] $LocalStructPtr,[UInt32]1)
[Runtime.InteropServices.Marshal]::PtrToStringAuto($LocalStructPtr,$b)
[Runtime.InteropServices.Marshal]::FreeHGlobal($LocalStructPtr)

I think there is a problem with the paramaters or interop marshaling.

Any ideas what could be wrong?

2 Answers 2

0
out IntPtr lpBuffer

This is declared incorrectly. It should be:

IntPtr lpBuffer

Then the function call becomes:

$ret=[fontutil]::GetFontResourceInfoW($fn, [ref] $b, $LocalStructPtr,[UInt32]1)

You were passing the address of the pointer variable, but instead you pass that pointer variable's value. That's the address of the memory you allocated when you called AllocHGlobal.

Note also that your buffer has room for 130 characters rather than 260, because a UTF-16 code unit is two bytes wide. That's probably fine, but it might not be what you are expecting.

0

For marshaling strings, StringBuilder is a lot more convenient than mucking around with manually allocated buffers:

$code=@'
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Runtime.InteropServices;

public static class FontUtils {
    const int QFR_DESCRIPTION = 1;

    [DllImport("gdi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    static extern bool GetFontResourceInfoW(
        string lpszFilename, 
        [In, Out] ref int cbBuffer, 
        [Out] StringBuilder lpBuffer, 
        int dwQueryType
    );

    public static string GetFontDescription(string fileName) {
        int bufferSize = 0;
        StringBuilder sb = new StringBuilder();
        if (!GetFontResourceInfoW(fileName, ref bufferSize, sb, QFR_DESCRIPTION)) {
            throw new Win32Exception();
        }
        sb.Capacity = bufferSize / sizeof(char);
        if (!GetFontResourceInfoW(fileName, ref bufferSize, sb, QFR_DESCRIPTION)) {
            throw new Win32Exception();
        }
        return sb.ToString();
    }
}
'@
Add-Type $code

[FontUtils]::GetFontDescription('c:\windows\fonts\arial.ttf')

Of course, it's perfectly possible to write the C# code in PowerShell as well (the marshaler isn't language dependent), I just think this is a cleaner separation of concerns.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.