-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Microsoft.VisualBasic.Information.TypeName
returns wrong name for COM objects
#35937
Comments
Tagging subscribers to this area: @cston |
The change in runtime/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Information.vb Line 500 in ddade10
Seems to be due to the lack of support for |
I tried to do the implementation based on the referencesource, declaring a IDispatch interface locally to test, but when call pDispatch.GetTypeInfo, it throws System.Runtime.InteropServices.COMException: A null reference pointer was passed to the code fragment. (0x800706F4) (original message was in my language). Tried to debug, not I could not find the root cause. |
@ehasis if you have a branch to share, perhaps @AaronRobinsonMSFT could have a quick look and maybe have a suggestion? nearly out of time to fix this for 5.0 release. |
Agree. I am close to vacation, next Monday, but if I can get a repo or branch to continue with I can take a look today or tomorrow. |
@AaronRobinsonMSFT here is the branch: https://github.com/ehasis/runtime/tree/vb-typename |
@ehasis Thanks for the project. The first issue here is the function we are updating (i.e.
We are calling My application is Imports System
Module Program
Sub Main(args As String())
Console.WriteLine("Hello World!")
Dim xl = CreateObject("Excel.Application")
Console.WriteLine(TypeName(xl))
xl.Quit()
End Sub
End Module @danmosemsft Who is our resident Visual Basic expert? I have no idea how this language is supposed to work or how method lookup occurs. Based on Visual Studio, I would assume |
@ehasis I think the function we actually need to update is the one in Versioned.vb - the reference source is at https://github.com/microsoft/referencesource/blob/a7bd3242bd7732dec4aebb21fbc0f6de61c2545e/Microsoft.VisualBasic/runtime/msvbalib/Helpers/Versioned.vb#L115-L135. I think @cston is going to have to be the authority here since I really don't understand this layering at all. More than willing to help with any COM related issues though. |
@cston is it |
Digging more about this problem, the thing got a bit confusing. In the reference source, the Microsoft.VisualBasic.Information.TypeName calls Microsoft.VisualBasic.Information.LegacyTypeNameOfCOMObject that retrieve the name from the IDispatch. But when compiled with .NET Framework, the code calls to Microsoft.VisualBasic.CompilerServices.Versioned.TypeName. Same happens when compiled with .NET Core, as noted by @AaronRobinsonMSFT. The details is that in the reference source Microsoft.VisualBasic.CompilerServices.Versioned.TypeName calls Microsoft.VisualBasic.Information.TypeNameOfCOMObject that have a bit more complex implementation, but is very similar to Microsoft.VisualBasic.Information.LegacyTypeNameOfCOMObject. The actual core implementation does not have this round trip. |
Yes, the VisualBasic compiler remaps calls to
Yes, it will be necessary to implement |
Ok, I'll implement on that way. |
@cston I have fully implemented the TypeName to match the reference source in the branch https://github.com/ehasis/runtime/tree/vb-typename, but it's still not working as expected. Still returns _ComObject for net5.0. Also, when the test runs with net461, expected for Scripting.Dictonary is Dictionary, while the actual is IDictionary. |
@ehasis This is failing at https://github.com/ehasis/runtime/blob/d9ceef9814040a3f7a7037c418532859c2209314/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Information.vb#L196. The |
@ehasis Good news first - the code in your branch works as expected. Bad news, the IL Linker is being a pain and breaking the COM definitions for In order to fix this the Linker needs to be told to leave these types alone. Add the below XML to a file call "ILLinkTrim.xml", place that file next to <linker>
<assembly fullname="Microsoft.VisualBasic.Core">
<!-- Required for type querying via IDispatch -->
<type fullname="Microsoft.VisualBasic.CompilerServices.UnsafeNativeMethods/*"/>
</assembly>
</linker> @danmosemsft @stephentoub @cston Unsure what the policy is on adding new linker trim files. I am heading on vacation soon so will be unable to shepherd this work further. |
@eerhardt can you provide guidance |
There are a few tracking issues for COM + ILLinker:
The workaround for .NET 5 is what @AaronRobinsonMSFT has above - Add an runtime/src/libraries/Microsoft.CSharp/src/ILLinkTrim.xml Lines 2 to 5 in 1ec6939
runtime/src/coreclr/src/System.Private.CoreLib/ILLinkTrim.xml Lines 12 to 19 in 1ec6939
|
@MichalStrehovsky Can you confirm that this is the appropriate fix for these COM scenarios in the framework? |
Yes. The other option would be to opt out the assembly from trimming - I assume the trimming that's problematic here is the trimming that happens at the framework build where everything is forced to trim, no matter if it's compatible or not. COM is linker unfriendly and is supposed to get detected as such (I hope there's warning for this somewhere). |
* Initial try of TypeName for ComObjects on Windows * Implemented TypeNameOfCOMObject to use in Versioned.TypeName * Separated tests of the TypeName for COM objects * Utils.VBFriendlyName now matches reference source * Moved TypeName for COM objects tests to VersionedTests * Added ILLinkTrim.xml as a temporary solution as comented on #35937 * UnsafeNativeMethods are only available on Windows * Test refactoration * Skip COM interop test on Mono * Explicit types in ILLinkTrim.xml * Call GetTypeFromProgID with throwOnError true * Disabled TypeName_ComObject test on Windows Nano
** Environment **
.NET Core SDK (reflecting any global.json):
Version: 5.0.100-preview.3.20216.6
Commit: 9f62a32109
Runtime Environment:
OS Name: Windows
OS Version: 10.0.18362
Office 365 Installed
** Reproducible on .NET Framework 4.8? **
No
** Steps to reproduce **
** Expected behavior **
Output:
ApplicationClass
** Actual behavior **
Output:
__ComObject
** Remarks **
I have some VB.NET code snippets that was migrated from VBScript. They don't work correctly when running on .NET 5 or .NET Core 3.1, because
TypeName
doesn't return correct result for COM objects.I believe this bug is caused by this function:
runtime/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Information.vb
Line 500 in ddade10
The .NET Framework version of this function:
https://github.com/microsoft/referencesource/blob/a7bd3242bd7732dec4aebb21fbc0f6de61c2545e/Microsoft.VisualBasic/runtime/msvbalib/Information.vb#L181
If
Microsoft.VisualBasic.Core
is not a good place to use COM interop, you can put implementation ofTypeNameOfCOMObject
inMicrosoft.VisualBasic.Forms
. WhenMicrosoft.VisualBasic.Forms
is not found at runtime, return "__ComObject" .The text was updated successfully, but these errors were encountered: