Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct getting created with ANSI instead of Unicode string fields, leading to wrong Marshal.SizeOf result #389

Closed
palenshus opened this issue Sep 9, 2021 · 2 comments · Fixed by #410
Assignees
Labels
bug Something isn't working

Comments

@palenshus
Copy link

Actual behavior

When I gen code for the WER_REPORT_INFORMATION struct, the size returned by Marshal.SizeOf() is incorrect. I believe what's happening is it's treating the internal strings as 1-byte per char instead of 2-bytes per char, leading to a size ~50% what it should be.

Here's what's generated:

internal partial struct WER_REPORT_INFORMATION
{
	/// <summary>The size of this structure, in bytes.</summary>
	internal uint dwSize;
	/// <summary>A handle to the process for which the report is being generated. If this member is <b>NULL</b>, this is the calling process.</summary>
	internal win32.Foundation.HANDLE hProcess;
	/// <summary>The name used to look up consent settings. If this member is empty, the default is the name specified by the <i>pwzEventType</i> parameter of <a href="https://docs.microsoft.com/windows/desktop/api/werapi/nf-werapi-werreportcreate">WerReportCreate</a>.</summary>
	internal __char_64 wzConsentKey;
	/// <summary>The display name. If this member is empty, the default is the name specified by <i>pwzEventType</i> parameter of <a href="https://docs.microsoft.com/windows/desktop/api/werapi/nf-werapi-werreportcreate">WerReportCreate</a>.</summary>
	internal __char_128 wzFriendlyEventName;
	/// <summary>The name of the application. If this parameter is empty, the default is the base name of the image file.</summary>
	internal __char_128 wzApplicationName;
	/// <summary>The full path to the application.</summary>
	internal __char_260 wzApplicationPath;
	/// <summary>A description of the problem. This description is displayed in <b>Problem Reports and Solutions</b> on Windows Vista or the problem reports pane of the <b>Action Center</b> on Windows 7.</summary>
	internal __char_512 wzDescription;
	/// <summary>A handle to the parent window.</summary>
	internal win32.Foundation.HWND hwndParent;

    ...
}

And here's the native struct:

typedef struct _WER_REPORT_INFORMATION {
  DWORD  dwSize;
  HANDLE hProcess;
  WCHAR  wzConsentKey[64];
  WCHAR  wzFriendlyEventName[128];
  WCHAR  wzApplicationName[128];
  WCHAR  wzApplicationPath[MAX_PATH];
  WCHAR  wzDescription[512];
  HWND   hwndParent;
} WER_REPORT_INFORMATION, *PWER_REPORT_INFORMATION;

When I call Marshal.SizeOf(reportInformation), it returns 1120.

When I try to call WerReportCreate with this struct, it returns E_INVALIDARG.

Expected behavior

The SizeOf call should return 2204 (or 2208 due to 8-byte packing).

Repro steps

  1. NativeMethods.txt content:
WerReportCreate
  1. NativeMethods.json content (if present):
  1. Any of your own code that should be shared?
WER_REPORT_INFORMATION reportInformation = default;
reportInformation.dwSize = (uint)Marshal.SizeOf(reportInformation);
reportInformation.hProcess = (HANDLE)IntPtr.Zero;
reportInformation.hwndParent = (HWND)IntPtr.Zero;
"A functional problem was detected and logs were collected for further investigation.".AsSpan().CopyTo(reportInformation.wzDescription.AsSpan());
"Diagnostic Tracing".AsSpan().CopyTo(reportInformation.wzFriendlyEventName.AsSpan());

Marshal.ThrowExceptionForHR(PInvoke.WerReportCreate(
    eventType,
    WER_REPORT_TYPE.WerReportCritical,
    reportInformation,
    out nint reportHandle));

Context

  • CsWin32 version: 0.1.506-beta
  • Target Framework: .NET 6.0
@palenshus palenshus added the bug Something isn't working label Sep 9, 2021
@AArnott
Copy link
Member

AArnott commented Sep 18, 2021

This is surprising. I can repro and am investigating.

@AArnott AArnott self-assigned this Sep 18, 2021
AArnott added a commit that referenced this issue Sep 18, 2021
…char` type

Without this, the .NET marshaler will assume Ansi, which is the C# default.

Fixes #389
@palenshus
Copy link
Author

Works like a charm now, thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants