You're welcome. I had been searching for what AnyCPU actually did a couple days ago and this thread was one of the first hits on google, so now that I know I figured I should update it for future searchers.
jessemtcarlton
Posts
-
Invalid Cast Exception Occurs when changing target cpu option in VS. -
Invalid Cast Exception Occurs when changing target cpu option in VS.The problem you're having is from the way x64 windows runs x86 code. The x64 regedit and the x86 regedit point to different hives. x64 windows running x86 code will actually substitute a 32bit registry in much the same way it substitutes the 32bit Program files directory. Your program is actually accessing the key at HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion when running in x86 mode, instead of HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion that it gets in anyCPU or x64 mode, and since the DigitalProductId key doesn't exist there, you're getting your default value which is 0. That's where the System.Int32 comes from. If you run yor program on an x86 OS it will work, it's only failing because you're specifying x86 on an x64 OS. Leave it on AnyCPU and you should be fine. The code that VS compiles doesn't change when you change the Target CPU, it only changes a header flag that the interpreter reads and forces it to use the x86 interpreter.