VB.NET pinvoke声明错了吗?

时间:2012-11-14 16:58:27

标签: vb.net pinvoke

我从pinvoke网站复制并粘贴了以下VB.NET结构。

http://www.pinvoke.net/default.aspx/Structures/BITMAPINFOHEADER.html

然而,当我将它粘贴到模块名称下的模块中时,VB.NET告诉我需要声明:

Option Strict Off
Option Explicit On

Imports System
Imports System.Diagnostics
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Module modDrawing

    StructLayout(LayoutKind.Explicit)>Public Structure BITMAPINFOHEADER
    <FieldOffset(0)> Public biSize As Int32
    <FieldOffset(4)> Public biWidth As Int32
    <FieldOffset(8)> Public biHeight As Int32
    <FieldOffset(12)> Public biPlanes As Int16
    <FieldOffset(14)> Public biBitCount As Int16
    <FieldOffset(16)> Public biCompression As Int32
    <FieldOffset(20)> Public biSizeImage As Int32
    <FieldOffset(24)> Public biXPelsperMeter As Int32
    <FieldOffset(28)> Public biYPelsPerMeter As Int32
    <FieldOffset(32)> Public biClrUsed As Int32
    <FieldOffset(36)> Public biClrImportant As Int32
End Structure

我哪里出错了,拜托? 非常感谢你。

2 个答案:

答案 0 :(得分:1)

看看这个:(不是完整的代码)

    Module modDrawing

        <StructLayout(LayoutKind.Explicit)> _
        Public Structure BITMAPINFOHEADER
            <FieldOffset(0)> Public biSize As Int32
            <FieldOffset(4)> Public biWidth As Int32
            <FieldOffset(8)> Public biHeight As Int32
            <FieldOffset(12)> Public biPlanes As Int16
            <FieldOffset(14)> Public biBitCount As Int16
            <FieldOffset(16)> Public biCompression As Int32
            <FieldOffset(20)> Public biSizeImage As Int32
            <FieldOffset(24)> Public biXPelsperMeter As Int32
            <FieldOffset(28)> Public biYPelsPerMeter As Int32
            <FieldOffset(32)> Public biClrUsed As Int32
            <FieldOffset(36)> Public biClrImportant As Int32
        End Structure

   End Module

答案 1 :(得分:0)

pinvoke.net代码中的错误是该属性缺少初始<。而不是

StructLayout(LayoutKind.Explicit)>

应该阅读

<StructLayout(LayoutKind.Explicit)>

然而,LayoutKind.Explicit在这里并不是非常明智。当然你可以指定所有的场偏移,但为什么让自己变得困难?使用LayoutKind.Sequential。由于这是默认值,因此您可以省略该属性。

Public Structure BITMAPINFOHEADER
    Public biSize As Int32
    Public biWidth As Int32
    Public biHeight As Int32
    Public biPlanes As Int16
    Public biBitCount As Int16
    Public biCompression As Int32
    Public biSizeImage As Int32
    Public biXPelsperMeter As Int32
    Public biYPelsPerMeter As Int32
    Public biClrUsed As Int32
    Public biClrImportant As Int32
End Structure