非托管函数的调用

为什么非托管函数在VB.NET中的模块级调用会出现一些异常,但是在类中调用确是正常的?
代码:
Module Module1
    ' 定义 LOGFONT 结构
    Public Const GB2312_CHARSET = 134
    Public Const DEFAULT_CHARSET = 1
    Public Const DEFAULT_QUALITY = 0
    Public Const DEFAULT_PITCH = 0
    Public Const FF_DONTCARE = 2
    Public Const OUT_CHARACTER_PRECIS = 2
    Public Const CLIP_CHARACTER_PRECIS = 1
    Public Const LF_FACESIZE = 32
    Public Const FW_NORMAL = 400
    <StructLayout(LayoutKind.Sequential)> _
    Public 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
 <DllImport("GDI32.dll")> _
        Public Shared Function CreateFontIndirect(ByRef lf As LOGFONT) As IntPtr
        End Function

Public Sub Main()
            'Create a LOGFONT structure
            Dim lf As LOGFONT
            With lf
                .lfHeight = CInt(Me.TextBox2.Text)
                .lfWidth = CInt(Me.TextBox3.Text)
                .lfCharSet = 1
                .lfEscapement = CInt(Me.TextBox4.Text)
                .lfFaceName = fName
                .lfUnderline = 1
                .lfStrikeOut = 1
            End With

            ' Create a Font Pointer
            Dim hfont As IntPtr = CreateFontIndirect(lf)
'以下代码省略
End Sub
End Module
此时的非托管函数调用会出现异常
如果修改为:
Module Module1
    <StructLayout(LayoutKind.Sequential)> _
    Public 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
 Class LibWrap
        <DllImport("GDI32.dll")> _
        Public Shared Function CreateFontIndirect(ByRef lf As LOGFONT) As IntPtr
        End Function

    End Class
Public Sub Main()
            Dim lf As LOGFONT
            With lf
                .lfHeight = CInt(Me.TextBox2.Text)
                .lfWidth = CInt(Me.TextBox3.Text)
                .lfCharSet = 1
                .lfEscapement = CInt(Me.TextBox4.Text)
                .lfFaceName = fName
                .lfUnderline = 1
                .lfStrikeOut = 1
            End With

            ' Create a Font Pointer
            Dim hfont As IntPtr = LibWrap.CreateFontIndirect(lf)
'以下代码省略
End Sub
End Model1
此时的非托管函数调用则是正常的。

posted @ 2004-06-22 22:00  Snowolf_Studio  阅读(687)  评论(0编辑  收藏  举报