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

Using Dynamic with COM object doesn't work #32630

Open
dotMorten opened this Issue Oct 5, 2018 · 6 comments

Comments

Projects
None yet
4 participants
@dotMorten
Copy link

dotMorten commented Oct 5, 2018

I'm trying to use dynamic against COM objects to access the Location API LocationDisp.LatLongReportFactory as described here: https://docs.microsoft.com/en-us/windows/desktop/LocationAPI/locationdisp-latlongreportfactory

This works fine on .NET Framework, but fails on .NET Core when trying to access properties and methods on the COM object.

Repro steps:

            Type t = Type.GetTypeFromCLSID(new Guid("9DCC3CC8-8609-4863-BAD4-03601F4C65E8"));
            dynamic handle = Activator.CreateInstance(t);

            //Fails on .NET Core (works on .NET Framework):
            uint accuracy = handle.DesiredAccuracy;
            
            // Using reflection approach instead succeeds:
            var accuracy2 = t.InvokeMember("DesiredAccuracy", 
                Reflection.BindingFlags.Instance | Reflection.BindingFlags.GetProperty | Reflection.BindingFlags.Public,
                null, handle, new object[] { });

Exception:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''System.__ComObject' does not contain a definition for 'DesiredAccuracy''
StackTrace:
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at System.Device.TestApp.Program.Main(String[] args) in e:\Sources\System.Device.TestApp\Program.cs:line 16

image

I'm seeing this behavior both with 2.1 and 3.0-preview

@jkotas

This comment has been minimized.

Copy link
Member

jkotas commented Oct 5, 2018

cc @AaronRobinsonMSFT

@AaronRobinsonMSFT AaronRobinsonMSFT self-assigned this Oct 5, 2018

@jeffschwMSFT jeffschwMSFT added this to the 3.0 milestone Oct 16, 2018

@jeffschwMSFT jeffschwMSFT modified the milestones: 3.0, Future Jan 30, 2019

@jeffschwMSFT

This comment has been minimized.

Copy link
Member

jeffschwMSFT commented Jan 30, 2019

Per @richlander "It requires a dependency that we don’t want to take in a lean stack. That said, it would be straightforward for anyone to build a layer on top that did this."

@dotMorten

This comment has been minimized.

Copy link
Author

dotMorten commented Jan 30, 2019

@jeffschwMSFT @richlander
That really worries me. This means we can't port/use the existing desktop GeoCoordinate APIs for fetching device location, one of the larger items missing from the .net core 3.0 supported APIs list.

@jeffschwMSFT

This comment has been minimized.

Copy link
Member

jeffschwMSFT commented Jan 30, 2019

@dotMorten perhaps we can find a way to enable this without the use of dynamic. Check out the Excel sample: https://github.com/dotnet/samples/tree/master/core/extensions/ExcelDemo
You can use native com objects in a statically typed way.

@dotMorten

This comment has been minimized.

Copy link
Author

dotMorten commented Jan 31, 2019

@jeffschwMSFT Yeah I tried that and failed (it doesn't generate any APIs). After your post I tried again, and somehow it now works. That's awesome.

I would however have preferred a way where System.Device.dll didn't have to be completely rewritten, but a straight port "just works"

@jeffschwMSFT

This comment has been minimized.

Copy link
Member

jeffschwMSFT commented Jan 31, 2019

@AaronRobinsonMSFT is taking a closer look, but I am happy to see that our recent work enabling COM is at least one path forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment