示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (_injected)
            {
                _injector.Unload(_hModule, _process.Id);

                if (_injector.GetLastError() != null)
                {
                    MessageBox.Show(_injector.GetLastError().ToString());
                }

                comboBox1.Enabled      = true;
                textBox1.Enabled       = true;
                textBox2.Enabled       = true;
                button2.Enabled        = true;
                checkBox1.Enabled      = true;
                numericUpDown1.Enabled = true;
                _injected    = false;
                button1.Text = @"Inject";
                return;
            }

            if (checkBox1.Checked)
            {
                while (Process.GetProcessesByName(textBox2.Text).Length == 0)
                {
                    Thread.Sleep(500);
                }

                _process = Process.GetProcessesByName(textBox2.Text)[0];
            }
            else
            {
                _process = Process.GetProcessesByName(textBox2.Text)[0];

                if (_process.Id == 0 && !checkBox1.Checked)
                {
                    MessageBox.Show(@"Process not found.");
                    return;
                }
            }

            Thread.Sleep((int)numericUpDown1.Value);

            _injector = InjectionMethod.Create((InjectionMethodType)_injectionStyle);
            _hModule  = _injector.Inject(textBox1.Text, _process.Id);

            //if no errors, return
            if (_injector.GetLastError() == null)
            {
                comboBox1.Enabled      = false;
                textBox1.Enabled       = false;
                textBox2.Enabled       = false;
                button2.Enabled        = false;
                checkBox1.Enabled      = false;
                numericUpDown1.Enabled = false;
                _injected    = true;
                button1.Text = @"Eject";
                return;
            }

            MessageBox.Show(_injector.GetLastError().ToString());
        }