调用GetProductInfo时出现PInvokeStackImbalance错误[VB.NET]

时间:2011-08-24 09:00:35

标签: vb.net

我在Win 7 Ultimate上测试它,它会产生错误:

  

检测到PInvokeStackImbalance消息:调用PInvoke函数   'WindowsApplication1!WindowsApplication1.Form1 :: GetProductInfo'有   堆栈不平衡。这可能是因为托管PInvoke   签名与非托管目标签名不匹配。检查一下   调用约定和PInvoke签名匹配的参数   目标非托管签名。

Public Class Form1


   Private Const PRODUCT_STARTER As Integer = &HB                            'Starter
   Private Const PRODUCT_HOME_BASIC As Integer = &H2                         'Home Basic
   Private Const PRODUCT_HOME_BASIC_N As Integer = &H5                       'Home Basic N
   Private Const PRODUCT_HOME_PREMIUM As Integer = &H3                       'Home Premium
   Private Const PRODUCT_HOME_PREMIUM_N As Integer = &H1A                    'Home Premium N
   Private Const PRODUCT_BUSINESS As Integer = &H6                           'Business
   Private Const PRODUCT_BUSINESS_N As Integer = &H10                        'Business N
   Private Const PRODUCT_ENTERPRISE As Integer = &H4                         'Enterprise
   Private Const PRODUCT_ENTERPRISE_N As Integer = &H1B                      'Enterprise N
   Private Const PRODUCT_ULTIMATE As Integer = &H1                           'Ultimate
   Private Const PRODUCT_ULTIMATE_N As Integer = &H1C                        'Ultimate N

   Private Declare Function GetProductInfo Lib "kernel32" _
      (ByVal dwOSMajorVersion As Long, ByVal dwOSMinorVersion As Long, _
      ByVal dwSpMajorVersion As Long, ByVal dwSpMinorVersion As Long, _
      ByRef pdwReturnedProductType As Long) As Long

   Public Function GetOSProduct() As String
      Dim os As OperatingSystem = Environment.OSVersion
      Dim prodType As Long
      GetOSProduct = ""
      If GetProductInfo(os.Version.Major, os.Version.Minor, 0, 0, prodType) Then
         Select Case prodType
            Case PRODUCT_ULTIMATE
               GetOSProduct = "Ultimate"
            Case PRODUCT_ULTIMATE_N
               GetOSProduct = "Ultimate N"
            Case Else
               GetOSProduct = "unknown"
         End Select
      End If
   End Function

   Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      MsgBox(GetOSProduct)
   End Sub
End Class

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

(在VB6中使用)替换为整数(在VB.NET中使用)