使用未分配的局部变量“SHELLEXECUTEINFO”

时间:2014-02-19 15:40:03

标签: c# .net winforms compiler-errors

大家好,我在调试用c#编写的应用程序时遇到错误 这是错误:

使用未分配的本地变量“SHELLEXECUTEINFO”

这是错误行cbSize = Marshal.SizeOf(sHELLEXECUTEINFO),

我不知道为什么我会收到此错误或如何纠正错误。

private bool PWOLaunch(int iTab)
    {
        string pWOLocation = Settings.Default.PWOLocation;
        if (!File.Exists(pWOLocation))
        {
            this.openFileDialog1.ShowDialog();
            pWOLocation = this.openFileDialog1.FileName;
            if (!pWOLocation.Contains("PWO.exe"))
            {
                MessageBox.Show("Failed to Locate file: PWO.exe\nThis bot is only for Pokemon World Online!");
                return false;
            }
            if (pWOLocation.Contains("PWO.exe"))
            {
                Settings.Default.PWOLocation = pWOLocation;
                Settings.Default.Save();
            }
        }
        Form1.PROCESS_INFORMATION pROCESSINFORMATION = new Form1.PROCESS_INFORMATION();
        Form1.STARTUPINFO sTARTUPINFO = new Form1.STARTUPINFO();
        Form1.SECURITY_ATTRIBUTES sECURITYATTRIBUTE = new Form1.SECURITY_ATTRIBUTES();
        Form1.SECURITY_ATTRIBUTES sECURITYATTRIBUTE1 = new Form1.SECURITY_ATTRIBUTES();
        Form1.SHELLEXECUTEINFO sHELLEXECUTEINFO = new Form1.SHELLEXECUTEINFO()
        {
            cbSize = Marshal.SizeOf(sHELLEXECUTEINFO),
            lpFile = pWOLocation,
            nShow = 1,
            lpVerb = "runas",
            fMask = 64
        };
        if (!Form1.CreateProcess(pWOLocation, null, ref sECURITYATTRIBUTE, ref sECURITYATTRIBUTE1, false, 32, IntPtr.Zero, null, ref sTARTUPINFO, out pROCESSINFORMATION))
        {
            MessageBox.Show("Failed to Launch file: PWO.exe");
            return false;
        }
        StringBuilder stringBuilder = new StringBuilder("ThunderRT6FormDC");
        IntPtr zero = IntPtr.Zero;
        while (zero == IntPtr.Zero)
        {
            zero = this.FindProcessWindow(stringBuilder, pROCESSINFORMATION.dwProcessId);
        }
        if (!this.EmbedGame(zero, iTab))
        {
            MessageBox.Show("Failed to launch game into bot");
        }
        this.gameWindowHandles[iTab] = zero;
        this.gameProcesshandles[iTab] = pROCESSINFORMATION.dwProcessId;
        this.gameBotHandles[iTab] = IntPtr.Zero;
        this.gameLogHandles[iTab] = IntPtr.Zero;
        Form1.ShowWindow(this.gameWindowHandles[1], 0);
        this.launchAllToolStripMenuItem.Text = "Restart Game";
        return true;
    }

2 个答案:

答案 0 :(得分:3)

初始化程序在初始化之前使用该变量。初始化者常见的鸡和蛋问题。你可以使用另一个重载:

    Form1.SHELLEXECUTEINFO sHELLEXECUTEINFO = new Form1.SHELLEXECUTEINFO()
    {
        cbSize = Marshal.SizeOf(typeof(Form1.SHELLEXECUTEINFO)),
        lpFile = pWOLocation,
        // etc...
    };

或更通用的解决方案:

    Form1.SHELLEXECUTEINFO sHELLEXECUTEINFO = new Form1.SHELLEXECUTEINFO()
    {
        lpFile = pWOLocation,
        // etc...
    };
    sHELLEXECUTEINFO.cbSize = Marshal.SizeOf(sHELLEXECUTEINFO));

请某人检查你的代码btw。

答案 1 :(得分:0)

C#编译器不允许使用未初始化的变量。您似乎在使用之前未初始化sHELLEXECUTEINFO变量。