Pankoza3
Femtopat
- Katılım
- 3 Kasım 2024
- Mesajlar
- 8
Daha fazla
- Cinsiyet
- Erkek
Bu benim C# da dllımport ile çağırdığım G++ X64 Windows 11 24H2 Home Single Language de derlenmiş DLL dosyamın kaynak kodu:
Ancak ben bunu C# programımda kullanınca hata alıyorum. Hata olduğundan bile emin değilim. Kullanılan yer:
Hata:
OpenProcess failed: 0
C++:
#include <windows.h>
#include <iostream>
extern "C" __declspec(dllexport) bool lowinject(DWORD pid, const wchar_t* dllPath);
bool lowinject(DWORD pid, const wchar_t* dllPath) {
HANDLE hProcess = OpenProcess(
PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION |
PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ,
FALSE, pid);
if (hProcess == NULL) {
std::cerr << "OpenProcess failed: " << GetLastError() << "\n";
return false;
}
size_t size = (wcslen(dllPath) + 1) * sizeof(wchar_t);
LPVOID remoteMem = VirtualAllocEx(hProcess, NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (remoteMem == NULL) {
std::cerr << "VirtualAllocEx failed: " << GetLastError() << "\n";
CloseHandle(hProcess);
return false;
}
if (!WriteProcessMemory(hProcess, remoteMem, dllPath, size, NULL)) {
std::cerr << "WriteProcessMemory failed: " << GetLastError() << "\n";
VirtualFreeEx(hProcess, remoteMem, 0, MEM_RELEASE);
CloseHandle(hProcess);
return false;
}
HMODULE hKernel32 = GetModuleHandleW(L"kernel32.dll");
if (!hKernel32) {
std::cerr << "GetModuleHandleW failed: " << GetLastError() << "\n";
VirtualFreeEx(hProcess, remoteMem, 0, MEM_RELEASE);
CloseHandle(hProcess);
return false;
}
FARPROC loadLibraryAddr = GetProcAddress(hKernel32, "LoadLibraryW");
if (!loadLibraryAddr) {
std::cerr << "GetProcAddress failed: " << GetLastError() << "\n";
VirtualFreeEx(hProcess, remoteMem, 0, MEM_RELEASE);
CloseHandle(hProcess);
return false;
}
HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE)loadLibraryAddr, remoteMem, 0, NULL);
if (hThread == NULL) {
std::cerr << "CreateRemoteThread failed: " << GetLastError() << "\n";
VirtualFreeEx(hProcess, remoteMem, 0, MEM_RELEASE);
CloseHandle(hProcess);
return false;
}
WaitForSingleObject(hThread, INFINITE);
DWORD exitCode = 0;
if (!GetExitCodeThread(hThread, &exitCode)) {
std::cerr << "GetExitCodeThread failed: " << GetLastError() << "\n";
} else {
std::cout << "Remote thread exit code: " << exitCode << "\n";
}
VirtualFreeEx(hProcess, remoteMem, 0, MEM_RELEASE);
CloseHandle(hThread);
CloseHandle(hProcess);
return true;
}
C#:
[DllImport("lowinj.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern bool lowinject(uint pid, string dllPath);
private void lowleveldllenjekteet(object sender, RoutedEventArgs e)
{
Window inputWindow = new Window()
{
Title = "İşleme dll enjekte et",
Width = 350,
Height = 200,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
ResizeMode = ResizeMode.NoResize,
Owner = this
};
StackPanel panel = new StackPanel() { Margin = new Thickness(10) };
panel.Children.Add(new Label() { Content = "PID:" });
TextBox pidTextBox = new TextBox() { Height = 25, Margin = new Thickness(0, 0, 0, 10) };
panel.Children.Add(pidTextBox);
panel.Children.Add(new Label() { Content = "DLL Yolu:" });
TextBox dllPathTextBox = new TextBox() { Height = 25, Margin = new Thickness(0, 0, 0, 10) };
panel.Children.Add(dllPathTextBox);
Button okButton = new Button() { Content = "Tamam", Width = 70, HorizontalAlignment = HorizontalAlignment.Right };
panel.Children.Add(okButton);
inputWindow.Content = panel;
okButton.Click += (s, ea) =>
{
try
{
if (!uint.TryParse(pidTextBox.Text, out uint pid))
{
MessageBox.Show("PID geçersiz.");
return;
}
string dllPath = dllPathTextBox.Text;
lowinject(pid, dllPath);
MessageBox.Show($"PID: {pid}\nDLL Yolu: {dllPath}", "", MessageBoxButton.OK, MessageBoxImage.Information);
inputWindow.Close();
}
catch (Exception ex)
{
MessageBox.Show($"{ex.Message}");
}
};
inputWindow.ShowDialog();
}
Hata:
OpenProcess failed: 0
Son düzenleyen: Moderatör: