12 Replies - 2151 Views - Last Post: 03 February 2013 - 02:19 PM Rate Topic: -----

#1 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Get windows product Key

Posted 02 February 2013 - 07:23 AM

Hi, i have this code for retrieving the windows product key of a machine that i got online.

Imports Microsoft.Win32
Module Module1
    Public strkey As String
    Public Function sGetXPKey() As String
        'Open the Registry Key and then get the value (byte array) from the SubKey
        Dim RegKey As RegistryKey = _
        Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion", False)
        Dim bytDPID() As Byte = RegKey.GetValue("DigitalProductID")
        'Transfer only the needed bytes into our Key Array
        ' Key starts at byte 52 and is 15 bytes long.
        Dim bytKey(14) As Byte '0-14 = 15 bytes
        Array.Copy(bytDPID, 52, bytKey, 0, 15)
        'Our "Array" of valid CD-Key characters
        Dim strChar As String = "BCDFGHJKMPQRTVWXY2346789"
        'Finally, our decoded CD-Key to be returned

        'How Microsoft encodes this to begin with, I'd love to know...
        'but here's how we decode the byte array into a string
        'containing the CD-KEY.
        For j As Integer = 0 To 24
            Dim nCur As Short = 0
            For i As Integer = 14 To 0 Step -1
                nCur = CShort(nCur * 256 Xor bytKey(i))
                bytKey(i) = CByte(Int(nCur / 24))
                nCur = CShort(nCur Mod 24)
            Next
            strKey = strChar.Substring(nCur, 1) & strKey
        Next
        'Finally, insert the dashes into the string.
        For i As Integer = 4 To 1 Step -1
            strKey = strKey.Insert(i * 5, "-")
        Next
        Return strKey
    End Function
End Module



The problem is, it's not working...? Not an error, nothing, just empty.
Shouldn't that work?

Thanks for anyone who helps.

Is This A Good Question/Topic? 0
  • +

Replies To: Get windows product Key

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,356
  • Joined: 02-June 10

Re: Get windows product Key

Posted 02 February 2013 - 07:51 AM

Are you familiar with breakpoints and walking through the execution of your code line by line? I would expect basic debugging techniques should let you view each variable at runtime and see exactly what is and is not happening.


Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?
A:
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2878
  • View blog
  • Posts: 9,550
  • Joined: 12-December 12

Re: Get windows product Key

Posted 02 February 2013 - 08:11 AM

Have you checked with regedit that the key is in that location?

LOCAL MACHINE Software\Microsoft\Windows NT\CurrentVersion

Otherwise, it might be a permission issue; that is, you need to execute the code with admin privileges.

[Of course, we are assuming that you have a legitimate reason to obtain the key.]

This post has been edited by andrewsw: 02 February 2013 - 08:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Get windows product Key

Posted 02 February 2013 - 08:24 AM

It's only for gathering different types of information of my machine, for legitimate reasons... yes, the key is there (i've checked ofc) and i've run it with admin privileges, and still, no working... i'm putting in a msgbox the "strkey" variable in my form1... and shows nothing.

Any help?

I'm not to familiar with breakpoints, but if i don't find a solution, i will read more about it, thanks.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,356
  • Joined: 02-June 10

Re: Get windows product Key

Posted 02 February 2013 - 08:31 AM

View PostHeBTeMy, on 02 February 2013 - 09:24 AM, said:

I'm not to familiar with breakpoints, but if i don't find a solution, i will read more about it, thanks.


Dude... Stop working on the project and take an hour to learn how to do some basic debugging. Otherwise throwing this up here as "I don't know what's wrong" is really just saying "I couldn't be bothered with learning how to debug my own code - here, you do it for me."


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Jan 2013
Spoiler



Was This Post Helpful? 0
  • +
  • -

#6 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Get windows product Key

Posted 02 February 2013 - 08:35 AM

Okay, i'll do it, thanks.
Was This Post Helpful? 0
  • +
  • -

#7 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 154
  • View blog
  • Posts: 570
  • Joined: 01-February 13

Re: Get windows product Key

Posted 02 February 2013 - 09:13 AM

Hi HeBTeMy,
I would go through the links that tlhIn`toq gave above to get a good grip on debugging. It never hurts to learn how to do new things. Anyways if you don`t use the correct command to get the string it will show that there was no errors and you will still have an empty string. For example if you want it to put the key into a textbox try this in your main form in the event that gets the key. It should work. If not you have problems somewhere else.

TB_Key.Text = Module1.sGetXPKey

Was This Post Helpful? 0
  • +
  • -

#8 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Get windows product Key

Posted 02 February 2013 - 09:57 AM

I've tried that and it gives me on this line:
 Array.Copy(bytDPID, 52, bytKey, 0, 15)


ArgumentnullException was unhandled
The value cannot be null
Name of the parameter: source

I'm sorry if i type anything wrong, cause this is in my main language, Portuguese, and i translated myself the last 2 sentences.

Thanks anyway.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2878
  • View blog
  • Posts: 9,550
  • Joined: 12-December 12

Re: Get windows product Key

Posted 02 February 2013 - 10:06 AM

This says the first (source) parameter (of the Copy method) bytDPID is null, so it is not reading "DigitalProductID" from the RegistryKey.

Added: If I try this
?My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion","DigitalProductId", Nothing)

in the Immediate Window it just returns Nothing, even though the key is present. So I still suspect it is a permissions issue.

Again, if you don't know how to use the Immediate Window, then study debugging as previously advised.

This post has been edited by andrewsw: 02 February 2013 - 10:13 AM

Was This Post Helpful? 0
  • +
  • -

#10 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Get windows product Key

Posted 02 February 2013 - 11:40 AM

But how it could be? Even if i run it as admin... it shows nothing, and i tried to put the "True" in front of the regkey path, and then it gives me no permition to access it in VS, but if i run as admin, it gives the same error.. (The null one)

This post has been edited by HeBTeMy: 02 February 2013 - 11:40 AM

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2878
  • View blog
  • Posts: 9,550
  • Joined: 12-December 12

Re: Get windows product Key

Posted 02 February 2013 - 11:53 AM

Try this WMI approach instead. You probably need to Add Reference to System.Management (Project menu, Add Reference).

Actually, this gives me the ProductID, but isn't that the same thing? It's just that DigitalProductId is in binary format.

This post has been edited by andrewsw: 02 February 2013 - 12:07 PM

Was This Post Helpful? 0
  • +
  • -

#12 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Get windows product Key

Posted 03 February 2013 - 05:27 AM

Yeah, i wanted the product key that is under my laptop, it shoes the productid like you said.
Was This Post Helpful? 0
  • +
  • -

#13 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Get windows product Key

Posted 03 February 2013 - 02:19 PM

Btw, i tested it on my old pc (desktop one) and it shows the key with that code... must be win7, i just don't know why, the path is the same, privileges were given, what am i missing?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1