In article <(E-Mail Removed)>, MyAlias wrote:
> Can't solve this CallBack returning structures
>
> Error message:
> An unhandled exception of type 'System.NullReferenceException'
> occurred in MyTest.exe
> Additional information: Object reference not set to an instance of an
> object.
>
> Situation skeleton:
> Private Declare Function EnumFontFamiliesASCII Lib "gdi32" Alias
> "EnumFontFamiliesA" (ByVal hdc As Integer, ByVal lpszFamily As Integer,
> ByVal lpEnumFontFamProc As effp, ByVal lParam As Integer) As Integer
This is my first impression
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure LOGFONT
Public lfHeight As Integer
Public lfWidth As Integer
Public lfEscapement As Integer
Public lfOrientation As Integer
Public lfWeight As Integer
Public lfItalic As Byte
Public lfUnderline As Byte
Public lfStrikeOut As Byte
Public lfCharSet As Byte
Public lfOutPrecision As Byte
Public lfClipPrecision As Byte
Public lfQuality As Byte
Public lfPitchAndFamily As Byte
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=LF_FACESIZE)> _
Public lfFaceName As String
End Structure
....
Private Delcare Auto Function EnumFontFamilies lib "gdi32" _
(ByVal hdc As IntPtr, _
ByVal lpszFamily As String, _
ByVal lpEnumFontFamProc As effp, _
ByVal lpParam As IntPtr) As Integer
Private Declare Function GetDC Lib "user32" _
(ByVal hWnd As IntPtr) As IntPtr
Private Declare Function ReleaseDC Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal hDC As IntPtr) As Integer
> Delegate Function effp(ByRef lpNLF As LOGFONT, ByRef lpNTM As
> NEWTEXTMETRIC, ByVal FontType As Integer, ByRef lParam As Integer) As
> Integer
Delegate Function effp _
(ByRef lpelf As LOFFONT, _
ByRef lpntm As NEWTEXTMETRIC, _
ByVal FontType As Integer, _
ByVal lParam As IntPtr) As Integer
Private Function EnumFontFamProc( _
ByRef lpNLF As LOGFONT, _
ByRef lpNTM As NEWTEXTMETRIC, _
ByVal FontType As Integer, _
ByRef lParam As Integer) As Integer
' MyNonRelatedCode here
return 1
End Function
Public Function EnumFontFamilies() As String()
Dim shdc As IntPtr = GetDC(IntPtr.Zero)
' very important - keep a reference to you're
' delegate, or GC may snatch it from you
Dim cb As New effp(AddressOf EnumFontFamProc)
EnumFontFamilies( _
shdc, _
Nothing, _
cb, _
IntPtr.Zero)
Call ReleaseDC(IntPtr.Zero, shdc)
End Function
HTH
--
Tom Shelton [MVP]