Skip to content

Commit 44813ec

Browse files
committed
Changed string type to unicode.
1 parent c5eb5bd commit 44813ec

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

Memory/NativeHelper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public T GetDelegate<T>()
5555

5656
private delegate void CloseRemoteProcessDelegate(IntPtr process);
5757

58-
private delegate bool ReadRemoteMemoryDelegate(IntPtr process, IntPtr address, IntPtr buffer, uint size);
58+
private delegate bool ReadRemoteMemoryDelegate(IntPtr process, IntPtr address, IntPtr buffer, int size);
5959

60-
private delegate bool WriteRemoteMemoryDelegate(IntPtr process, IntPtr address, IntPtr buffer, uint size);
60+
private delegate bool WriteRemoteMemoryDelegate(IntPtr process, IntPtr address, IntPtr buffer, int size);
6161

62-
public delegate void EnumerateProcessCallback(uint pid, [MarshalAs(UnmanagedType.LPWStr)]string modulePath);
62+
public delegate void EnumerateProcessCallback(int pid, [MarshalAs(UnmanagedType.LPWStr)]string modulePath);
6363
private delegate void EnumerateProcessesDelegate(EnumerateProcessCallback callbackProcess);
6464

65-
public delegate void EnumerateRemoteSectionCallback(IntPtr baseAddress, IntPtr regionSize, [MarshalAs(UnmanagedType.LPStr)]string name, NativeMethods.StateEnum state, NativeMethods.AllocationProtectEnum protection, NativeMethods.TypeEnum type, [MarshalAs(UnmanagedType.LPWStr)]string modulePath);
65+
public delegate void EnumerateRemoteSectionCallback(IntPtr baseAddress, IntPtr regionSize, [MarshalAs(UnmanagedType.LPWStr)]string name, NativeMethods.StateEnum state, NativeMethods.AllocationProtectEnum protection, NativeMethods.TypeEnum type, [MarshalAs(UnmanagedType.LPWStr)]string modulePath);
6666
public delegate void EnumerateRemoteModuleCallback(IntPtr baseAddress, IntPtr regionSize, [MarshalAs(UnmanagedType.LPWStr)]string modulePath);
6767
private delegate void EnumerateRemoteSectionsAndModulesDelegate(IntPtr process, EnumerateRemoteSectionCallback callbackSection, EnumerateRemoteModuleCallback callbackModule);
6868

@@ -310,7 +310,7 @@ public void CloseRemoteProcess(IntPtr process)
310310
closeRemoteProcessDelegate(process);
311311
}
312312

313-
public bool ReadRemoteMemory(IntPtr process, IntPtr address, byte[] buffer, uint size)
313+
public bool ReadRemoteMemory(IntPtr process, IntPtr address, byte[] buffer, int size)
314314
{
315315
Contract.Requires(buffer != null);
316316

@@ -321,7 +321,7 @@ public bool ReadRemoteMemory(IntPtr process, IntPtr address, byte[] buffer, uint
321321
return result;
322322
}
323323

324-
public bool WriteRemoteMemory(IntPtr process, IntPtr address, byte[] buffer, uint size)
324+
public bool WriteRemoteMemory(IntPtr process, IntPtr address, byte[] buffer, int size)
325325
{
326326
Contract.Requires(buffer != null);
327327

Memory/RemoteProcess.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void ReadRemoteMemoryIntoBuffer(IntPtr address, ref byte[] data)
9292
return;
9393
}
9494

95-
nativeHelper.ReadRemoteMemory(Process.Handle, address, data, (uint)data.Length);
95+
nativeHelper.ReadRemoteMemory(Process.Handle, address, data, data.Length);
9696
}
9797

9898
/// <summary>Reads <paramref name="size"/> bytes from the address in the remote process.</summary>
@@ -352,7 +352,7 @@ public bool WriteRemoteMemory(IntPtr address, byte[] data)
352352
return false;
353353
}
354354

355-
return nativeHelper.WriteRemoteMemory(Process.Handle, address, data, (uint)data.Length);
355+
return nativeHelper.WriteRemoteMemory(Process.Handle, address, data, data.Length);
356356
}
357357

358358
/// <summary>Writes the given <paramref name="value"/> to the <paramref name="address"/> in the remote process.</summary>

NativeHelper/dllmain.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ EXTERN_DLL_EXPORT VOID __stdcall EnumerateProcesses(EnumerateProcessCallback cal
191191
lastError = GetLastError();
192192
}
193193

194-
typedef VOID(__stdcall EnumerateRemoteSectionsCallback)(LPVOID baseAddress, SIZE_T regionSize, BYTE name[IMAGE_SIZEOF_SHORT_NAME + 2], DWORD state, DWORD protection, DWORD type, WCHAR modulePath[PATH_MAXIMUM_LENGTH]);
194+
typedef VOID(__stdcall EnumerateRemoteSectionsCallback)(LPVOID baseAddress, SIZE_T regionSize, WCHAR name[IMAGE_SIZEOF_SHORT_NAME + 1], DWORD state, DWORD protection, DWORD type, WCHAR modulePath[PATH_MAXIMUM_LENGTH]);
195195
typedef VOID(__stdcall EnumerateRemoteModulesCallback)(LPVOID baseAddress, SIZE_T regionSize, WCHAR modulePath[PATH_MAXIMUM_LENGTH]);
196196

197197
EXTERN_DLL_EXPORT VOID __stdcall EnumerateRemoteSectionsAndModules(HANDLE process, EnumerateRemoteSectionsCallback callbackSection, EnumerateRemoteModulesCallback callbackModule)
@@ -205,7 +205,7 @@ EXTERN_DLL_EXPORT VOID __stdcall EnumerateRemoteSectionsAndModules(HANDLE proces
205205
{
206206
LPVOID BaseAddress;
207207
SIZE_T RegionSize;
208-
BYTE Name[IMAGE_SIZEOF_SHORT_NAME + 2];
208+
WCHAR Name[IMAGE_SIZEOF_SHORT_NAME + 1];
209209
DWORD State;
210210
DWORD Protection;
211211
DWORD Type;
@@ -274,14 +274,19 @@ EXTERN_DLL_EXPORT VOID __stdcall EnumerateRemoteSectionsAndModules(HANDLE proces
274274
readRemoteMemory(process, me32.modBaseAddr + DosHdr.e_lfanew + sizeof(IMAGE_NT_HEADERS), sectionHeaders.data(), NtHdr.FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER));
275275
for (int i = 0; i < NtHdr.FileHeader.NumberOfSections; ++i)
276276
{
277-
auto&& section = sectionHeaders[i];
277+
auto&& sectionHeader = sectionHeaders[i];
278278

279-
auto sectionAddress = (size_t)me32.modBaseAddr + section.VirtualAddress;
279+
auto sectionAddress = (size_t)me32.modBaseAddr + sectionHeader.VirtualAddress;
280280
for (auto j = it; j != std::end(sections); ++j)
281281
{
282282
if (sectionAddress >= (size_t)j->BaseAddress && sectionAddress < (size_t)j->BaseAddress + (size_t)j->RegionSize)
283283
{
284-
std::memcpy(j->Name, section.Name, IMAGE_SIZEOF_SHORT_NAME);
284+
// Copy the name because it is not null padded.
285+
char buffer[IMAGE_SIZEOF_SHORT_NAME + 1] = { 0 };
286+
std::memcpy(buffer, sectionHeader.Name, IMAGE_SIZEOF_SHORT_NAME);
287+
288+
size_t convertedChars = 0;
289+
mbstowcs_s(&convertedChars, j->Name, IMAGE_SIZEOF_SHORT_NAME, buffer, _TRUNCATE);
285290
std::memcpy(j->ModulePath, me32.szExePath, sizeof(SectionInfo::ModulePath));
286291
break;
287292
}

0 commit comments

Comments
 (0)