Ich habe eine Funktion für inject dll und der zweite Parameter ist ein char*
. Also, ich versuche zu konvertieren meine DLL vollständigen Pfad (einschließlich Ihres Namens + Erweiterung), die eine CString
char*
ist, aber seltsame Zeichen erzeugt.CString zu Char * Konvertierung seltsame Zeichen generieren
Jemand kann mir bitte helfen?
Hier ist:
bool Inject(DWORD pId, char *dllName); // Signature of my dll inject method
CString szMyDllName = L"myDll.dll";
CString MyDllPath = L"";
void CSpecialLauncherDlg::OnBnClickedStartDebugging() {
wchar_t path[MAX_PATH] = {0};
GetModuleFileName(NULL, path, _MAX_PATH);
CString szPath = path;
int pos = szPath.ReverseFind('\\');
if (pos < 0)
pos = 1;
else
pos += 1;
MyDllPath = szPath.Left(pos);
MyDllPath.AppendFormat(szMyDllName);
}
void CSpecialLauncherDlg::DebuggerThreadProc() {
// Here is my conversion from CString to char*
char* pStr = CT2A(MyDllPath);
LPCTSTR pszCharacterString = CA2W(pStr);
AfxMessageBox(pszCharacterString);// Generating strange characters here
Inject(pi.dwProcessId, pStr); // Then, dll injection fails :-(
}
EDIT:
Dll Methode einzuspritzen unterhalb
bool Inject(DWORD pId, char *dllName)
{
EnableDebugPrivilege();
HANDLE h = OpenProcess(PROCESS_ALL_ACCESS, false, pId);
if(h)
{
LPVOID LoadLibAddr = (LPVOID)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
LPVOID dereercomp = VirtualAllocEx(h, NULL, strlen(dllName), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
WriteProcessMemory(h, dereercomp, dllName, strlen(dllName), NULL);
HANDLE asdc = CreateRemoteThread(h, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibAddr, dereercomp, 0, NULL);
WaitForSingleObject(asdc, INFINITE);
VirtualFreeEx(h, dereercomp, strlen(dllName), MEM_RELEASE);
CloseHandle(asdc);
CloseHandle(h);
return true;
}
return false;
}
Funktioniert 'Inject (pi.dwProcessId, CT2A (MyDllPath));'? Einige dieser Makros generieren anonyme Provisorien, dh Sie können den Zeiger nicht außerhalb der Anweisung verwenden. – Bathsheba
Gibt es einen Grund, dass Sie char * im Gegensatz zu wchar * verwenden möchten? –
@RJProgrammer, ja, weil bei der inject-Methode in 'strlen (dllName)', 'strlen' ein' const char * 'als Parameter benötigt wird. PS: 'VirtualAllocEx'. – Saulo