Hi All,
I am trying to call this Win32 Api in C# but I have problem in the call back function. The call back function EnumFontExDelegate never get call. The following is my code:
Is there anything that I have missed out in my code? Thanks in advance.
class PrinterEnumerator
{
public const int LF_FACESIZE = 32;
private const byte DEFAULT_CHARSET = 1;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class LOGFONT
{
public int lfHeight = 0;
public int lfWidth = 0;
public int lfEscapement = 0;
public int lfOrientation = 0;
public int lfWeight = 0;
public byte lfItalic = 0;
public byte lfUnderline = 0;
public byte lfStrikeOut = 0;
public byte lfCharSet = 0;
public byte lfOutPrecision = 0;
public byte lfClipPrecision = 0;
public byte lfQuality = 0;
public byte lfPitchAndFamily = 0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public string lfFaceName = string.Empty;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct ENUMLOGFONTEX
{
public LOGFONT elfLogFont;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public string elfFullName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public string elfStyle;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public string elfScript;
}
public delegate int EnumFontExDelegate(ref ENUMLOGFONTEX lpelfe, IntPtr lpntme, int FontType, IntPtr lParam);
[DllImport("gdi32.dll", EntryPoint = "EnumFontFamiliesEx")]
static extern int EnumFontFamiliesEx(IntPtr hdc, [In, MarshalAs(UnmanagedType.LPStruct)] ref LOGFONT lpLogfont,
EnumFontExDelegate lpEnumFontFamExProc, IntPtr lParam, uint dwFlags);
[DllImport("gdi32.dll")]
static extern IntPtr CreateDC(string lpszDriver, string lpszDevice,
string lpszOutput, IntPtr lpInitData);
[DllImport("gdi32.dll")]
static extern bool DeleteDC(IntPtr hdc);
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref int size);
public static LOGFONT CreateLogFont()
{
LOGFONT lf = new LOGFONT();
lf.lfHeight = 0;
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = 0;
lf.lfItalic = 0;
lf.lfUnderline = 0;
lf.lfStrikeOut = 0;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = 0;
lf.lfClipPrecision = 0;
lf.lfQuality = 0;
lf.lfPitchAndFamily = 0;
lf.lfFaceName = "";
return lf;
}
public static void Test()
{
IntPtr hPrinterDC;
StringBuilder dp = new StringBuilder(256);
int size = dp.Capacity;
if (GetDefaultPrinter(dp, ref size))
{
Console.Write(String.Format("Printer: {0}, name length{1}", dp.ToString().Trim(), size));
}
else
{
Console.WriteLine("Error!");
}
hPrinterDC = CreateDC("WINSPOOL", dp.ToString(), null, IntPtr.Zero);
// instantiate the delegate
EnumFontExDelegate del = new EnumFontExDelegate(EnumFontFamExProc);
LOGFONT logFont = CreateLogFont();
// call the win32 function
EnumFontFamiliesEx(hPrinterDC, ref logFont, del, IntPtr.Zero, 0);
DeleteDC(hPrinterDC);
}
public static int EnumFontFamExProc(ref ENUMLOGFONTEX lpelfe, IntPtr lpntme, int FontType, IntPtr lParam)
{
Console.WriteLine("Test :" + lpelfe.elfFullName);
return 1;
}
}