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

Add ContextSwitcher class. #211

Merged
merged 1 commit into from
Feb 9, 2021
Merged

Add ContextSwitcher class. #211

merged 1 commit into from
Feb 9, 2021

Conversation

NN---
Copy link
Contributor

@NN--- NN--- commented Feb 9, 2021

@dahall dahall merged commit f45861e into dahall:master Feb 9, 2021
@dahall
Copy link
Owner

dahall commented Feb 9, 2021

I added a CoClass attribute to the IContextCallback so that you can new it directly.

var contextCallback = new IContextCallback();

Do you think anyone would use a helper class on this that would have methods to add COM objects and then automatically release them all on Dispose()?

@dahall
Copy link
Owner

dahall commented Feb 9, 2021

Something like:

public class ComContext : IDisposable
{
   private IContextCallback ctx;
   private List<(Type, object, CLSCTX, REGCLS)> regs = new List<(Type, object, CLSCTX, REGCLS)>();
   public ComContext() => ctx = new IContextCallback();

   public void Register<T>(T obj, CLSCTX clsCtx, REGCLS flags) => regs.Add(typeof(T), obj, clsCtx, flags);
   public void Dispose() => Disconnect();
   public void Connect() => ctx.ContextCallback(EnterCallback, default, IID_ContextCallback, 5);
   public void Disconnect() => ctx.ContextCallback(DisconnectCallback, default, IID_ContextCallback, 5);
   private HRESULT EnterCallback(in ComCallData data)
   {
      foreach (var i in regs)
         CoRegisterClassObj(i.....)
   }
   private HRESULT DisconnectCallback(in ComCallData data)
   {
      foreach (var i in regs)
         CoRevokeClassObj(i.....)
      return CoDisconnectContext(timout);
   }
}

@NN---
Copy link
Contributor Author

NN--- commented Feb 9, 2021

Don’t know whether anyone needs a helper class.
In fact this feature is a little bit esoteric , not so documented and used mainly in internal implementations.
There is a wrapper for it in CsWinRT: https://github.com/microsoft/CsWinRT/blob/cfbc973fd48453d53311706a3f4a9b8730c762c6/src/WinRT.Runtime/Interop/IContextCallback.cs

@NN---
Copy link
Contributor Author

NN--- commented Feb 9, 2021

Thanks for such fast response.

@NN--- NN--- deleted the contextswitcher branch February 10, 2021 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants