Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/Microsoft.Windows.CsWin32/Generator.FriendlyOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,17 +404,22 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverloads(MethodDefi

leadingOutsideTryStatements.AddRange([gcHandlesArrayDecl, strsArrayDecl, forLoop]);

// foreach (var gcHandle in paramNameGCHandles) gcHandle.Free();
var freeHandleStatement = ForEachStatement(
IdentifierName("var").WithTrailingTrivia(Space),
Identifier("gcHandle").WithTrailingTrivia(Space),
gcHandlesLocal.WithLeadingTrivia(Space),
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("gcHandle"),
IdentifierName("Free")))).WithLeadingTrivia(LineFeed));
// for (int i = 0; i < paramName.Length; i++)
// {
// paramNameGCHandles[i].Free()
// }
var freeHandleStatement = ForStatement(
VariableDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword))).AddVariables(
VariableDeclarator(loopVariable.Identifier).WithInitializer(EqualsValueClause(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))))),
BinaryExpression(SyntaxKind.LessThanExpression, loopVariable, GetSpanLength(origName, false)),
SingletonSeparatedList<ExpressionSyntax>(PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, loopVariable)),
Block().AddStatements(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ElementAccessExpression(gcHandlesLocal).AddArgumentListArguments(Argument(loopVariable)),
IdentifierName("Free"))).WithArgumentList(ArgumentList()))));

// ArrayPool<GCHandle>.Shared.Return(gcHandlesArray);
var returnGCHandlesArray = ExpressionStatement(
Expand Down
34 changes: 34 additions & 0 deletions test/GenerationSandbox.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,38 @@ public void FieldWithAssociatedEnum()
s.dwDescriptionId = SHDID_ID.SHDID_FS_FILE;
Assert.Equal(SHDID_ID.SHDID_FS_FILE, s.dwDescriptionId);
}

// Test for https://github.com/microsoft/CsWin32/issues/1421
[Fact]
public void RmRegisterResourcesDoesNotThrow()
{
var fileNames = new[] { @"C:\Windows\System32\notepad.exe" };

var sessionKey = new char[PInvoke.CCH_RM_SESSION_KEY + 1];
var rc = PInvoke.RmStartSession(out var session, sessionKey);
ThrowExceptionForError(rc);
try
{
// Throws InvalidOperationException: Handle is not initialized
rc = PInvoke.RmRegisterResources(session, fileNames, rgApplications: null, rgsServiceNames: null);
ThrowExceptionForError(rc);

// RmGetList call removed for brevity
}
finally
{
rc = PInvoke.RmEndSession(session);
ThrowExceptionForError(rc);
}

void ThrowExceptionForError(WIN32_ERROR error)
{
if (error == WIN32_ERROR.NO_ERROR)
{
return;
}

throw new Win32Exception((int)(uint)error);
}
}
}
9 changes: 7 additions & 2 deletions test/GenerationSandbox.Tests/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
BM_REQUEST_TYPE
BOOL
BOOLEAN
CCH_RM_SESSION_KEY
CHAR
CreateCursor
CreateFile
Expand All @@ -15,7 +16,6 @@ GetProcAddress
GetTickCount
GetWindowText
GetWindowTextLength
RmRegisterResources
HDC_UserSize
HIWORD
HRESULT_FROM_WIN32
Expand All @@ -42,6 +42,10 @@ PZZWSTR
RECT
RegLoadAppKey
RM_PROCESS_INFO
RmEndSession
RmRegisterResources
RmRegisterResources
RmStartSession
S_OK
SetupDiGetDeviceInterfaceDetail
SHDESCRIPTIONID
Expand All @@ -55,5 +59,6 @@ SIZE
VARDESC
WER_REPORT_INFORMATION
wglGetProcAddress
WIN32_ERROR
WPARAM
WriteFile
WriteFile
Loading