Results 1 to 13 of 13

Thread: [Resolved] Variable resets itself

  1. #1
    Hyperactive Member
    Join Date
    May 05
    Posts
    394

    [Resolved] Variable resets itself

    Hello,
    I have written an installer to install drivers and an application package. It's a simple program that launches other installers. At issue is the OS variable. This runs fine on an XP machine, but on W7 64 bit, I get screwy behavior. Upon load, the OS variable is initialized to 0. The OS is polled and OS is set to either 32 or 64, for 32 bit or 64 bit. The type of OS is then displayed in the title of the form, and on both XP and W7, it displays correctly. That is, 32 bit for my 32 bit XP machine and 64 bit for my co-workers 64 bit W7 machine. Now, when the Ok button is clicked, the OS variable is evaluated and if it's 0, the error message "Unable to determine OS bit width..." appears. Well, the error message should not appear at all because OS = 32 or 64, but on the W7 machine, it does appear. How in the world could OS be getting reset to 0?
    Driving me nuts!!
    Thanks.


    Code:
    Imports Microsoft.Win32
    Public Class Form1
        Private OS As Integer
    
        Private Sub BtnCancel_Click(sender As System.Object, e As System.EventArgs) Handles BtnCancel.Click
            Me.Close()
        End Sub
    
        Private Sub BtnOk_Click(sender As System.Object, e As System.EventArgs) Handles BtnOk.Click
            Me.Hide()
    
            If rbtnDriversOnly.Checked Or rbtnDriversAndApp.Checked Then
                If OS = 0 Then
                    MessageBox.Show("Unable to determine OS bit width.  Please manually install driver by browsing to the USB Drivers " _
                        & "folder on the CD and running DPInst_x86 (for 32 bit OS) or DPInst_x64 (for 64 bit OS).", "Error", _
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
                Else
                    Try
                        Select Case OS
                            Case 32
                                Shell("USB Drivers\DPInst_x86.exe", AppWinStyle.NormalFocus, True) 'launch, normal window, wait for completion
                            Case 64
                                Shell("USB Drivers\DPInst_x64.exe", AppWinStyle.NormalFocus, True) 'launch, normal window, wait for completion
                            Case Else
                        End Select
                    Catch ex As Exception
                        MessageBox.Show("Error launching USB Driver installation.  Please manually install driver by browsing to the USB Drivers " _
                            & "folder on the CD and running DPInst_x86 (for 32 bit OS) or DPInst_x64 (for 64 bit OS).", "Error", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            End If
    
            If rbtnDriversAndApp.Checked Or rbtnAppOnly.Checked Then
                Try
                    Shell("Application.exe", AppWinStyle.NormalFocus, False) 'launch application, normal window, don't wait for completion
                Catch ex As Exception
                    MessageBox.Show("Unable to launch Application.exe.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
    
            Me.Close()
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            rbtnDriversOnly.Checked = False
            rbtnDriversAndApp.Checked = False
            rbtnAppOnly.Checked = False
            BtnOk.Enabled = False
            BtnCancel.Enabled = True
            rbtnDriversOnly.Focus()
            OS = 0 'init to no detected OS
            Try
                If Registry.LocalMachine.OpenSubKey("Hardware\Description\System\CentralProcessor\0").GetValue("Identifier").ToString.Contains("x86") Then
                    OS = 32 '32 bit OS
                End If
                If Registry.LocalMachine.OpenSubKey("Hardware\Description\System\CentralProcessor\0").GetValue("Identifier").ToString.Contains("x64") Then
                    OS = 64 '64 bit OS
                End If
            Catch ex As Exception
            End Try
            If OS <> 0 Then 'if OS bit width detected, show it
                Me.Text = "USB Driver & App Installer - (" & OS.ToString & " Bit OS)"
            End If
            Timer1.Enabled = True
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            If rbtnDriversOnly.Checked Or rbtnDriversAndApp.Checked Or rbtnAppOnly.Checked Then
                BtnOk.Enabled = True
            Else
                BtnOk.Enabled = False
            End If
        End Sub
    End Class
    Last edited by rickford66; Aug 21st, 2012 at 06:05 PM.

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,196

    Re: Variable resets itself

    I can tell you right now that won't work for detecting all x64 processors. My current system has "Intel64 Family 6 Model 44 Stepping 2" in the identifier field.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  3. #3
    Hyperactive Member
    Join Date
    May 05
    Posts
    394

    Re: Variable resets itself

    Thanks for the tip. I'll have to check into that, but it's not the problem I'm having here. This program is correctly reporting the 64 bit machine, only to execute the if OS=0 statement just a bit later.

  4. #4
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,566

    Re: Variable resets itself

    Well, variables don't clear themselves automatically, so either you are doing it in code you didn't show (and probably forgot you wrote), or it never was anything other than 0. I can certainly see how it could get through that load event and remain 0 (if an exception is thrown, for instance), so how are you confirming that it is not 0 at the end of the load statement.

    EDIT: I guess I see how you are determining that it is correct, as you display it.
    My usual boring signature: Nothing

  5. #5
    Hyperactive Member
    Join Date
    May 05
    Posts
    394

    Re: Variable resets itself

    Shaggy, there is no more code. You are looking at the entire program.

  6. #6
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,196

    Re: Variable resets itself

    I can't see how you'd end up with 0 if it was set to either 32 or 64.

    I've simplified it down to setting the variable and then checking it on the button click and I don't see anywhere it could be reset to 0

    vb.net Code:
    1. Imports System.Management
    2.  
    3. Public Class Form1
    4.  
    5.     Private OS As Integer
    6.  
    7.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    8.  
    9.         OS = 0
    10.  
    11.         Dim query As String = "SELECT * FROM Win32_Processor"
    12.         For Each mo In New ManagementObjectSearcher(query).Get()
    13.             OS = mo.Properties("AddressWidth").Value.ToString()
    14.         Next
    15.  
    16.         Me.Text = OS.ToString() & "-bit OS"
    17.  
    18.     End Sub
    19.  
    20.     Private Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
    21.         Me.Hide()
    22.  
    23.         If OS = 0 Then
    24.             MessageBox.Show("Non determined OS architecture")
    25.         Else
    26.             Try
    27.                 Select Case OS
    28.                     Case 32
    29.                         MessageBox.Show("32-bit OS")
    30.                     Case 64
    31.                         MessageBox.Show("64-bit OS")
    32.                 End Select
    33.             Catch ex As Exception
    34.                 MessageBox.Show("Blah....")
    35.             End Try
    36.         End If
    37.     End Sub
    38. End Class
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  7. #7
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,566

    Re: Variable resets itself

    That's suggestive, though I see no way for the results you are reporting to occur. There are two things you might try (they are different variations on the same thing):

    1) Change the break model (on the Debug menu) to break when a runtime exception is thrown.

    2) Wrap the ENTIRE Load event in a Try...Catch block and throw up a message in the case of an exception.

    The fact that there is so little code, and that you are getting this exception on a Win7 64-bit machine, only, brings to mind the strange fact that exceptions thrown in the Load event will cause the Load event to cease without any visible sign. I realize that this shouldn't be possible to affect you, but it is bug-like behavior related to that particular OS, so the first step might be to try to identify if it is happening at all, then it might be possible to figure out how you are getting the results that you are. If you take those steps, and either one shows that you are getting an exception in the Load event....that could be fascinating.
    My usual boring signature: Nothing

  8. #8
    Hyperactive Member
    Join Date
    May 05
    Posts
    394

    Re: Variable resets itself

    After some experimenting, it turns out that things are not quite as I thought. MattP. called it on not reading the x64 correctly. No exceptions are thrown. On the 64 bit machine, there is no x64 in the key, so it should have never set OS to 64. OS is displayed in the title bar as 64 bit, but displaying the OS variable directly every time the timer fires shows it to have the value 0. So the real question is, how in the world did it ever get to be 64?

  9. #9
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,566

    Re: Variable resets itself

    Well, just to rule out the obvious: You didn't happen to hard code the title to something in the designer during testing did you?
    My usual boring signature: Nothing

  10. #10
    Hyperactive Member
    Join Date
    May 05
    Posts
    394

    Re: Variable resets itself

    Hahahahahahahahahahahaha... omg, I did exactly that. I thought I took that out. I'm so embarrassed. Thanks for pointing it out. Ok, I'm good to go now. Duh, what you must think of me.

  11. #11
    Hyperactive Member
    Join Date
    May 05
    Posts
    394

    Re: [Resolved] Variable resets itself

    BTW, thanks all for the help. I'm sure I'll get it working now.

  12. #12
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,566

    Re: [Resolved] Variable resets itself

    What I think of you? I think you're human.

    I spent all of yesterday morning chasing false leads trying to figure out a bug all because I tried to copy an .mdb file from one folder to another twice, and managed to fail both times without noticing. That made me think that the problem was not in the file, but in the program that accessed the file, because I was using a non-corrupted version rather than the corrupted version. It took me about three hours to figure out that I had copied the file to the wrong folder.
    My usual boring signature: Nothing

  13. #13
    Hyperactive Member
    Join Date
    May 05
    Posts
    394

    Re: [Resolved] Variable resets itself

    I did the 64 bit os thing just to check to see if it fit on the title bar, and was fully intending to remove it... but got sidetracked. Oh well, this problem is solved now. Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •