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.07k stars 86 forks source link

BCryptSetProperty PUCHAR pbInput translated to byte #581

Open Rans4ckeR opened 2 years ago

Rans4ckeR commented 2 years ago

Actual behavior

The extern method works but the other one does not seem usable. I'm expected to pass a pointer using a byte.

From BCryptSetProperty function (bcrypt.h) - Win32 apps | Microsoft Docs:

[in] PUCHAR pbInput

The address of a buffer that contains the new property value. The cbInput parameter contains the size of this buffer.

Generated code:

[SupportedOSPlatform("windows6.0.6000")]
internal static unsafe winmdroot.Foundation.NTSTATUS BCryptSetProperty(void* hObject, string pszProperty, in byte pbInput, uint cbInput, uint dwFlags)
{
    fixed (byte* pbInputLocal = &pbInput)
    {
        fixed (char* pszPropertyLocal = pszProperty)
        {
            winmdroot.Foundation.NTSTATUS __result = PInvoke.BCryptSetProperty(hObject, pszPropertyLocal, pbInputLocal, cbInput, dwFlags);
            return __result;
        }
    }
}

[DllImport("BCrypt", ExactSpelling = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[SupportedOSPlatform("windows6.0.6000")]
internal static extern unsafe winmdroot.Foundation.NTSTATUS BCryptSetProperty(void* hObject, winmdroot.Foundation.PCWSTR pszProperty, byte* pbInput, uint cbInput, uint dwFlags);

Expected behavior

Both generated methods are usable.

Repro steps

  1. NativeMethods.txt content:

    BCryptAddContextFunction
    BCryptEnumContextFunctions
    BCryptEnumContexts
    BCryptFreeBuffer
    BCryptRemoveContextFunction
    BCryptResolveProviders
    NCryptOpenStorageProvider
    NCryptEnumAlgorithms
    NCRYPT_FLAGS
    NCRYPT_KEY_HANDLE
    IGroupPolicyObject
    CoInitializeEx
    CoCreateInstance
    CoUninitialize
    RegCreateKeyEx
    RegDeleteValue
    RegSetKeyValue
    CryptEnumOIDInfo
    BCRYPT_ECCKEY_BLOB
    PCSTR
    CRYPTOAPI_BLOB
    NTE_NO_MORE_ITEMS
    BCRYPT_ECC_CURVE_*
    szOID_ECC_CURVE_*
    BCRYPT_ECDSA_ALGORITHM
    BCRYPT_ECDH_ALGORITHM
    CLSID_GroupPolicyObject
    NCryptProviderName
    BCryptGetProperty
    BCryptOpenAlgorithmProvider
    BCryptCloseAlgorithmProvider
    BCRYPT_ECC_CURVE_NAMES
    CryptFindOIDInfo
    BCryptGenerateKeyPair
    BCryptSetProperty
    BCryptDestroyKey
    BCRYPT_KEY_LENGTHS_STRUCT
  2. NativeMethods.json content (if present):

  3. Any of your own code that should be shared?

Context

AArnott commented 2 years ago

This parameter is in the metadata as

[In][MemorySize(BytesParamIndex = 3)] byte* pbInput

I'm surprised that it doesn't include the [NativeArrayInfo] attribute, which also can carry the info that this MemorySize attribute being used here is. @sotteson1 Can you comment on the latest metadata versions and how CsWin32 should interpret these two attributes and where we would expect one vs. the other?