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
1.99k stars 84 forks source link

Functions FindResource, LoadImage should have an additional overload #1076

Closed harborsiem closed 8 months ago

harborsiem commented 8 months ago

Is your feature request related to a problem? Please describe.

Functions FindResource, LoadImage (maybe more functions ?) should have an additional overload for the second parameter because the second parameter can be a value from the macro MAKEINTRESOURCE. But one have only a PCWSTR or a String for this parameter. So the user have to cast the int value when he had to use macro MAKEINTRESOURCE. For FindResource it is also necessary to have an overload for the 3. parameter. Example:

HMODULE resourceHandle = ...;
int id = 2;
HRSRC hResource = default;
fixed (char* imageLocal = "IMAGE")
{
      hResource = PInvoke.FindResource(resourceHandle, (PCWSTR)(char*)id, imageLocal);
}

Describe the solution you'd like Additional friendly overloads:

public static unsafe winmdroot.Foundation.HRSRC FindResource(winmdroot.Foundation.HMODULE hModule, ushort lpName, string lpType)
{
    fixed (char* lpTypeLocal = lpType)
    {
        char* lpNameLocal = (char*)lpName;
        {
        winmdroot.Foundation.HRSRC __result = PInvoke.FindResource(hModule, lpNameLocal, lpTypeLocal);
        return __result;
        }
    }
}

public static unsafe winmdroot.Foundation.HRSRC FindResource(winmdroot.Foundation.HMODULE hModule, ushort lpName, ushort lpType)
{
    char* lpTypeLocal = (char*)lpType;
    {
        char* lpNameLocal = (char*)lpName;
        {
        winmdroot.Foundation.HRSRC __result = PInvoke.FindResource(hModule, lpNameLocal, lpTypeLocal);
        return __result;
        }
    }
}

Describe alternatives you've considered

Additional context

AArnott commented 8 months ago

I don't think we can add overloads here, for a few reasons.

What if instead, CsWin32 were willing to generated MAKEINTRESOURCE (on request) so that you can do this:

HMODULE resourceHandle = ...;
HRSRC hResource = PInvoke.FindResource(resourceHandle, MAKEINTRESOURCE(2), "IMAGE");

Would that suffice?

AArnott commented 8 months ago

Duplicate of #486