Skip to content

Commit

Permalink
Handle int/HRESULT return value when PreserveSig = false
Browse files Browse the repository at this point in the history
  • Loading branch information
kant2002 committed Jun 5, 2021
1 parent 56e21d4 commit 599da74
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/coreclr/tools/Common/TypeSystem/IL/Stubs/PInvokeILEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,12 @@ private void EmitDelegateCall(DelegateMarshallingMethodThunk delegateMethod, PIn

private void EmitPInvokeCall(PInvokeILCodeStreams ilCodeStreams)
{
if (!_flags.PreserveSig && _targetMethod.Signature.ReturnType != _targetMethod.Context.GetWellKnownType(WellKnownType.Void))
throw new NotSupportedException();
if (!_flags.PreserveSig)
{
TypeDesc returnType = _targetMethod.Signature.ReturnType;
if (!returnType.IsEnum && returnType != _targetMethod.Context.GetWellKnownType(WellKnownType.Void) && returnType != _targetMethod.Context.GetWellKnownType(WellKnownType.Int32))
throw new NotSupportedException();
}

ILEmitter emitter = ilCodeStreams.Emitter;
ILCodeStream fnptrLoadStream = ilCodeStreams.FunctionPointerLoadStream;
Expand Down Expand Up @@ -306,9 +310,21 @@ private void EmitPInvokeCall(PInvokeILCodeStreams ilCodeStreams)

if (!_flags.PreserveSig)
{
ILLocalVariable tempResult = emitter.NewLocal(context
.GetWellKnownType(WellKnownType.Int32));
if (_targetMethod.Signature.ReturnType != _targetMethod.Context.GetWellKnownType(WellKnownType.Void))
{
callsiteSetupCodeStream.EmitStLoc(tempResult);
callsiteSetupCodeStream.EmitLdLoc(tempResult);
}

callsiteSetupCodeStream.Emit(ILOpcode.call, emitter.NewToken(
InteropTypes.GetMarshal(context)
.GetKnownMethod("ThrowExceptionForHR", null)));
if (_targetMethod.Signature.ReturnType != _targetMethod.Context.GetWellKnownType(WellKnownType.Void))
{
callsiteSetupCodeStream.EmitLdLoc(tempResult);
}
}

// if the SetLastError flag is set in DllImport, call the PInvokeMarshal.SaveLastError
Expand Down

0 comments on commit 599da74

Please sign in to comment.