microsoft / CsWin32

A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.
MIT License
2.12k stars 92 forks source link

EnumProcessModules can not return modules collection. #1281

Closed NeverMorewd closed 1 month ago

NeverMorewd commented 2 months ago

Actual behavior

EnumProcessModules receive FreeLibrarySafeHandle as module handles. However FreeLibrarySafeHandle contains only one module handle.

Expected behavior

Get all module handles.

Repro steps

  1. NativeMethods.txt content:

    EnumProcessModules 
  2. NativeMethods.json content (if present):

  3. Any of your own code that should be shared? Current function content generated by cswin32 SG.

    public static unsafe winmdroot.Foundation.BOOL EnumProcessModules(SafeHandle hProcess, out FreeLibrarySafeHandle lphModule, uint cb, out uint lpcbNeeded)
    {
    bool hProcessAddRef = false;
    try
    {
        fixed (uint* lpcbNeededLocal = &lpcbNeeded)
        {
            winmdroot.Foundation.HANDLE hProcessLocal;
            if (hProcess is object)
            {
                hProcess.DangerousAddRef(ref hProcessAddRef);
                hProcessLocal = (winmdroot.Foundation.HANDLE)hProcess.DangerousGetHandle();
            }
            else
                throw new ArgumentNullException(nameof(hProcess));
            winmdroot.Foundation.HMODULE lphModuleLocal;
            winmdroot.Foundation.BOOL __result = PInvoke.EnumProcessModules(hProcessLocal, &lphModuleLocal, cb, lpcbNeededLocal);
            lphModule = new FreeLibrarySafeHandle(lphModuleLocal, ownsHandle: true);
            return __result;
        }
    }
    finally
    {
        if (hProcessAddRef)
            hProcess.DangerousRelease();
    }
    }
    • CsWin32 version: [0.3.106]
    • Win32Metadata version (if explicitly set by project):
    • Target Framework: [net6.0;net8.0;netframework4.8]
    • LangVersion (if explicitly set by project): [latest]
AArnott commented 1 month ago

Same root cause as #1266.