Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all,
I have a code and it is to change the wallpaper. But it does not work. It is in a console and whenever I try, the stack unbalances. Could someone help me?
Thanks,
iProgramIt

My code:
VB
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

    ''' <summary>
    ''' Returns the path of the windows installation directory.
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetPath() As String
        On Error Resume Next

        'Create Buffer [
        GetPath = Space(255)
        ']

        'Return Windows Directory Path [
        Call GetWindowsDirectory(GetPath, Len(GetPath))

    End Function
    Public Function ApplyStyles(ByVal EnableStyles As Boolean, ByVal ImageToUse As String)
        'Check OS Version [
        If Not My.Computer.Info.OSFullName.StartsWith("Microsoft Windows 7") Then
            MessageBox.Show("You must be running Microsoft Windows 7 in order to use this program", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Exit Function
        End If
        ']

        'Get Windows Directory [
        Dim WindowsInstallPath As String
        WindowsInstallPath = GetPath().Trim().TrimEnd(Chr(0)) & "\"
        ']

        'Create Missing Folders [
        If Not My.Computer.FileSystem.DirectoryExists(WindowsInstallPath & "System32\oobe\info") Then
            My.Computer.FileSystem.CreateDirectory(WindowsInstallPath & "System32\oobe\info")
        End If
        If Not My.Computer.FileSystem.DirectoryExists(WindowsInstallPath & "System32\oobe\info\backgrounds") Then
            My.Computer.FileSystem.CreateDirectory(WindowsInstallPath & "System32\oobe\info\backgrounds")
        End If
        ']

        'Check For Blank Box [
        If ImageToUse.Trim = "" Then

            'Ask To Select [
            Dim MyResult As DialogResult = MessageBox.Show("There is no picture file selected, would you like to select one now?", "No File Selected", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
            ']

            'Confirmation [
            Select Case MyResult
                Case Windows.Forms.DialogResult.Yes

            End Select
            ']

            'Exit Method [
            Exit Function
            ']

        End If
        ']

        'Check File Exists [
        If Not My.Computer.FileSystem.FileExists(ImageToUse) Then

            'Show Error And Abort [
            MessageBox.Show("The image file specified does not exist, please ensure that it is present on your computer.", "Error - File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
            ']

        End If
        ']

        Try
            'Create Temporary File [
            Dim TemporaryName As String = FileIO.FileSystem.GetTempFileName
            ']

            'Reformat [
            Dim ChangeToJPEG As New Bitmap(ImageToUse)
            ']

            'Save Reformatted File [
            ChangeToJPEG.Save(TemporaryName, System.Drawing.Imaging.ImageFormat.Jpeg)
            ']

            'Release Bitmap [
            ChangeToJPEG.Dispose()
            ']

            'Get Temporary File Size [
            Dim MyInformation As New IO.FileInfo(TemporaryName)
            If MyInformation.Length > 262144 Then

                'Delete Temporary File [
                If MyInformation.Exists() Then
                    MyInformation.Delete()
                End If
                ']

                'Error [
                MessageBox.Show("The image file specified would be over 256KB when converted to a JPG file and too large to use. Please select another file.", "Error - File Too Large", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Function
                ']

            End If
            ']

            'Delete Temporary File [
            If MyInformation.Exists() Then
                MyInformation.Delete()
            End If
            ']

        Catch MyException As Exception

            'Format Error [
            MessageBox.Show("The image file specified is not of the correct format. Logon Background Changer cannot convert it to JPG format for use as a logon screen. Please select another file.", "Error - Incorrect Format", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
            ']

        End Try

        'Enable Or Disable Background [
        Dim MyEnableSelection As Integer
        If EnableStyles Then
            MyEnableSelection = 1
        Else
            MyEnableSelection = 0
        End If
        My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background", "OEMBackground", MyEnableSelection)
        ']

        'Finally Change Screen [
        Try

            'Reformat [
            Dim MyFinalPath As String = WindowsInstallPath & "System32\oobe\info\backgrounds\backgroundDefault.jpg"
            Dim ChangeToJPEG As New Bitmap(ImageToUse)
            ']

            'Delete Old File [
            If My.Computer.FileSystem.FileExists(MyFinalPath) Then
                My.Computer.FileSystem.DeleteFile(MyFinalPath)
            End If
            ']

            'Save Reformatted File [
            ChangeToJPEG.Save(MyFinalPath, System.Drawing.Imaging.ImageFormat.Jpeg)
            ']

            'Release Bitmap [
            ChangeToJPEG.Dispose()
            ']

            'Set Registry File Containing Path [
            My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\LogonBackgroundChanger", "PersistFile", ImageToUse)
            ']

        Catch MyException As Exception

            MessageBox.Show("The image file specified is not of the correct format. Logon Background Changer cannot convert it to JPG format for use as a logon screen. Please select another file.", "Error - Incorrect Format", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function

        End Try
        ']

        'Show Success [
        MessageBox.Show("The changes were successfully applied.", "Changes Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
        ']
        Return "good"
    End Function
Posted

1 solution

The stack gets screwed up because you're using VB6 Declarations. A Long type in VB6 is a 64-bit signed integer in VB.NET. Use Integer instead.
 
Share this answer
 
Comments
iProgramIt 7-May-15 23:35pm    
When I change it, it does not apply the changes.
iProgramIt 7-May-15 23:38pm    
Would you be able to post an alternative to this code? If so, thanks
Dave Kreskowiak 8-May-15 7:53am    
Nope. I've got my own huge project to write.
iProgramIt 8-May-15 8:26am    
What is that project? I am making A program called HackersConsole.
Dave Kreskowiak 8-May-15 9:36am    
Work stuff. It's basically my teams entire process, end to end. A large MVC5 website, 95% complete, 47 projects, ~175,000 lines of code and I'm half of the development team.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900