Link to home
Start Free TrialLog in
Avatar of Shane Russell
Shane RussellFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Converting this to vbscript ?

ok here is the code in visual basic to attain the windows xp cd key :

'======================

'**************************************
'Windows API/Global Declarations for :Vi
'     ew Windows XP CD Key
'**************************************
Option Explicit

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long ' Note that if you declare the lpData parameter as String, you must pass it By Value.
   Private Const REG_BINARY = 3
   Private Const HKEY_LOCAL_MACHINE = &H80000002
   Private Const ERROR_SUCCESS = 0&
'**************************************
' Name: View Windows XP CD Key
' Description:Function: sGetXPCDKey() wi
'     ll return the CD Key for Windows XP in t
'     he format XXXXX-XXXXX-XXXXX-XXXXX-XXXXX.
'
' By: Snytax
'
'
' Inputs:Nothing.
'
' Returns:Your Windows XP CD Key.
'
'Assumes:None
'
'Side Effects:None
'This code is copyrighted and has limite
'     d warranties.
'Please see http://www.Planet-Source-Cod
'     e.com/xq/ASP/txtCodeId.57164/lngWId.1/qx
'     /vb/scripts/ShowCode.htm
'for details.
'**************************************

'sGetXPCDKey() -
'Returns the Windows XP CD Key if succes
'     sful.
'Returns nothing upon failure.

Public Function sGetXPCDKey() As String
   'Read the value of:
   'HKLM\SOFTWARE\MICROSOFT\Windows NT\Curr
   '     entVersion\DigitalProductId
   Dim bDigitalProductID() As Byte
   Dim bProductKey() As Byte
   Dim ilByte As Long
   Dim lDataLen As Long
   Dim hKey As Long
   'Open the registry key: HKLM\SOFTWARE\MI
   '     CROSOFT\Windows NT\CurrentVersion

   If RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\MICROSOFT\Windows NT\CurrentVersion", hKey) = ERROR_SUCCESS Then
       lDataLen = 164
       ReDim Preserve bDigitalProductID(lDataLen)
       'Read the value of DigitalProductID

       If RegQueryValueEx(hKey, "DigitalProductId", 0&, REG_BINARY, bDigitalProductID(0), lDataLen) = ERROR_SUCCESS Then
           'Get the Product Key, 15 bytes long, off
           '     set by 52 bytes
           ReDim Preserve bProductKey(14)

           For ilByte = 52 To 66
               bProductKey(ilByte - 52) = bDigitalProductID(ilByte)
           Next ilByte
       Else
           'ERROR: Could not read "DigitalProductID
           '     "
           sGetXPCDKey = ""
           Exit Function
       End If
   Else
       'ERROR: Could not open "HKLM\SOFTWARE\MI
       '     CROSOFT\Windows NT\CurrentVersion"
       sGetXPCDKey = ""
       Exit Function
   End If
   'Now we are going to 'base24' decode the
   '     Product Key
   Dim bKeyChars(0 To 24) As Byte
   'Possible characters in the CD Key:
   bKeyChars(0) = Asc("B")
   bKeyChars(1) = Asc("C")
   bKeyChars(2) = Asc("D")
   bKeyChars(3) = Asc("F")
   bKeyChars(4) = Asc("G")
   bKeyChars(5) = Asc("H")
   bKeyChars(6) = Asc("J")
   bKeyChars(7) = Asc("K")
   bKeyChars(8) = Asc("M")
   bKeyChars(9) = Asc("P")
   bKeyChars(10) = Asc("Q")
   bKeyChars(11) = Asc("R")
   bKeyChars(12) = Asc("T")
   bKeyChars(13) = Asc("V")
   bKeyChars(14) = Asc("W")
   bKeyChars(15) = Asc("X")
   bKeyChars(16) = Asc("Y")
   bKeyChars(17) = Asc("2")
   bKeyChars(18) = Asc("3")
   bKeyChars(19) = Asc("4")
   bKeyChars(20) = Asc("6")
   bKeyChars(21) = Asc("7")
   bKeyChars(22) = Asc("8")
   bKeyChars(23) = Asc("9")
   Dim nCur As Integer
   Dim sCDKey As String
   Dim ilKeyByte As Long
   Dim ilBit As Long

   For ilByte = 24 To 0 Step -1
       'Step through each character in the CD k
       '     ey
       nCur = 0

       For ilKeyByte = 14 To 0 Step -1
           'Step through each byte in the Product K
           '     ey
           nCur = nCur * 256 Xor bProductKey(ilKeyByte)
           bProductKey(ilKeyByte) = Int(nCur / 24)
           nCur = nCur Mod 24
       Next ilKeyByte
       sCDKey = Chr(bKeyChars(nCur)) & sCDKey
       If ilByte Mod 5 = 0 And ilByte <> 0 Then sCDKey = "-" & sCDKey
   Next ilByte
   sGetXPCDKey = sCDKey
End Function

'======================

What I would like is it converted into vbscript so that I can carry it around on a floppy disk with me or something to that extent, because it would mean no runtime files, no installation or any of that and it would be a lot more useful to me then actually having an exe program to attain the key.

Thanks for the time and help !
Avatar of Rafaelkl
Rafaelkl

what kind of help did you want?
wanna someone to write vbscript for you? or you just want to get help to fulfil your needs?
Windows Script Host(cscript.exe/wscript.exe) support a WshShell object in script file, whose method  "RegRead/RegWrite/RegDelete" can access registry.
Avatar of Shane Russell

ASKER

I want some help converting this to vbscript
Avatar of Bob Learned
I think that you would have a problem, since VBScript cannot make API calls.  You could make an ActiveX control that you could instantiate from VBScript.

Bob
You can use WSH to read the registry values because I found examples of how to do that I just get stuck on how to read them correctly into the variables and how I would set it up so that it decodes the digitalproductid.

Unless you are reffering to something else that I am missing ?

If making an active x control is the only way, would you be able to help me with that or is that for another question ?
I am not saying that it is the only way, but it is one way.  Do you have VB6 development environment at your disposal?

Bob
Not on this laptop but I do have a desktop with visual basic 6 developement enviroment at my disposal yes :)
Create a DLL project, and put that code into a class module.  Build the DLL.

In VBScript:

Dim CDKeyReader
Dim CDKey

CDKeyReader = CreateObject(<progID of DLL>)

CDKey = CDKeyReader.sGetXPCDKey()

Bob

Where do I get the progID of DLL ? Also once I have made that, then what do I do with that ?
A little about ActiveX and ProgID that might help:

What is a ProgID and How Do I Change It?
http://www.vbaccelerator.com/progid.htm#sechow

You might be able to put this on a floppy disk (BTW what is a floppy?  *GRIN*), and reference it without having to register the ActiveX DLL as long as you put everything all in one directory and that constructs are simple.

BTW, I use Flash drives to distribute changes, since everyone here has Windows XP, and I haven't used a floppy in years.

Bob
once the dll is made, what do I do with that ??
You might be able to put this on a floppy disk and reference it without having to register the ActiveX DLL as long as you put it in the same folder as the VBScript file.  Try it.

Bob
Ive got the dll made and ive got that coding in a vbscript file, however Im not sure it will work because I have no clue on how to get or attain the ProgID, please can you explain step by step how I do that because that site didnt make a bit of sense to me
A Visual Basic ProgID is a unique identifier which is constructed through the combination of the DLL's project and object names (ProjectName.ObjectName).

I don't use VB6 anymore (I used it for about 8 years), and COM stuff is slowly slipping from my grasp, so forgive me if this takes longer than usual to answer.

Bob

I have never done any creating of dll's so in turn have no idea how to find out or attain the ProgID for the piece of coding within the vbscript, I just need to know what id to insert in that coding so that I can get it to work.
If you had a scenario like this:

Project name = NetworkFunctions
Class name = CDKeyReader

Then, then ProgID should be NetworkFunctions.CDKeyReader.  Also, the CDKeyReader class attribute would need to be marked as Multi-Use.

Bob
ok that does not help much because I tried doing that and its just not working, any chance you could try and convert it, I mean even if you converted half of it and I will post a new question with the other half of the script to be converted or something !! ?? !!
ASKER CERTIFIED SOLUTION
Avatar of Colosseo
Colosseo
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks scott, that is exactly what I wanted, this way I dont have to create any exe's or install runtime files or nothing :)

Spot on with that conversion scott :) I was trying to make the msgbox work within the function and it didnt work then I put the msgbox outside of the function and it works a treat !!

Thanks so much !!
No bother and thanks for the grade

Cheers

Scott