locked
SetComputerName C# Vista

    Question

  • Here's the Code (all of it)

    using Microsoft.Win32;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Windows.Forms;

    namespace SetCN
    {
        public partial class frmIdentifyFr : Form
        {
            //[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
            //static extern bool SetComputerNameEx(COMPUTER_NAME_FORMAT NameType, string lpBuffer);
            [DllImport("Kernel32.dll", SetLastError=true, CharSet = CharSet.Auto)]
            public static extern Boolean SetComputerName(string lpBuffer);
            enum COMPUTER_NAME_FORMAT
            {
                ComputerNameNetBIOS,
                ComputerNameDnsHostname,
                ComputerNameDnsDomain,
                ComputerNameDnsFullyQualified,
                ComputerNamePhysicalNetBIOS,
                ComputerNamePhysicalDnsHostname,
                ComputerNamePhysicalDnsDomain,
                ComputerNamePhysicalDnsFullyQualified,
            }
            public frmIdentifyFr()
            {

                InitializeComponent();
            }

            private void frmIdentifyFr_Load(object sender, EventArgs e)
            {
                cboRo.SelectedIndex = -1;
                cboFr1.SelectedIndex = -1;
                cboFr2.SelectedIndex = -1;
                cboFr3.SelectedIndex = -1;
                cboRo.Focus();
            }

            private void btnSetCompName_Click(object sender, EventArgs e)
            {
                if (cboRo.SelectedIndex < 0 | cboFr1.SelectedIndex < 0 | cboFr2.SelectedIndex < 0 | cboFr3.SelectedIndex < 0)
                {
                    MessageBox.Show("You must select from the dropdown in each box.", "Incomplete Entry");
                    cboRo.Focus();
                }
                else
                {
                    string computerName = "";
                    computerName = cboRo.Text.Substring(0, 1) + cboFr1.Text + cboFr2.Text + cboFr3.Text;
                    DialogResult mbButton = MessageBox.Show("You are about to set the Computer Name to " + computerName + ".  \nAre you sure your Computer Name is correct?", "Set Computer Name", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                    if (mbButton == DialogResult.Yes)
                    {
                       
                        //bool compNameSet = SetComputerNameEx(COMPUTER_NAME_FORMAT.ComputerNameNetBIOS, computerName);
                        bool compNameSet = SetComputerName(computerName);
                        if (compNameSet)
                        {
                            string setComputerNamePath = @"C:\WinCM\WinVersion\ComputerNameSet.txt";
                            FileStream scnFs = new FileStream(setComputerNamePath, FileMode.Create, FileAccess.Write);
                            StreamWriter computerNameOut = new StreamWriter(scnFs);
                            computerNameOut.Write(computerName);
                            computerNameOut.Close();
                        }
                        else
                        {
                            Int32 err = Marshal.GetLastWin32Error();
                            throw new Win32Exception(err);
                            MessageBox.Show("Computer Name NOT Set, you will need Administrator rights to accomplish this.", "Error Setting Computer Name");
                        }
                        this.Close();
                    }
                    else
                    {
                        cboRo.SelectedIndex = -1;
                        cboFr1.SelectedIndex = -1;
                        cboFr2.SelectedIndex = -1;
                        cboFr3.SelectedIndex = -1;
                        cboRo.Focus();
                    }
                }
            }
        }
    }


    So, I run this on XP, works like a champ.  I run this on Vista (Home or Business) and compNameSet comes back false (and it doesn't do the set) so I throw an exception for the LastWin32Error, which comes back Invalid handle.  If anyone has this working on Vista, I need a little help, Both Vistas are at least SP1.  VS 2008 SP1 and SDK 6.1. Running as Administrator on all machines tested.

    This has been resolved, in Vista need properties to be set to Run as Administrator.

    Thanks,
    Art
    Thursday, November 06, 2008 6:30 PM

Answers

  • This has been resolved, in Vista I needed properties to be set to Run as Administrator.

    Thanks,
    Art
    Thursday, November 06, 2008 10:44 PM

All replies

  • Please post this question here: http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=160&SiteID=1
    Thursday, November 06, 2008 8:28 PM
  • This has been resolved, in Vista I needed properties to be set to Run as Administrator.

    Thanks,
    Art
    Thursday, November 06, 2008 10:44 PM