GetHashCode of string return different values on different OS

Posted on 2011-04-30
Last Modified: 2012-05-11

I have a 3.5 desktop application. This uses couple of .net dll which are also written in 3.5. These dll have common funtions. Now I wanted that these dlls should not be used by other people where this desktop app is installed. So whenever I create instance of this dll, I pass a hard-coded string as "MyApp_Protected_2000". This string is passed from my win form to the constructor of the dll. The constructor of the dll uses .GetHashCode to get the hashcode of this string. I compare this hashcode return value which I have pre-generated in development environment (XP with 32-bit OS) and compare it like this:

========= Class Code ===================
If strValidate.Trim.GetHashCode <> -5999234 then      
      Throw new exception "Unauthorized User"

This way I ensured that noone can use this dll.

But now I find that if this app is used on Windows 7 (64-bit), the GetHashCode returns different value other than -5999234  for the same string "MyApp_Protected_2000".

a. Why is this so? And how can I resolve this issue? What can I do to ensure that the string returns same value no matter what CLR version and OS versiion?

b. Or should I find the encrypted value of "MyApp_Protected_2000" in design-time and compare with that? The only problem I see here is that the encrypted value would have some special chars and I am afraid that might break the app. Also now I have doubt that even the encryption function may return different values on 32-bit and 64-bit OS.

Please help.


Question by:vj_mi
    LVL 83

    Accepted Solution

    LVL 10

    Assisted Solution

    Hashcode do vary by versions, as is explained in the MS article.

    I guess the only way round this would be to write your own code for creating the hashes.


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now