将vbscript转换为vb.net [英] Converting vbscript to vb.net

查看:84
本文介绍了将vbscript转换为vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我之前在网上发现这个脚本,我不能完全转换它所以它也可以在vb.net中工作,我正试图从注册表中获取Windows序列号以进行聚会信息app。



vbscript:

Hello, i found this script online sometime ago, and i can''t convert it entirely so it can work in vb.net too, i''m trying to get the windows serial number from the registry for a "gathering information" app.

vbscript:

const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
strValueName = "DigitalProductId"
strComputer = "."
dim iValues()
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
      strComputer & "\root\default:StdRegProv")
oReg.GetBinaryValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,iValues
Dim arrDPID
arrDPID = Array()
For i = 52 to 66
ReDim Preserve arrDPID( UBound(arrDPID) + 1 )
arrDPID( UBound(arrDPID) ) = iValues(i)
Next

Dim arrChars
arrChars = Array("B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9")

For i = 24 To 0 Step -1
k = 0
For j = 14 To 0 Step -1
 k = k * 256 Xor arrDPID(j)
 arrDPID(j) = Int(k / 24)
 k = k Mod 24
Next
strProductKey = arrChars(k) & strProductKey
' <------- add the "-" between the groups of 5 Char -------->
If i Mod 5 = 0 And i <> 0 Then strProductKey = "-" & strProductKey
Next
strFinalKey = strProductKey
'

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
   ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
   strOS   = objOperatingSystem.Caption
   strBuild   = objOperatingSystem.BuildNumber
   strSerial   = objOperatingSystem.SerialNumber
   strRegistered  = objOperatingSystem.RegisteredUser
Next
Set wshShell=CreateObject("wscript.shell")
strPopupMsg = strOS & vbNewLine & vbNewLine
strPopupMsg = strPopupMsg & "Build Number:  " & strBuild & vbNewLine
strPopupMsg = strPopupMsg & "PID:  " & strSerial & vbNewLine & vbNewLine
strPopupMsg = strPopupMsg & "Registered to:  " & strRegistered & vbNewLine & vbNewLine & vbNewLine
strPopupMsg = strPopupMsg & "Your Windows Product Key is:" & vbNewLine & vbNewLine & strFinalKey
strPopupTitle = "Microsoft Windows License Information"
wshShell.Popup strPopupMsg,,strPopupTitle,vbCancelOnly+vbinformation
WScript.Quit





vb.net:



vb.net:

Dim iValues() As Integer
        oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
              My.Computer.Name & "\root\default:StdRegProv")
        oReg.GetBinaryValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductID", False)
        Dim arrDPID() As Int32
        Dim k As Int32 = 0

        ReDim arrDPID(50)
        For i = 52 To 66
            ReDim Preserve arrDPID(UBound(arrDPID) + 1)
            arrDPID(UBound(arrDPID)) = iValues(i)
        Next
        Dim arrChars(50) As Char
        Dim strProductKey As String
        Dim strFinalKey As String
        arrChars = {"B", "C", "D", "F", "G", "H", "J", "K", "M", "P", "Q", "R", "T", "V", "W", "X", "Y", "2", "3", "4", "6", "7", "8", "9"}
        For i = 24 To 0 Step -1
            k = 0
            For j = 14 To 0 Step -1
                k = k * 256 Xor arrDPID(j)
                arrDPID(j) = Int(k / 24)
                k = k Mod 24
            Next
            strProductKey = arrChars(k) & strProductKey
            If i Mod 5 = 0 And i <> 0 Then strProductKey = "-" & strProductKey
        Next
        strFinalKey = strProductKey
        '

        Dim objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & My.Computer.Name & "\root\cimv2")
        Dim colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
       
        For Each objOperatingSystem In colOperatingSystems
            Dim strOS As String
            Dim strBuild As String
            Dim strSerial As String
            Dim strRegistered As String
            strOS = objOperatingSystem.Caption
            strBuild = objOperatingSystem.BuildNumber
            strSerial = objOperatingSystem.SerialNumber
            strRegistered = objOperatingSystem.RegisteredUser
        Next





但我在第2行和第3行遇到运行时错误,说我无法创建activeX componen t。

有什么帮助吗?谢谢



but i get an runtime error at the 2nd and 3rd line saying that i can''t create an activeX component.
any help? thanks

推荐答案

一开始你写的:

oReg =对象...

你是否申报了oReg正确并初始化它?
At the start you write:
oReg = Object...
Did you declare oReg correctly and initialized it?


这篇关于将vbscript转换为vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆