Skip to content

Fix MSO interfaces - #1565

Merged
RussKie merged 7 commits into
dotnet:masterfrom
JeremyKuhne:mso
Sep 5, 2019
Merged

RussKie merged 7 commits into
dotnet:masterfrom
JeremyKuhne:mso

Conversation

@JeremyKuhne

@JeremyKuhne JeremyKuhne commented Aug 3, 2019 •

Copy link
Copy Markdown
Member

This fixes MSO component interfaces so that they correctly match the COM interface definitions. It also hardens against being invoked with null pointers.

See related issue: #247

Need to define the interop scenarios with Office to fully test this. Wanted to make sure this PR is active for visibility. (And to prevent others from replicating the work I did here.)

Proposed changes

  • Update component manager interfaces to be correct on both x32 and x64
  • Remove unnecessary app domain related proxies
  • Simplify component manager construction logic

Customer Impact

  • Message processing is faster (removed layers, lighter com interop when hosted)
  • APIs are now correct and safer (particularly on x64)

Regression?

  • No

Risk

  • Moderate for hosted scenarios (with a present IMsoComponentManager or hosting a native control that needs it), but outside of that low. I don't believe that is a common scenario for this release.

Test methodology

  • Ran existing tests (this impacts the message loop, so it is broadly hit)
  • Added basic tests (there were none)

The tests that still don't exist are message pumping related. Can probably do it if we fire up a separate thread for that specific test with it's own message pump?

Microsoft Reviewers: Open in CodeFlow

@JeremyKuhne
JeremyKuhne requested a review from a team as a code owner August 3, 2019 01:20

@weltkante weltkante left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done a first look through this, seems like you have made a mistake with the component ids. I'm referencing office 2010 headers, if you have newer onces yours might be more correct, but I doubt they broke compatibility. (The office 2010 headers are in your VS 2017 installation under VSSDK\VisualStudioIntegration\Common\Inc\office10 - don't know if VS 2019 has them somewhere different. You can find the component manager headers in msoci.h and the VS version in the parent folder under olecm.h, but VS doesn't support 64 bit so the office ones are to be preferred I think)

In addition to your component id confusion you added dereferencing operations into the component ids which are not supposed to be there. Component IDs are DWORD_PTR in my header and while the host (VS or Office) can put a pointer to an uint there it doesn't have to, it might just as well be a numeric counter. DWORD_PTR does not mean "pointer to DWORD", it means it was a DWORD potentially holding a pointer variable on 32 bit, so it needed to be extended on 64 bit Windows. Since you aren't the owner of the content in no case can you safely dereference a DWORD_PTR because you don't know what it points to, or if it points to anything at all and isn't just a plain numeric value.

I didn't review Application.ComponentManager.cs and Application.ThreadContext.cs because they moved to a different file so its not easily viewed in github, I might check later with external tools.

Also I noted that you didn't yet refactor the AppDomain code to always assume Default Domain, which means the changeset won't run as-is on .NET Core because it tries to a call a nonexistant API (throws at runtime)

@codecov

codecov Bot commented Aug 3, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #1565 into master will decrease coverage by 0.02696%.
The diff coverage is 56.57805%.

@@                Coverage Diff                 @@
##              master      #1565         +/-   ##
==================================================
- Coverage   26.03666%   26.0097%   -0.02696%     
==================================================
  Files            830        815         -15     
  Lines         267542     267258        -284     
  Branches       37941      37893         -48     
==================================================
- Hits           69659      69513        -146     
+ Misses        192956     192814        -142     
- Partials        4927       4931          +4
Flag Coverage Δ
#Debug 26.0097% <56.57805%> (-0.02697%) ⬇️
#production 26.0097% <56.57805%> (-0.02697%) ⬇️
#test 100% <ø> (ø) ⬆️

@JeremyKuhne
JeremyKuhne force-pushed the mso branch 2 times, most recently from 4b22546 to 89a85a5 Compare August 3, 2019 22:44
@JeremyKuhne

Copy link
Copy Markdown
Member Author

Broke this into 3 commits. First is just breaking out the classes, second is fixing style (and using Path.Join in one spot), third is the actual functional changes.

@JeremyKuhne

Copy link
Copy Markdown
Member Author

Tried hardening FGetActiveComponent. Sort of ugly, but I think I'm doing the right thing...

@weltkante weltkante left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good to me now, with one minor bug introduced in FOnComponentExitState.

I'll have to do some further review of the usage of MSG structs across callers/implementors before I agree that your removal of the ANSI/UNICODE split is safe.

@JeremyKuhne
JeremyKuhne force-pushed the mso branch 2 times, most recently from b3fcde9 to 93b69c8 Compare August 5, 2019 00:22
@JeremyKuhne

Copy link
Copy Markdown
Member Author

I put back the A/W Get/Dispatch and extended it to FContinueMessageLoop.

@RussKie RussKie changed the title Fix MSO interfaces WIP: Fix MSO interfaces Aug 5, 2019
@RussKie

RussKie commented Aug 5, 2019

Copy link
Copy Markdown
Contributor

@JeremyKuhne please fill in the compliance sections.

@RussKie RussKie added waiting-author-feedback The team requires more information from the author 🚫 * NO-MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) labels Aug 5, 2019
@ghost ghost removed the waiting-author-feedback The team requires more information from the author label Aug 6, 2019
@JeremyKuhne

Copy link
Copy Markdown
Member Author

Proposed changes

  • Update component manager interfaces to be correct on both x32 and x64
  • Remove unnecessary app domain related proxies
  • Simplify component manager construction logic

Customer Impact

  • Message processing is faster (removed layers, lighter com interop when hosted)
  • APIs are now correct and safer (particularly on x64)

Regression?

  • No

Risk

  • Moderate for hosted scenarios (with a present IMsoComponentManager or hosting a native control that needs it), but outside of that low. I don't believe that is a common scenario for this release.

Test methodology

  • Ran existing tests (this impacts the message loop, so it is broadly hit)
  • Added basic tests (there were none)

The tests that still don't exist are message pumping related. Can probably do it if we fire up a separate thread for that specific test with it's own message pump?

@weltkante

weltkante commented Aug 6, 2019 •

Copy link
Copy Markdown
Contributor

Moderate for hosted scenarios (with a present IMsoComponentManager or hosting a native control that needs it), but outside of that low. I don't believe that is a common scenario for this release.

Did anyone even test if this is working with Office? If you unblock the currently not working scenario of writing a .NET Core Office Addin without having tested the refactored code at all against a native IComponentManager I would consider that a not unsignificant risk of breaking Office.

Previously this threw a PlatformNotSupportedException or something along those lines, due to the usage of AppDomain code. This prevents people from writing bad addins by failing early.

Personally I don't think moving untested code into 3.0 release is a good idea. If you really want to get it in untested against Office you may want to consider disabling Office integration by turning off the lookup of a native IComponentManager.

@JeremyKuhne

Copy link
Copy Markdown
Member Author

Did anyone even test if this is working with Office?

No, as I presume we're fundamentally broken there with .NET Core. If you know of how to do this, please share. I'm actually surprised that anything would work unless VSTO was updated for .NET Core. (I also have no idea how the runtime is hosted for Office. I presumed that loading Core into a process with Framework wasn't possible- so we may be fundamentally blocked?)

I do understand that this area isn't well tested, the reason I put this up here was that I had already started investigating and I didn't want others to duplicate effort. I wanted a reference that could be used to potentially fix issues in the Framework version of WinForms. That all said, this does remove a lot of cruft and sets us up with a much less broken starting point, if not a working starting point. It is at least unit tested, which is more than we did have. I'd rather people be exploring and opening issues on this base than the other, but I'm also fine with this going in after 3.0 branches away.

@weltkante

Copy link
Copy Markdown
Contributor

No, as I presume we're fundamentally broken there with .NET Core. If you know of how to do this, please share. I'm actually surprised that anything would work unless VSTO was updated for .NET Core. (I also have no idea how the runtime is hosted for Office. I presumed that loading Core into a process with Framework wasn't possible- so we may be fundamentally blocked?)

This is a COM addin API, loading of the framework happens as a side effect, Office has no part in it. Since I understand COM is supported by .NET Core 3 you can register the necessary keys and Office will load the addin just like any other addin.

We have several Office addins on Desktop Framework in our products and our current plan is to look at a first attempt porting them when approaching dotnet 5. (We first have to move and restructure a lot of other components since they are using several parts of Desktop Framework which are no longer supported on Core.) There is currently no time pressure getting this in, I don't think anyone asked for this feature, my only worry is not getting it right the first time.

I may be able to write some simple example addin on a weekend if it helps, but ideally someone from Office would coordinate since this is very much underdocumented internal API.

I do understand that this area isn't well tested, the reason I put this up here was that I had already started investigating and I didn't want others to duplicate effort. I wanted a reference that could be used to potentially fix issues in the Framework version of WinForms. That all said, this does remove a lot of cruft and sets us up with a much less broken starting point, if not a working starting point. It is at least unit tested, which is more than we did have. I'd rather people be exploring and opening issues on this base than the other, but I'm also fine with this going in after 3.0 branches away.

Yes I understand that, I just wanted to raise a word of caution against breaking Office. If this scenarion is unlocked people will eventually try it out, and if it breaks Office this isn't great.

The problem with waiting for issues raised is that this kind of breaking can go undetected a very long time and just surface on customer sides. I doubt the average Office user will be raising issues against WinForms Core because some modal dialog locks up when having a certain combination of addins active.

A workable compromise may be removing the native ComponentManager integration for now (just not query for it and always use your own). This sacrifices communication of modality with Office, but at least its not breaking things actively. I don't know how WPF works in Office addins (we only ever used WinForms in our projects) but as far as I can look into the WPF source I have yet to see any ComponentManager integration. Maybe removing this works just as well and it only needs to be re-added if Office says they need it for modality handling.

@RussKie

RussKie commented Aug 7, 2019

Copy link
Copy Markdown
Contributor

@merriemcgaw do you know who we can engage from the Office team to help us test the changes?

@RussKie RussKie removed the 🚫 * NO-MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) label Aug 8, 2019
@zsd4yr

zsd4yr commented Aug 12, 2019

Copy link
Copy Markdown
Contributor

@OliaG may know our contact as well 🌘 👩‍🚀

@RussKie RussKie added the 🚫 * NO-MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) label Aug 13, 2019
@RussKie RussKie added this to the Future milestone Aug 13, 2019
@JeremyKuhne

Copy link
Copy Markdown
Member Author

Rebased

@RussKie RussKie added the 🚫 * NO-MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) label Sep 3, 2019
@JeremyKuhne

Copy link
Copy Markdown
Member Author

@RussKie why are you putting No-Merge back on these? I understood that Master was open again. @zsd4yr is that not the case?

@RussKie

RussKie commented Sep 4, 2019

Copy link
Copy Markdown
Contributor

I don't want anything to be merged before #1684 is complete. We have found few issues already, I don't want to deal with red herrings.
The re-targetting is almost done, it won't be long before we'll start merging changes into the master again.

@RussKie RussKie removed the 🚫 * NO-MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) label Sep 4, 2019
@JeremyKuhne

Copy link
Copy Markdown
Member Author

I don't want anything to be merged before #1684 is complete.

Thanks for the details!

@JeremyKuhne JeremyKuhne changed the title WIP: Fix MSO interfaces Fix MSO interfaces Sep 4, 2019
@JeremyKuhne

Copy link
Copy Markdown
Member Author

Created #1763 to track understanding/testing Office interop. Removed WIP as the code is reviewed and unit tested and would like to get this in as a baseline for further work.

@JeremyKuhne

Copy link
Copy Markdown
Member Author

@RussKie, @zsd4yr if you're ok with the state of this can you approve? I've written unit tests and I've created an issue to track understanding the interop scenario. I don't think it's worth trying to hold onto this PR until we understand the scenario better. It's taxing and will conflict a ton with other pending cleanup work.

@RussKie RussKie modified the milestones: Future, 5.0 Sep 5, 2019
@RussKie RussKie added code cleanup cleanup code for unused apis/properties/comments - no functional changes. enhancement Product code improvement that does NOT require public API changes/additions labels Sep 5, 2019

@RussKie RussKie left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me.
I'm still confused with the use of bool and BOOL in p/invokes - what are the guidelines for these?

Comment thread src/Common/src/UnsafeNativeMethods.cs Resolved
Move the nested classes in Application to separate files.
This fixes MSO component interfaces so that they correctly match the COM interface definitions. It also hardens against being invoked with null pointers.
…pDomain world of .NET Core.

Attempt to reduce the complexity on component manager initialization.
@JeremyKuhne

Copy link
Copy Markdown
Member Author

Rebased and addressed feedback

@RussKie
RussKie merged commit 236533c into dotnet:master Sep 5, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Feb 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

code cleanup cleanup code for unused apis/properties/comments - no functional changes. enhancement Product code improvement that does NOT require public API changes/additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants