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

Convert exception help context parsing to managed #70251

Merged
merged 15 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 18 additions & 9 deletions src/tests/Interop/COM/NETClients/Primitives/ErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void Run()
{
this.VerifyExpectedException();
this.VerifyReturnHResult();
this.VerifyHelpLink();
}

private void VerifyExpectedException()
Expand All @@ -40,21 +41,29 @@ private void VerifyReturnHResult()

var hrs = new[]
{
unchecked((int)0x80004001),
unchecked((int)0x80004003),
unchecked((int)0x80070005),
unchecked((int)0x80070057),
unchecked((int)0x8000ffff),
-1,
1,
2
};
unchecked((int)0x80004001),
unchecked((int)0x80004003),
unchecked((int)0x80070005),
unchecked((int)0x80070057),
unchecked((int)0x8000ffff),
-1,
1,
2
};

foreach (var hr in hrs)
{
Assert.Equal(hr, this.server.Return_As_HResult(hr));
Assert.Equal(hr, this.server.Return_As_HResult_Struct(hr).hr);
}
}

private void VerifyHelpLink()
{
string helpLink = "C:\\Windows\\system32\\dummy.hlp";
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
uint helpContext = 5678;
var ex = Assert.Throws<COMException>(() => { this.server.Throw_HResult_HelpLink(unchecked((int)-1), helpLink, helpContext); });
Assert.Equal($"{helpLink}#{helpContext}", ex.HelpLink);
}
}
}
9 changes: 9 additions & 0 deletions src/tests/Interop/COM/NETServer/ErrorMarshalTesting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@ public Server.Contract.HResult Return_As_HResult_Struct(int hresultToReturn)
{
return new Server.Contract.HResult { hr = hresultToReturn };
}

public void Throw_HResult_HelpLink(int hresultToReturn, string helpLink, uint helpContext)
{
Marshal.GetExceptionForHR(hresultToReturn);

Exception e = Marshal.GetExceptionForHR(hresultToReturn);
e.HelpLink = $"{helpLink}#{helpContext}";
throw e;
}
}
43 changes: 43 additions & 0 deletions src/tests/Interop/COM/NativeClients/Primitives/ErrorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,48 @@ namespace
THROW_FAIL_IF_FALSE(hr == hrMaybe);
}
}

void VerifyHelpContext(_In_ IErrorMarshalTesting *et)
{
::printf("Verify expected helplink and context\n");

HRESULT hrs[] =
{
E_NOTIMPL,
E_POINTER,
E_ACCESSDENIED,
E_INVALIDARG,
E_UNEXPECTED,
HRESULT{-1},
S_FALSE,
HRESULT{2}
};

BSTR helpLink = SysAllocString(OLESTR("C:\\Windows\\system32\\dummy.hlp"));
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved

for (int i = 0; i < ARRAY_SIZE(hrs); ++i)
{
HRESULT hr = hrs[i];
DWORD helpContext = (DWORD)(i + 0x1234);
HRESULT hrMaybe = et->Throw_HResult_HelpLink(hr, helpLink, helpContext);
THROW_FAIL_IF_FALSE(hr == hrMaybe);

IErrorInfo* pErrInfo;
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
THROW_IF_FAILED(GetErrorInfo(0, &pErrInfo));

BSTR helpLinkMaybe;
THROW_IF_FAILED(pErrInfo->GetHelpFile(&helpLinkMaybe));
THROW_FAIL_IF_FALSE(VarBstrCmp(helpLink, helpLinkMaybe, LANG_ENGLISH, 0) == VARCMP_EQ);
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
SysFreeString(helpLinkMaybe);

DWORD helpContextMaybe;
THROW_IF_FAILED(pErrInfo->GetHelpContext(&helpContextMaybe));
THROW_FAIL_IF_FALSE(helpContext == helpContextMaybe);
pErrInfo->Release();
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
}

SysFreeString(helpLink);
}
}

void Run_ErrorTests()
Expand All @@ -89,4 +131,5 @@ void Run_ErrorTests()
VerifyExpectedException(errorMarshal);
VerifyReturnHResult(errorMarshal);
VerifyReturnHResultStruct(errorMarshal);
VerifyHelpContext(errorMarshal);
}
19 changes: 19 additions & 0 deletions src/tests/Interop/COM/NativeServer/ErrorMarshalTesting.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ class ErrorMarshalTesting : public UnknownImpl, public IErrorMarshalTesting
return hresultToReturn;
}

DEF_FUNC(Throw_HResult_HelpLink)(
/*[in]*/ int hresultToReturn,
/*[in]*/ BSTR helpLink,
/*[in]*/ DWORD helpContext)
{
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
ICreateErrorInfo* pCreateErrInfo;
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
CreateErrorInfo(&pCreateErrInfo);
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
pCreateErrInfo->SetHelpFile(helpLink);
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
pCreateErrInfo->SetHelpContext(helpContext);
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved

IErrorInfo* pErrInfo;
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
pCreateErrInfo->QueryInterface(IID_IErrorInfo, (void**)&pErrInfo);
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
SetErrorInfo(0, pErrInfo);
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
pErrInfo->Release();
pCreateErrInfo->Release();
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved

return HRESULT{ hresultToReturn };
}

public: // IUnknown
STDMETHOD(QueryInterface)(
/* [in] */ REFIID riid,
Expand Down
2 changes: 2 additions & 0 deletions src/tests/Interop/COM/ServerContracts/Server.Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ public interface IErrorMarshalTesting

[PreserveSig]
HResult Return_As_HResult_Struct(int hresultToReturn);

void Throw_HResult_HelpLink(int hresultToReturn, string helpLink, uint helpContext);
}

public enum IDispatchTesting_Exception
Expand Down
4 changes: 4 additions & 0 deletions src/tests/Interop/COM/ServerContracts/Server.Contracts.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ IErrorMarshalTesting : IUnknown
/*[in]*/ int hresultToReturn ) = 0;
virtual int STDMETHODCALLTYPE Return_As_HResult_Struct (
/*[in]*/ int hresultToReturn ) = 0;
virtual HRESULT STDMETHODCALLTYPE Throw_HResult_HelpLink (
/*[in]*/ int hresultToReturn,
/*[in]*/ BSTR helpLink,
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
/*[in]*/ DWORD helpContext ) = 0;
};

enum IDispatchTesting_Exception
Expand Down