Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit 60058c3

Browse files
committed
Update interop to use void* instead of byte* for buffers
Refactored all P/Invoke signatures and usages in Lci, Vmmi, LeechCore, and Vmm to use void* for buffer and pointer parameters instead of byte*. Added IntPtr overloads for memory free functions. Updated all related code, delegates, and documentation to match. This improves type safety, reduces unsafe casting, and aligns with modern .NET interop practices.
1 parent 113f079 commit 60058c3

4 files changed

Lines changed: 96 additions & 91 deletions

File tree

src/VmmSharpEx/Internal/Lci.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,25 @@ internal static partial class Lci
2626
public static partial void LcClose(IntPtr hLC);
2727

2828
[LibraryImport("leechcore.dll", EntryPoint = "LcMemFree")]
29-
public static unsafe partial void LcMemFree(IntPtr pv);
29+
public static unsafe partial void LcMemFree(void* pv);
30+
31+
[LibraryImport("leechcore.dll", EntryPoint = "LcMemFree")]
32+
public static partial void LcMemFree(IntPtr pv);
3033

3134
[LibraryImport("leechcore.dll", EntryPoint = "LcAllocScatter1")]
3235
[return: MarshalAs(UnmanagedType.Bool)]
3336
public static unsafe partial bool LcAllocScatter1(uint cMEMs, out IntPtr pppMEMs);
3437

3538
[LibraryImport("leechcore.dll", EntryPoint = "LcRead")]
3639
[return: MarshalAs(UnmanagedType.Bool)]
37-
public static unsafe partial bool LcRead(IntPtr hLC, ulong pa, uint cb, byte* pb);
40+
public static unsafe partial bool LcRead(IntPtr hLC, ulong pa, uint cb, void* pb);
3841

3942
[LibraryImport("leechcore.dll", EntryPoint = "LcReadScatter")]
4043
public static unsafe partial void LcReadScatter(IntPtr hLC, uint cMEMs, IntPtr ppMEMs);
4144

4245
[LibraryImport("leechcore.dll", EntryPoint = "LcWrite")]
4346
[return: MarshalAs(UnmanagedType.Bool)]
44-
public static unsafe partial bool LcWrite(IntPtr hLC, ulong pa, uint cb, byte* pb);
47+
public static unsafe partial bool LcWrite(IntPtr hLC, ulong pa, uint cb, void* pb);
4548

4649
[LibraryImport("leechcore.dll", EntryPoint = "LcGetOption")]
4750
[return: MarshalAs(UnmanagedType.Bool)]
@@ -53,7 +56,7 @@ internal static partial class Lci
5356

5457
[LibraryImport("leechcore.dll", EntryPoint = "LcCommand")]
5558
[return: MarshalAs(UnmanagedType.Bool)]
56-
public static unsafe partial bool LcCommand(IntPtr hLC, LcCmd fOption, uint cbDataIn, byte* pbDataIn, out IntPtr ppbDataOut, out uint pcbDataOut);
59+
public static unsafe partial bool LcCommand(IntPtr hLC, LcCmd fOption, uint cbDataIn, void* pbDataIn, out IntPtr ppbDataOut, out uint pcbDataOut);
5760

5861
[StructLayout(LayoutKind.Sequential)]
5962
public struct LC_CONFIG_ERRORINFO

src/VmmSharpEx/Internal/Vmmi.cs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public unsafe struct VMMDLL_VFS_FILELIST
5757
{
5858
public uint dwVersion;
5959
public uint _Reserved;
60-
public delegate* unmanaged<IntPtr, byte*, ulong, IntPtr, int> pfnAddFile;
61-
public delegate* unmanaged<IntPtr, byte*, IntPtr, int> pfnAddDirectory;
60+
public delegate* unmanaged<IntPtr, void*, ulong, IntPtr, int> pfnAddFile;
61+
public delegate* unmanaged<IntPtr, void*, IntPtr, int> pfnAddDirectory;
6262
public IntPtr h;
6363
}
6464

@@ -941,8 +941,10 @@ public static partial bool VMMDLL_ConfigSet(
941941
ulong qwValue);
942942

943943
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_MemFree")]
944-
public static unsafe partial void VMMDLL_MemFree(
945-
void* pvMem);
944+
public static unsafe partial void VMMDLL_MemFree(void* pvMem);
945+
946+
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_MemFree")]
947+
public static partial void VMMDLL_MemFree(IntPtr pvMem);
946948

947949
// VFS (VIRTUAL FILE SYSTEM) FUNCTIONALITY BELOW:
948950

@@ -957,7 +959,7 @@ public static unsafe partial bool VMMDLL_VfsList(
957959
public static unsafe partial uint VMMDLL_VfsRead(
958960
IntPtr hVMM,
959961
[MarshalAs(UnmanagedType.LPUTF8Str)] string wcsFileName,
960-
byte* pb,
962+
void* pb,
961963
uint cb,
962964
out uint pcbRead,
963965
ulong cbOffset);
@@ -966,7 +968,7 @@ public static unsafe partial uint VMMDLL_VfsRead(
966968
public static unsafe partial uint VMMDLL_VfsWrite(
967969
IntPtr hVMM,
968970
[MarshalAs(UnmanagedType.LPUTF8Str)] string wcsFileName,
969-
byte* pb,
971+
void* pb,
970972
uint cb,
971973
out uint pcbRead,
972974
ulong cbOffset);
@@ -993,7 +995,7 @@ public static unsafe partial bool VMMDLL_MemReadEx(
993995
IntPtr hVMM,
994996
uint dwPID,
995997
ulong qwA,
996-
byte* pb,
998+
void* pb,
997999
uint cb,
9981000
out uint pcbReadOpt,
9991001
VmmFlags flags);
@@ -1004,14 +1006,14 @@ public static unsafe partial bool VMMDLL_MemReadPage(
10041006
IntPtr hVMM,
10051007
uint dwPID,
10061008
ulong qwA,
1007-
byte* pbPage);
1009+
void* pbPage);
10081010

10091011
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_MemPrefetchPages")]
10101012
[return: MarshalAs(UnmanagedType.Bool)]
10111013
public static unsafe partial bool VMMDLL_MemPrefetchPages(
10121014
IntPtr hVMM,
10131015
uint dwPID,
1014-
byte* pPrefetchAddresses,
1016+
void* pPrefetchAddresses,
10151017
uint cPrefetchAddresses);
10161018

10171019
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_MemWrite")]
@@ -1020,7 +1022,7 @@ public static unsafe partial bool VMMDLL_MemWrite(
10201022
IntPtr hVMM,
10211023
uint dwPID,
10221024
ulong qwA,
1023-
byte* pb,
1025+
void* pb,
10241026
uint cb);
10251027

10261028
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_MemVirt2Phys")]
@@ -1086,7 +1088,7 @@ public static unsafe partial bool VMMDLL_Scatter_Clear(
10861088

10871089
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_PidList")]
10881090
[return: MarshalAs(UnmanagedType.Bool)]
1089-
public static unsafe partial bool VMMDLL_PidList(IntPtr hVMM, byte* pPIDs, ref ulong pcPIDs);
1091+
public static unsafe partial bool VMMDLL_PidList(IntPtr hVMM, void* pPIDs, ref ulong pcPIDs);
10901092

10911093
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_PidGetFromName")]
10921094
[return: MarshalAs(UnmanagedType.Bool)]
@@ -1103,7 +1105,7 @@ public static unsafe partial bool VMMDLL_Scatter_Clear(
11031105
public static unsafe partial bool VMMDLL_ProcessGetInformation(
11041106
IntPtr hVMM,
11051107
uint dwPID,
1106-
byte* pProcessInformation,
1108+
void* pProcessInformation,
11071109
ref ulong pcbProcessInformation);
11081110

11091111
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_ProcessGetInformationAll")]
@@ -1115,7 +1117,7 @@ out uint pcProcessInformation
11151117
);
11161118

11171119
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_ProcessGetInformationString")]
1118-
public static unsafe partial byte* VMMDLL_ProcessGetInformationString(
1120+
public static unsafe partial void* VMMDLL_ProcessGetInformationString(
11191121
IntPtr hVMM,
11201122
uint dwPID,
11211123
uint fOptionString);
@@ -1126,15 +1128,15 @@ public static unsafe partial bool VMMDLL_ProcessGetDirectories(
11261128
IntPtr hVMM,
11271129
uint dwPID,
11281130
[MarshalAs(UnmanagedType.LPUTF8Str)] string uszModule,
1129-
byte* pData);
1131+
void* pData);
11301132

11311133
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_ProcessGetSectionsU")]
11321134
[return: MarshalAs(UnmanagedType.Bool)]
11331135
public static unsafe partial bool VMMDLL_ProcessGetSections(
11341136
IntPtr hVMM,
11351137
uint dwPID,
11361138
[MarshalAs(UnmanagedType.LPUTF8Str)] string uszModule,
1137-
byte* pData,
1139+
void* pData,
11381140
uint cData,
11391141
out uint pcData);
11401142

@@ -1146,15 +1148,15 @@ public static unsafe partial bool VMMDLL_PdbLoad(
11461148
IntPtr hVMM,
11471149
uint dwPID,
11481150
ulong vaModuleBase,
1149-
byte* pModuleMapEntry);
1151+
void* pModuleMapEntry);
11501152

11511153
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_PdbSymbolName")]
11521154
[return: MarshalAs(UnmanagedType.Bool)]
11531155
public static unsafe partial bool VMMDLL_PdbSymbolName(
11541156
IntPtr hVMM,
11551157
[MarshalAs(UnmanagedType.LPStr)] string szModule,
11561158
ulong cbSymbolAddressOrOffset,
1157-
byte* szSymbolName,
1159+
void* szSymbolName,
11581160
out uint pdwSymbolDisplacement);
11591161

11601162
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_PdbSymbolAddress")]
@@ -1367,17 +1369,17 @@ public static unsafe partial bool VMMDLL_Map_GetServices(
13671369
[return: MarshalAs(UnmanagedType.Bool)]
13681370
public static unsafe partial bool VMMDLL_Map_GetPfn(
13691371
IntPtr hVMM,
1370-
byte* pPfns,
1372+
void* pPfns,
13711373
uint cPfns,
1372-
byte* pPfnMap,
1374+
void* pPfnMap,
13731375
ref uint pcbPfnMap);
13741376

13751377
// REGISTRY FUNCTIONALITY BELOW:
13761378
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_WinReg_HiveList")]
13771379
[return: MarshalAs(UnmanagedType.Bool)]
13781380
public static unsafe partial bool VMMDLL_WinReg_HiveList(
13791381
IntPtr hVMM,
1380-
byte* pHives,
1382+
void* pHives,
13811383
uint cHives,
13821384
out uint pcHives);
13831385

@@ -1387,7 +1389,7 @@ public static unsafe partial bool VMMDLL_WinReg_HiveReadEx(
13871389
IntPtr hVMM,
13881390
ulong vaCMHive,
13891391
uint ra,
1390-
byte* pb,
1392+
void* pb,
13911393
uint cb,
13921394
out uint pcbReadOpt,
13931395
VmmFlags flags);
@@ -1398,7 +1400,7 @@ public static unsafe partial bool VMMDLL_WinReg_HiveWrite(
13981400
IntPtr hVMM,
13991401
ulong vaCMHive,
14001402
uint ra,
1401-
byte* pb,
1403+
void* pb,
14021404
uint cb);
14031405

14041406
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_WinReg_EnumKeyExU")]
@@ -1407,7 +1409,7 @@ public static unsafe partial bool VMMDLL_WinReg_EnumKeyEx(
14071409
IntPtr hVMM,
14081410
[MarshalAs(UnmanagedType.LPUTF8Str)] string uszFullPathKey,
14091411
uint dwIndex,
1410-
byte* lpName,
1412+
void* lpName,
14111413
ref uint lpcchName,
14121414
out ulong lpftLastWriteTime);
14131415

@@ -1417,10 +1419,10 @@ public static unsafe partial bool VMMDLL_WinReg_EnumValue(
14171419
IntPtr hVMM,
14181420
[MarshalAs(UnmanagedType.LPUTF8Str)] string uszFullPathKey,
14191421
uint dwIndex,
1420-
byte* lpValueName,
1422+
void* lpValueName,
14211423
ref uint lpcchValueName,
14221424
out uint lpType,
1423-
byte* lpData,
1425+
void* lpData,
14241426
ref uint lpcbData);
14251427

14261428
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_WinReg_QueryValueExU")]
@@ -1429,7 +1431,7 @@ public static unsafe partial bool VMMDLL_WinReg_QueryValueEx(
14291431
IntPtr hVMM,
14301432
[MarshalAs(UnmanagedType.LPUTF8Str)] string uszFullPathKeyValue,
14311433
out uint lpType,
1432-
byte* lpData,
1434+
void* lpData,
14331435
ref uint lpcbData);
14341436

14351437
// MEMORY SEARCH FUNCTIONALITY BELOW:
@@ -1446,10 +1448,10 @@ public static unsafe partial bool VMMDLL_MemSearch(
14461448
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_UtilFillHexAscii")]
14471449
[return: MarshalAs(UnmanagedType.Bool)]
14481450
public static unsafe partial bool VMMDLL_UtilFillHexAscii(
1449-
byte* pb,
1451+
void* pb,
14501452
uint cb,
14511453
uint cbInitialOffset,
1452-
byte* sz,
1454+
void* sz,
14531455
ref uint pcsz);
14541456

14551457
[LibraryImport("vmm.dll", EntryPoint = "VMMDLL_Log")]

src/VmmSharpEx/LeechCore.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private void Dispose(bool disposing)
188188
public unsafe byte[]? Read(ulong pa, uint cb)
189189
{
190190
var arr = new byte[cb];
191-
fixed (byte* pb = arr)
191+
fixed (void* pb = arr)
192192
{
193193
if (!Lci.LcRead(_handle, pa, cb, pb))
194194
{
@@ -205,15 +205,15 @@ private void Dispose(bool disposing)
205205
/// <param name="pa">Physical address to read.</param>
206206
/// <param name="result">Receives the value read from memory on success.</param>
207207
/// <returns><see langword="true"/> if successful; otherwise <see langword="false"/>.</returns>
208-
/// <seealso cref="Lci.LcRead(IntPtr, ulong, uint, byte*)"/>
208+
/// <seealso cref="Lci.LcRead(IntPtr, ulong, uint, void*)"/>
209209
public unsafe bool ReadValue<T>(ulong pa, out T result)
210210
where T : unmanaged, allows ref struct
211211
{
212212
uint cb = (uint)sizeof(T);
213213
result = default;
214214
fixed (void* pb = &result)
215215
{
216-
return Lci.LcRead(_handle, pa, cb, (byte*)pb);
216+
return Lci.LcRead(_handle, pa, cb, pb);
217217
}
218218
}
219219

@@ -236,7 +236,7 @@ public unsafe bool ReadValue<T>(ulong pa, out T result)
236236
uint cb = checked((uint)sizeof(T) * (uint)count);
237237
fixed (T* pb = arr)
238238
{
239-
if (!Lci.LcRead(_handle, pa, cb, (byte*)pb))
239+
if (!Lci.LcRead(_handle, pa, cb, pb))
240240
{
241241
return null;
242242
}
@@ -260,7 +260,7 @@ public unsafe bool ReadValue<T>(ulong pa, out T result)
260260
uint cb = checked((uint)sizeof(T) * (uint)count);
261261
fixed (T* pb = arr.Span)
262262
{
263-
if (!Lci.LcRead(_handle, pa, cb, (byte*)pb))
263+
if (!Lci.LcRead(_handle, pa, cb, pb))
264264
{
265265
arr.Dispose();
266266
return null;
@@ -282,7 +282,7 @@ public unsafe bool ReadSpan<T>(ulong pa, Span<T> span)
282282
uint cb = checked((uint)sizeof(T) * (uint)span.Length);
283283
fixed (T* pb = span)
284284
{
285-
return Lci.LcRead(_handle, pa, cb, (byte*)pb);
285+
return Lci.LcRead(_handle, pa, cb, pb);
286286
}
287287
}
288288

@@ -300,7 +300,7 @@ public unsafe bool WriteSpan<T>(ulong pa, Span<T> span)
300300
uint cb = checked((uint)sizeof(T) * (uint)span.Length);
301301
fixed (T* pb = span)
302302
{
303-
return Lci.LcWrite(_handle, pa, cb, (byte*)pb);
303+
return Lci.LcWrite(_handle, pa, cb, pb);
304304
}
305305
}
306306

@@ -326,7 +326,7 @@ public unsafe bool Read(ulong pa, IntPtr pb, uint cb)
326326
/// <returns><see langword="true"/> on success; otherwise <see langword="false"/>.</returns>
327327
public unsafe bool Read(ulong pa, void* pb, uint cb)
328328
{
329-
if (!Lci.LcRead(_handle, pa, cb, (byte*)pb))
329+
if (!Lci.LcRead(_handle, pa, cb, pb))
330330
{
331331
return false;
332332
}
@@ -390,7 +390,7 @@ public unsafe bool WriteValue<T>(ulong pa, T value)
390390
{
391391
_parent?.ThrowIfMemWritesDisabled();
392392
uint cb = (uint)sizeof(T);
393-
return Lci.LcWrite(_handle, pa, cb, (byte*)&value);
393+
return Lci.LcWrite(_handle, pa, cb, &value);
394394
}
395395

396396
/// <summary>
@@ -407,7 +407,7 @@ public unsafe bool WriteArray<T>(ulong pa, T[] data)
407407
uint cb = checked((uint)sizeof(T) * (uint)data.Length);
408408
fixed (T* pb = data)
409409
{
410-
return Lci.LcWrite(_handle, pa, cb, (byte*)pb);
410+
return Lci.LcWrite(_handle, pa, cb, pb);
411411
}
412412
}
413413

@@ -434,7 +434,7 @@ public unsafe bool Write(ulong pa, IntPtr pb, uint cb)
434434
public unsafe bool Write(ulong pa, void* pb, uint cb)
435435
{
436436
_parent?.ThrowIfMemWritesDisabled();
437-
return Lci.LcWrite(_handle, pa, cb, (byte*)pb);
437+
return Lci.LcWrite(_handle, pa, cb, pb);
438438
}
439439

440440
/// <summary>
@@ -488,7 +488,7 @@ public unsafe bool ExecuteCommand(LcCmd fOption, ReadOnlySpan<byte> dataIn, out
488488
}
489489
else
490490
{
491-
fixed (byte* pbDataIn = dataIn)
491+
fixed (void* pbDataIn = dataIn)
492492
{
493493
if (!Lci.LcCommand(_handle, fOption, (uint)dataIn.Length, pbDataIn, out pbDataOut, out cbDataOut))
494494
{
@@ -598,7 +598,7 @@ public struct MEM_SCATTER_NATIVE
598598
/// A read-only view over the page contents pointed at by <see cref="pb"/>.
599599
/// </summary>
600600
/// <remarks>
601-
/// DANGER: Do not access this memory after the memory is freed via <see cref="Lci.LcMemFree"/>.
601+
/// DANGER: Do not access this memory after the memory is freed via <see cref="Lci.LcMemFree(nint)"/>.
602602
/// </remarks>
603603
public readonly unsafe ReadOnlySpan<byte> Data => new(
604604
pointer: pb.ToPointer(),

0 commit comments

Comments
 (0)