microsoft / microsoft/CsWin32

Generate MAKEINTRESOURCE macro(s) to improve API usability

Open
#486 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C#
Stars
2.5k
Forks
124
Avg merge
1d 3h
Merged PRs (30d)
9

Description

Is your feature request related to a problem? Please describe.
While using the UpdateResource API, I had to define a custom overload to make use of the alternate parameter form of lpName which takes its value from the MAKEINTRESOURCE macro

Describe the solution you'd like
Additional overload generation for functions with overloaded parameter types/meanings.

Describe alternatives you've considered
Alternatively, the MAKEINTRESOURCE macro could be exposed as a function (#466), but doing so exposes a public API for turning ints into LPCSTR which seems somewhat dangerous

Custom UpdateResource overload

Code (click to expand)
#pragma warning disable CS1591,CS1573,CS0465,CS0649,CS8019,CS1570,CS1584,CS1658,CS0436
namespace Windows.Win32
{
  using global::System;
  using global::System.Diagnostics;
  using global::System.Runtime.CompilerServices;
  using global::System.Runtime.InteropServices;
  using winmdroot = global::Windows.Win32;

  internal static partial class PInvoke
  {
    // Copied (and slightly modified) from the autogenerated pinvoke stuff
    // Needed an overload to handle the case where lpName comes from MAKEINTRESOURCE
    /// <inheritdoc cref="UpdateResource(winmdroot.Foundation.HANDLE, winmdroot.Foundation.PCWSTR, winmdroot.Foundation.PCWSTR, ushort, void*, uint)"/>
    internal static unsafe winmdroot.Foundation.BOOL UpdateResource(SafeHandle hUpdate, string lpType, short lpName, ushort wLanguage, void* lpData, uint cb)
    {
      bool hUpdateAddRef = false;
      try
      {
        // fixed (char* lpNameLocal = lpName)
        // {

        // the lpName transformation is patterned after the MAKEINTRESOURCE macro
        // #define MAKEINTRESOURCEW(i) ((LPWSTR)((ULONG_PTR)((WORD)(i))))
        // I manually evaluated all of the defines in the above expression to show all the base types
        // #define MAKEINTRESOURCEW(i) ((*wchar_t)((unsigned long long)((unsigned short)(i))))
        char* lpNameLocal = (char*)(ulong)lpName;
        fixed (char* lpTypeLocal = lpType)
        {
          winmdroot.Foundation.HANDLE hUpdateLocal;
          if (hUpdate is object)
          {
            hUpdate.DangerousAddRef(ref hUpdateAddRef);
            hUpdateLocal = (winmdroot.Foundation.HANDLE)hUpdate.DangerousGetHandle();
          }
          else
            hUpdateLocal = default(winmdroot.Foundation.HANDLE);
          winmdroot.Foundation.BOOL __result = PInvoke.UpdateResource(hUpdateLocal, lpTypeLocal, lpNameLocal, wLanguage, lpData, cb);
          return __result;
        }
        // }
      }
      finally
      {
        if (hUpdateAddRef)
          hUpdate.DangerousRelease();
      }
    }
  }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated UpdateResource signatures and the custom overload shown in the issue. Trace how CsWin32 handles parameters with alternate meanings, then define done as generating the needed overloads for MAKEINTRESOURCE-style parameters without requiring a custom P/Invoke implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.