Thursday, January 21, 2010

Get Client CPU ID with Javascript

For whatever purpose it may serve, here's how to get the client CPU ID through javascript. Just a warning though, this script uses ActiveX object which is disabled by default on browsers and I'm not sure if you can enable it on non-IE. So for now we'll just do it with Internet Explorer.


Go to Tools then Internet Options.





Then on the Security tab click on "Custom Level..."





Go down until you see "Initialize and script ActiveX controls not marked as safe..." and enable it then click Ok.


Now we go to the scripting part. Add this next snippet in your html page.



<script type="text/javascript">
    var str = "";
    var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}");
    e = new Enumerator(wmi.InstancesOf("Win32_Processor"));
    for(; !e.atEnd(); e.moveNext()) {
        var s = e.item();
        str = s.ProcessorID;
    }
</script>


You can add the script above anywhere on your html head or body, but if you chose to put it on the body be sure to place it anywhere above this next snippet, which just displays the CPU ID in the page.


<b>Client CPU ID : </b><span id="spanCPUID" />

<script type="text/javascript">

    document.getElementById("spanCPUID").innerHTML = str;
</script>
                        


And that's it, you should now see the client computer's cpu id when you run the page.


-k

6 comments:

  1. Hi Elyk! It's a really useful post, unfortunately I'm having a problem trying to make it run in my application. I'm getting the following message error: The automatization server couldn't create the object. I'm using IE8 and I've tried to change the security settings, but the error continues. Could you please help me with this?. Any suggestion will be really appreciated.

    Thanks!

    Rose

    ReplyDelete
  2. Hi Rose,

    You could try enabling other options in the Security Settings like "Run ActiveX controls and plug-ins" or "Download unsigned ActiveX controls"..

    But be careful on these, it could leave your computer vulnerable to viruses or whatnot.. I assume you're testing this for local purposes only.. and also just so you know, I've found out that CPU ID's are no longer unique these days so if you're looking to get unique identification for your client PC's I think this is not the best choice.

    ReplyDelete
  3. Hi Elyk, thanks for your answer. I am happy to tell you that now it's working fine. Thank you. But I have one more question, you mentioned in your reply that "you assume I'm testing this for local purposes only". Does that mean that it won't work remotely? Because what I have to do is develop a program that could recognize who is the client at the other side (getting the MAC Address as you showed in your other post). Can this be done remotely with this ActiveX object?

    Thanks again, hope you may answer!

    Rose

    ReplyDelete
  4. I mean you should only use it for intranet (local area network) systems. But yes it will work remotely.

    The only problem is if ever you will use this approach over the internet, you would leave your clients vulnerable to hacker attacks by enabling activeX.

    ReplyDelete
  5. hello i want to get clients CPU id , by JavaScript , but couldn't do so . kindly help me out .

    ReplyDelete
  6. Hi,
    It is working fine when I run this on my local machine, but after hosting the application, its not work . why?
    Please help me about this?

    ReplyDelete