Da ich von nem Freund um Hilfe bei ner art Produktaktivierung gefragt wurde, hab ich schnell die folgenden Code-Schnipsel ergooglet.
ACHTUNG! Man sollte den Key auf keinen Fall einfach mal so übers Internet übertragen, sondern unbedingt vorher hashen. (MD5, etc)
Dafür muss man den Key eigentlich auch nicht von den Zahlen in die Zeichen übersetzen.
ACHTUNG! Es muss in den AGB / der Lizenz / Privacy-Statement stehen, dass zu irgendwelchen Zwecken eine Kennung des Computers / der Windows-Installation übertragen wird.
Der Code funktioniert auch problemlos, ohne Admin-Rechte, unter Windows Vista. (Dank an Dennis Alexander)
VB.net-Code
Folgenden Code in eine leere VB-Datei kopieren und dann mit Win32.WindowsCDKey bzw. Win32.WindowsCDKeyParts aufrufen.
Original (C#): dotnet-snippets.de
VB6-Code
Folgenden Code in ein Modul kopieren und dann eine der beiden Funktionen aufrufen.
Original: s.o. + codeproject.com
Keywords: Visual Basic, VB6, VB.net, C#, C++, Windows CD-Key, Registry, Produktaktivierung
ACHTUNG! Man sollte den Key auf keinen Fall einfach mal so übers Internet übertragen, sondern unbedingt vorher hashen. (MD5, etc)
Dafür muss man den Key eigentlich auch nicht von den Zahlen in die Zeichen übersetzen.
ACHTUNG! Es muss in den AGB / der Lizenz / Privacy-Statement stehen, dass zu irgendwelchen Zwecken eine Kennung des Computers / der Windows-Installation übertragen wird.
Der Code funktioniert auch problemlos, ohne Admin-Rechte, unter Windows Vista. (Dank an Dennis Alexander)
VB.net-Code
Folgenden Code in eine leere VB-Datei kopieren und dann mit Win32.WindowsCDKey bzw. Win32.WindowsCDKeyParts aufrufen.
VB.NET-Quellcode
- Imports Microsoft.Win32
- Imports System
- Public Class Win32
- #Region "Properties"
- ''' <summary>
- ''' Reads the Windows CD key from the registry and returns it as string separated by '-' chars.
- ''' </summary>
- Public Shared ReadOnly Property WindowsCDKey() As String
- Get
- Dim rKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion")
- Dim rpk As Byte() = rKey.GetValue("DigitalProductId", 0)
- Dim strKey As String = ""
- Const iRPKOffset As Integer = 52
- Const strPossibleChars As String = "BCDFGHJKMPQRTVWXY2346789"
- Dim i As Integer = 28
- Do
- Dim lAccu As Long = 0
- Dim j As Integer = 14
- Do
- lAccu *= 256
- lAccu += Convert.ToInt64(rpk(iRPKOffset + j))
- rpk(iRPKOffset + j) = Convert.ToByte(Convert.ToInt64(Math.Floor(CSng(lAccu) / 24F)) And Convert.ToInt64(255))
- lAccu = lAccu Mod 24
- j -= 1
- Loop While j >= 0
- i -= 1
- strKey = strPossibleChars(CInt(lAccu)).ToString() + strKey
- If (0 = ((29 - i) Mod 6)) AndAlso (-1 <> i) Then
- i -= 1
- strKey = "-" + strKey
- End If
- Loop While i >= 0
- Return strKey
- End Get
- End Property
- ''' <summary>
- ''' Reads the Windows CD key from the registry and returns it as string array.
- ''' </summary>
- Public Shared ReadOnly Property WindowsCDKeyParts() As String()
- Get
- Dim strKeyParts As String() = WindowsCDKey.Split("-"C)
- Return strKeyParts
- End Get
- End Property
- #End Region
- End Class
VB6-Code
Folgenden Code in ein Modul kopieren und dann eine der beiden Funktionen aufrufen.
VB.NET-Quellcode
- Option Explicit
- 'APIs
- Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
- (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
- ByVal samDesired As Long, 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 Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
- '--
- 'Consts
- Const ERROR_SUCCESS As Long = 0&
- Const HKEY_LOCAL_MACHINE As Long = &H80000002
- Const NT_CURRENT As String = "SOFTWARE\MICROSOFT\Windows NT\CurrentVersion"
- Const REG_OPTION_NON_VOLATILE = &H0
- Const KEY_QUERY_VALUE = &H1
- Const KEY_SET_VALUE = &H2
- Const KEY_CREATE_SUB_KEY = &H4
- Const KEY_ENUMERATE_SUB_KEYS = &H8
- Const KEY_NOTIFY = &H10
- Const KEY_CREATE_LINK = &H20
- Const KEY_ALL_ACCESS = KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY _
- Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK
- '--
- 'Functions
- 'Reads the Windows CD key from the registry and returns it as string separated by '-' chars.
- Public Function WindowsCDKey() As String
- Dim hRegistryKey As Long
- If RegOpenKeyEx(HKEY_LOCAL_MACHINE, NT_CURRENT, REG_OPTION_NON_VOLATILE, _
- KEY_ALL_ACCESS, hRegistryKey) = ERROR_SUCCESS Then
- Dim rpk(163) As Byte
- Dim DataLength As Long: DataLength = 164
- If RegQueryValueEx(hRegistryKey, "DigitalProductId", _
- 0, 0, ByVal VarPtr(rpk(0)), DataLength) = ERROR_SUCCESS Then
- Dim strKey As String
- Const iRPKOffset As Integer = 52
- Dim strPossibleChars As String: strPossibleChars = "BCDFGHJKMPQRTVWXY2346789"
- Dim i As Integer: i = 28
- Do
- Dim lAccu As Long: lAccu = 0
- Dim j As Integer: j = 14
- Do
- lAccu = lAccu * 256
- lAccu = lAccu + CLng(rpk(iRPKOffset + j))
- rpk(iRPKOffset + j) = CByte(Int(CSng(lAccu) / 24#) And CLng(255))
- lAccu = lAccu Mod 24
- j = j - 1
- Loop While j >= 0
- i = i - 1
- strKey = Mid$(strPossibleChars, lAccu + 1, 1) & strKey
- If (0 = ((29 - i) Mod 6)) And (-1 <> i) Then
- i = i - 1
- strKey = "-" + strKey
- End If
- Loop While i >= 0
- WindowsCDKey = strKey
- End If
- RegCloseKey hRegistryKey
- End If
- End Function
- 'Reads the Windows CD key from the registry and returns it as string array.
- Public Function WindowsCDKeyParts() As String()
- Dim strKeyParts() As String
- strKeyParts = Split(WindowsCDKey, "-")
- WindowsCDKeyParts = strKeyParts
- End Function
- '--
Keywords: Visual Basic, VB6, VB.net, C#, C++, Windows CD-Key, Registry, Produktaktivierung
Dieser Beitrag wurde bereits 6 mal editiert, zuletzt von „Mad Andy“ ()