Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
I have a console app and I am trying to get the windows dir.
I have the following code, but it says it is unbalancing the stack. I am desperate.
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

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

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

End Function

Thanks in advance,
iProgramIt

Edit:
Full code for changing logon screen wallpaper:
VB
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
Updated 7-May-15 3:26am
v2
Comments
ZurdoDev 7-May-15 9:04am    
What is the exact error? I would also recommend you use .Net instead of old windows api calls.

Try Environment.GetEnvironmentVariable("windir")

1 solution

Or...you could just ask .NET...
VB
Console.WriteLine(Environment.GetEnvironmentVariable("windir"))
 
Share this answer
 
Comments
iProgramIt 7-May-15 9:22am    
The logon screen background is what is not working. Whenever I change it, it does not work. Code:

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