我有一個注入DLL函數,第二個參數是char*
。所以,我試圖將我的dll完整路徑(包括你的名字+擴展名),這是一個CString
到char*
,但正在產生奇怪的字符。CString char *轉換生成奇怪的字符
有人可以幫助我嗎?
這裏是:
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 :-(
}
編輯:
DLL注入下面
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;
}
是否注入(pi.dwProcessId,CT2A(MyDllPath));'工作?其中一些宏會生成匿名臨時對象,這意味着您不能在語句外使用指針。 – Bathsheba
是否有你想使用char *而不是wchar *的理由? –
@RJProgrammer,是的,因爲在注入方法中,在'strlen(dllName)'中,'strlen'需要一個'const char *'作爲參數。 PS:'VirtualAllocEx'。 – Saulo