2016-09-16 37 views
-2

我有一個注入DLL函數,第二個參數是char*。所以,我試圖將我的dll完整路徑(包括你的名字+擴展名),這是一個CStringchar*,但正在產生奇怪的字符。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; 
} 
+0

是否注入(pi.dwProcessId,CT2A(MyDllPath));'工作?其中一些宏會生成匿名臨時對象,這意味着您不能在語句外使用指針。 – Bathsheba

+0

是否有你想使用char *而不是wchar *的理由? –

+0

@RJProgrammer,是的,因爲在注入方法中,在'strlen(dllName)'中,'strlen'需要一個'const char *'作爲參數。 PS:'VirtualAllocEx'。 – Saulo

回答

-2

解決方法:

CT2CA pszCharacterString = (MyDllPath); // Converted with sucess! :D 

Inject(pi.dwProcessId, pszCharacterString); 
+0

請給我票。 – Saulo

+0

完整的虛假。嚴重的是,唯一合法的投票結果就是下降。不要屠殺你的意見。修復'Inject'接口和實現。避免學習Unicode不會導致高質量的代碼(或質量答案)。這當然不是。 – IInspectable

+0

@IInspectable,但它的作品!這更重要。 – Saulo