PC Review


Reply
Thread Tools Rate Thread

Can't solve this CallBack returning structures

 
 
MyAlias
Guest
Posts: n/a
 
      25th May 2004
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
Delegate Function effp(ByRef lpNLF As LOGFONT, ByRef lpNTM As
NEWTEXTMETRIC, ByVal FontType As Integer, ByRef lParam As Integer) 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 Integer = GetDC(0)
-> EnumFontFamiliesASCII(shdc, 0, AddressOf EnumFontFamProc, 0)
Call ReleaseDC(0, shdc)
End Function

The error occurs at line signaled by '->'

Error cause from the top level view:
The windows does not care about the NET framework, it does not notify
that the structures lpNLF and lpNTM where initialized

Why do I say that?:
Because there is no error when replacing in effp and EnumFontFamProc
ByRef lpNLF As LOGFONT, ByRef lpNTM As NEWTEXTMETRIC
by
ByVal lpNLF As Integer, ByVal lpNTM As Integer


What is the Attribute to use or any other workarround if available?


Thanks for taking you time

 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      25th May 2004
Hi,

Best work around

Dim ff As FontFamily
For Each ff In FontFamily.Families
Debug.WriteLine(ff.Name)
Next

Ken
---------------------
"MyAlias" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> Delegate Function effp(ByRef lpNLF As LOGFONT, ByRef lpNTM As
> NEWTEXTMETRIC, ByVal FontType As Integer, ByRef lParam As Integer) 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 Integer = GetDC(0)
> -> EnumFontFamiliesASCII(shdc, 0, AddressOf EnumFontFamProc, 0)
> Call ReleaseDC(0, shdc)
> End Function
>
> The error occurs at line signaled by '->'
>
> Error cause from the top level view:
> The windows does not care about the NET framework, it does not notify
> that the structures lpNLF and lpNTM where initialized
>
> Why do I say that?:
> Because there is no error when replacing in effp and EnumFontFamProc
> ByRef lpNLF As LOGFONT, ByRef lpNTM As NEWTEXTMETRIC
> by
> ByVal lpNLF As Integer, ByVal lpNTM As Integer
>
>
> What is the Attribute to use or any other workarround if available?
>
>
> Thanks for taking you time
>



 
Reply With Quote
 
MyAlias
Guest
Posts: n/a
 
      25th May 2004
May you explain how does your solution apply to microsoft windows
callbacks returning structures?


Ken Tucker [MVP] wrote:

> Hi,
>
> Best work around
>
> Dim ff As FontFamily
> For Each ff In FontFamily.Families
> Debug.WriteLine(ff.Name)
> Next
>
> Ken
> ---------------------
> "MyAlias" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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
>> Delegate Function effp(ByRef lpNLF As LOGFONT, ByRef lpNTM As
>>NEWTEXTMETRIC, ByVal FontType As Integer, ByRef lParam As Integer) 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 Integer = GetDC(0)
>>-> EnumFontFamiliesASCII(shdc, 0, AddressOf EnumFontFamProc, 0)
>> Call ReleaseDC(0, shdc)
>> End Function
>>
>>The error occurs at line signaled by '->'
>>
>>Error cause from the top level view:
>> The windows does not care about the NET framework, it does not notify
>>that the structures lpNLF and lpNTM where initialized
>>
>>Why do I say that?:
>> Because there is no error when replacing in effp and EnumFontFamProc
>> ByRef lpNLF As LOGFONT, ByRef lpNTM As NEWTEXTMETRIC
>> by
>> ByVal lpNLF As Integer, ByVal lpNTM As Integer
>>
>>
>>What is the Attribute to use or any other workarround if available?
>>
>>
>>Thanks for taking you time
>>

>
>
>


 
Reply With Quote
 
Mattias Sjögren
Guest
Posts: n/a
 
      25th May 2004

How did you declare NEWTEXTMETRIC?



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      25th May 2004
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]
 
Reply With Quote
 
MyAlias
Guest
Posts: n/a
 
      26th May 2004
Thanks for your attention

NEWTEXTMETRIC only has Integers and Bytes, no problem
The problem appears for arrays
LOGFONT has lfFaceName(LF_FACESIZE) As Byte
the solution I got from another declaration is to marshall:
<MarshalAsAttribute(UnmanagedType.ByValArray,
SizeConst:=LF_FACESIZE)> lfFaceName() As Byte


I don't know what parameters to use for marshalling structures inside
structures.


I got this unsolved:


Private Structure WCRANGE
Dim wcLow As Short
Dim cGlyphs As Short
End Structure

Private Structure GLYPHSET
Dim cbThis As Integer
Dim flAccel As Integer
Dim cGlyphsSupported As Integer
Dim cRanges As Integer
OK-> <MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst:=100)> Dim
ranges() As Integer
End Structure

what marshall parameters to use to declare
ranges() As WCRANGE
instead of
ranges() As Integer
in order to have EXACTLY the same memory contents?



Mattias Sjögren wrote:

> How did you declare NEWTEXTMETRIC?
>
>
>
> Mattias
>


 
Reply With Quote
 
MyAlias
Guest
Posts: n/a
 
      26th May 2004

Thats the solution I partially found after 8 hours
I believe your code will work completely


If you still have time, could you tell me how to get a NET form's hdc?
I tried GetDC and CreateDC but this generates an error.
I need the hDC to list font properties such as GetFontUnicodeRanges


Another marshalling problem:

Private Structure WCRANGE
Dim wcLow As Short
Dim cGlyphs As Short
End Structure

Private Structure GLYPHSET
Dim cbThis As Integer
Dim flAccel As Integer
Dim cGlyphsSupported As Integer
Dim cRanges As Integer
OK-> <MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst:=100)> Dim
ranges() As Integer
End Structure

what marshall parameters to use to declare
ranges() As WCRANGE
instead of
ranges() As Integer
in order to have exactly the same memory contents?

Thanks for taking your time

 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      26th May 2004
On Wed, 26 May 2004 00:49:20 +0100, MyAlias wrote:

>
> Thats the solution I partially found after 8 hours
> I believe your code will work completely


Well, I'm glad you found an answer... Wish I had seen it earlier

>
> If you still have time, could you tell me how to get a NET form's hdc?
> I tried GetDC and CreateDC but this generates an error.
> I need the hDC to list font properties such as GetFontUnicodeRanges


Dim g As Graphics = Me.CreateGraphics()
Dim hDC As IntPtr = g.GetHdc()

' do stuff with the hDC

' make sure you clean up the graphics object
g.Dispose()

>
> Another marshalling problem:
>
> Private Structure WCRANGE
> Dim wcLow As Short
> Dim cGlyphs As Short
> End Structure
>
> Private Structure GLYPHSET
> Dim cbThis As Integer
> Dim flAccel As Integer
> Dim cGlyphsSupported As Integer
> Dim cRanges As Integer
> OK-> <MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst:=100)> Dim
> ranges() As Integer
> End Structure
>
> what marshall parameters to use to declare
> ranges() As WCRANGE
> instead of
> ranges() As Integer
> in order to have exactly the same memory contents?
>
> Thanks for taking your time


I answered this in another thread, but the short answer is you can't...
The current .NET marshaller doesn't support arrays of structs in structs.
You'll need to pass it in as integers, and then break them up manually
after the call...

I think, but I haven't confirmed that this will be added in 2.0.
--
Tom Shelton [MVP]
 
Reply With Quote
 
MyAlias
Guest
Posts: n/a
 
      26th May 2004
Thanks a lot for your help
it spared me a lot of time


Tom Shelton wrote:
> On Wed, 26 May 2004 00:49:20 +0100, MyAlias wrote:
>
>
>>
>>Thats the solution I partially found after 8 hours
>>I believe your code will work completely

>
>
> Well, I'm glad you found an answer... Wish I had seen it earlier
>
>
>>If you still have time, could you tell me how to get a NET form's hdc?
>>I tried GetDC and CreateDC but this generates an error.
>>I need the hDC to list font properties such as GetFontUnicodeRanges

>
>
> Dim g As Graphics = Me.CreateGraphics()
> Dim hDC As IntPtr = g.GetHdc()
>
> ' do stuff with the hDC
>
> ' make sure you clean up the graphics object
> g.Dispose()
>
>
>>Another marshalling problem:
>>
>> Private Structure WCRANGE
>> Dim wcLow As Short
>> Dim cGlyphs As Short
>> End Structure
>>
>> Private Structure GLYPHSET
>> Dim cbThis As Integer
>> Dim flAccel As Integer
>> Dim cGlyphsSupported As Integer
>> Dim cRanges As Integer
>>OK-> <MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst:=100)> Dim
>>ranges() As Integer
>> End Structure
>>
>>what marshall parameters to use to declare
>> ranges() As WCRANGE
>>instead of
>> ranges() As Integer
>>in order to have exactly the same memory contents?
>>
>>Thanks for taking your time

>
>
> I answered this in another thread, but the short answer is you can't...
> The current .NET marshaller doesn't support arrays of structs in structs.
> You'll need to pass it in as integers, and then break them up manually
> after the call...
>
> I think, but I haven't confirmed that this will be added in 2.0.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interop - returning a buffer through callback Udi Microsoft C# .NET 0 19th Apr 2007 11:27 AM
Returning array of strings through callback from unmanaged to mana =?Utf-8?B?QnJ1Y2UgUGFya2Vy?= Microsoft Dot NET Compact Framework 5 23rd Apr 2006 12:39 AM
P/Invoke: method returning an array of Structures with char[] =?Utf-8?B?TGlvbmVsIFJleWVybw==?= Microsoft Dot NET Compact Framework 1 16th Feb 2006 11:18 PM
Script callback returning ViewState? Flinky Wisty Pomm Microsoft ASP .NET 2 2nd Dec 2005 09:29 AM
Array of structures from a COM callback. =?Utf-8?B?UmljaCBDbGF4dG9u?= Microsoft C# .NET 7 21st Sep 2005 07:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:22 PM.