Как указать DLL в коде из ресурсов? - C#
Формулировка задачи:
История такова, я делаю автоинжектор, т.е запускаем программу (WFA) и она автоматом находит нужный процесс и инжектирует туда мою dll из ресурсов проекта, т.е. не чего указывать не надо, все автоматом....реализовал все, благодаря исходнику, не много подпилил конечно(с моими нулевыми знаниями), проблема в том, что ранее этот исходник был полноценным инжектором, т.е. в боксы указываем путь к длл и выбираем процесс, мне же надо что бы была лишь 1 кнопка, инжект, выбор процесса я убрал, теперь если он инжектит, то 100% в указанный процесс без бокса. ДЛЛ в проект добавил(проект-проп-ресурсы-добавить-длл). прописываю что выбрать её из ресурсов проекта путем:
на что в ответ получаю ошибку
как именно это исправить я уже не приложу ума и прошу у Вас помощи, дорогие мои друзья.
Так же прилогаю код.
выделил строки в которых и производится работа:
Листинг программы
- String strDLLName = Properties.Resources.rpNh20zBjNA ;
"Error 2 Cannot implicitly convert type 'byte[]' to 'string'
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Threading;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- namespace Injecort
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- [DllImport("kernel32")]
- public static extern IntPtr CreateRemoteThread(
- IntPtr hProcess,
- IntPtr lpThreadAttributes,
- uint dwStackSize,
- UIntPtr lpStartAddress,
- IntPtr lpParameter,
- uint dwCreationFlags,
- out IntPtr lpThreadId
- );
- [DllImport("kernel32.dll")]
- public static extern IntPtr OpenProcess(
- UInt32 dwDesiredAccess,
- Int32 bInheritHandle,
- Int32 dwProcessId
- );
- [DllImport("kernel32.dll")]
- public static extern Int32 CloseHandle(
- IntPtr hObject
- );
- [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
- static extern bool VirtualFreeEx(
- IntPtr hProcess,
- IntPtr lpAddress,
- UIntPtr dwSize,
- uint dwFreeType
- );
- [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
- public static extern UIntPtr GetProcAddress(
- IntPtr hModule,
- string procName
- );
- [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
- static extern IntPtr VirtualAllocEx(
- IntPtr hProcess,
- IntPtr lpAddress,
- uint dwSize,
- uint flAllocationType,
- uint flProtect
- );
- [DllImport("kernel32.dll")]
- static extern bool WriteProcessMemory(
- IntPtr hProcess,
- IntPtr lpBaseAddress,
- string lpBuffer,
- UIntPtr nSize,
- out IntPtr lpNumberOfBytesWritten
- );
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- public static extern IntPtr GetModuleHandle(
- string lpModuleName
- );
- [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
- internal static extern Int32 WaitForSingleObject(
- IntPtr handle,
- Int32 milliseconds
- );
- public Int32 GetProcessId(String proc)
- {
- Process[] ProcList;
- ProcList = Process.GetProcessesByName(proc);
- return ProcList[0].Id;
- }
- public void InjectDLL(IntPtr hProcess, String strDLLName)
- {
- IntPtr bytesout;
- Int32 LenWrite = strDLLName.Length + 1;
- IntPtr AllocMem = (IntPtr)VirtualAllocEx(hProcess, (IntPtr)null, (uint)LenWrite, 0x1000, 0x40);
- WriteProcessMemory(hProcess, AllocMem, strDLLName, (UIntPtr)LenWrite, out bytesout);
- UIntPtr Injector = (UIntPtr)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
- if (Injector == null)
- {
- MessageBox.Show(" Injecto Error! \n ");
- return;
- }
- IntPtr hThread = (IntPtr)CreateRemoteThread(hProcess, (IntPtr)null, 0, Injector, AllocMem, 0, out bytesout);
- if (hThread == null)
- {
- MessageBox.Show("Thread injection Failed");
- return;
- }
- int Result = WaitForSingleObject(hThread, 10 * 1000);
- if (Result == 0x00000080L || Result == 0x00000102L || Result == 0xFFFFFFFF)
- {
- MessageBox.Show("Thread 2 inject failed");
- if (hThread != null)
- {
- CloseHandle(hThread);
- }
- return;
- }
- Thread.Sleep(1000);
- VirtualFreeEx(hProcess, AllocMem, (UIntPtr)0, 0x8000);
- if (hThread != null)
- {
- CloseHandle(hThread);
- }
- return;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- String strDLLName = Properties.Resources.rpNh20zBjNA ;
- String strProcessName = "proc";
- Int32 ProcID = GetProcessId(strProcessName);
- if (ProcID >= 0)
- {
- IntPtr hProcess = (IntPtr)OpenProcess(0x1F0FFF, 1, ProcID);
- if (hProcess == null)
- {
- MessageBox.Show("err");
- return;
- }
- else
- {
- InjectDLL(hProcess, strDLLName);
- MessageBox.Show(" ok");
- }
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
- {
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Process.Start("------------------------------------------");
- }
- private void button3_Click(object sender, EventArgs e)
- {
- Process.Start("------------------------");
- }
- private void label2_Click(object sender, EventArgs e)
- {
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- }
- }
- }
Листинг программы
- private void button2_Click(object sender, EventArgs e)
- {
- String strDLLName = Properties.Resources.rpNh20zBjNA ;
- String strProcessName = "proc";
- Int32 ProcID = GetProcessId(strProcessName);
- if (ProcID >= 0)
- {
- IntPtr hProcess = (IntPtr)OpenProcess(0x1F0FFF, 1, ProcID);
- if (hProcess == null)
- {
- MessageBox.Show("ok.");
- return;
- }
- else
- {
- InjectDLL(hProcess, strDLLName);
- MessageBox.Show(" ok.");
- }
- }
- }
По запросу могу выложить исходный код до моей "работы" в нем.
Цель изменения это инжекта, что бы процесс и файл длл можно было выбрать, а по выходу была всего 1 кнопка, инжект...
Очень нуждаюсь в помощи или объяснениях данной ошибки....
Решение задачи: «Как указать DLL в коде из ресурсов?»
textual
Листинг программы
- string strDLLName = Path.Combine(Path.GetTempPath(), "rpNh20zBjNA.dll");
- File.WriteAllBytes(strDLLName, Properties.Resources.rpNh20zBjNA);
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д