microsoft / microsoft/CsWinRT

Roslyn analyzer/code fixer for use of Marshal.* functions

Open
#561 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C#
Stars
665
Forks
134
Avg merge
1d 3h
Merged PRs (30d)
32

Description

When using C#/WinRT, how developers do COM interop has changed a bit.

Before:


Guid guid = new Guid(ISurfaceImageSourceNative);
var imageSource = new MyImageSource();
IntPtr pointer = Marshal.GetIUnknownForObject(imageSource);
IntPtr pOut;
Marshal.QueryInterface(pointer, ref guid, out pOut);
var nativeInterface = (ISurfaceImageSourceNative)Marshal.GetObjectForIUnknown(pOut);
nativeInterface = null;
Marshal.Release(pOut);

Now:

var imageSource = new MyImageSource();
var nativeInterface = imageSource.As<ISurfaceImageSourceNative>();

The new code is so much prettier! But it will be a stumbling block for developers who are used to the old pattern. I think it should be possible to detect usage of Marshal.* APIs with a Roslyn analyzer, and hopefully a code-fixer as well. This could be added to https://github.com/dotnet/try-convert/issues/308

Thoughts?

Edit: Here's the definition of ISurfaceImageSourceNative

[ComImport, Guid(MainWindow.ISurfaceImageSourceNative), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISurfaceImageSourceNative {
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the requested Roslyn analyzer and code-fixer scope around the Marshal.* usage pattern shown in the issue, along with the referenced try-convert issue 308. Done should include a defined detection and migration behavior for the old COM interop pattern, but the issue does not name repository files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.