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

OutOfProcCOM not working with clients in PowerShell or using C# dynamic #22364

Open
1 of 3 tasks
Molinarius opened this issue Jan 14, 2021 · 6 comments
Open
1 of 3 tasks
Labels
Pri3 support-request Support-style question;customer needs help solving a problem [org][type][category] waiting-on-feedback Waiting for feedback from SMEs before they can be merged

Comments

@Molinarius
Copy link

Issue description

With clients in PowerShell or using C# dynamic, I cannot get the OutOfProcCOM example to work; I additionally tried it with Pyhton, too, and it didn't work, but I'm not too firm with Pyhton.

C# .NET5 dynamic:

> Environment.Version
[5.0.2]
> var type = Type.GetTypeFromCLSID(Guid.Parse("AF080472-F173-4D9D-8BE7-435776617347"));
> dynamic c = Activator.CreateInstance(type);
> c.ComputePi()
System.NotSupportedException: Specified method is not supported.
  + System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(int)
  + Microsoft.CSharp.RuntimeBinder.ComInterop.ComRuntimeHelpers.GetITypeInfoFromIDispatch(Microsoft.CSharp.RuntimeBinder.ComInterop.IDispatch)
  + Microsoft.CSharp.RuntimeBinder.ComInterop.IDispatchComObject.EnsureScanDefinedMethods()
  + Microsoft.CSharp.RuntimeBinder.ComInterop.IDispatchComObject.System.Dynamic.IDynamicMetaObjectProvider.GetMetaObject(System.Linq.Expressions.Expression)
  + System.Dynamic.DynamicMetaObject.Create(object, System.Linq.Expressions.Expression)
  + System.Dynamic.DynamicMetaObjectBinder.Bind(object[], ReadOnlyCollection<System.Linq.Expressions.ParameterExpression>, System.Linq.Expressions.LabelTarget)
  + System.Runtime.CompilerServices.CallSiteBinder.BindCore<T>(CallSite<T>, object[])
  + System.Dynamic.UpdateDelegates.UpdateAndExecute1<T0, TRet>(System.Runtime.CompilerServices.CallSite, T0)
  + System.Dynamic.UpdateDelegates.UpdateAndExecute1<T0, TRet>(System.Runtime.CompilerServices.CallSite, T0)

Exactly the same exception was thrown after I added dynamic dyn = obj; dyn.ComputePi(); in the ManagedClient project of the example.

C# .NET Framework 4 dynamic:

> Environment.Version
[4.0.30319.42000]
> var type = Type.GetTypeFromCLSID(Guid.Parse("AF080472-F173-4D9D-8BE7-435776617347"));
> dynamic c = Activator.CreateInstance(type);
> c.ComputePi()
System.NotSupportedException: Specified method is not supported.
  + System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(int, System.IntPtr)
  + System.Dynamic.ComRuntimeHelpers.GetITypeInfoFromIDispatch(System.Dynamic.IDispatch, bool)
  + System.Dynamic.IDispatchComObject.EnsureScanDefinedMethods()
  + System.Dynamic.IDispatchComObject.System.Dynamic.IDynamicMetaObjectProvider.GetMetaObject(System.Linq.Expressions.Expression)
  + System.Dynamic.DynamicMetaObject.Create(object, System.Linq.Expressions.Expression)
  + System.Dynamic.DynamicMetaObjectBinder.Bind(object[], ReadOnlyCollection<System.Linq.Expressions.ParameterExpression>, System.Linq.Expressions.LabelTarget)
  + System.Runtime.CompilerServices.CallSiteBinder.BindCore<T>(CallSite<T>, object[])
  + System.Dynamic.UpdateDelegates.UpdateAndExecute1<T0, TRet>(System.Runtime.CompilerServices.CallSite, T0)
  + System.Dynamic.UpdateDelegates.UpdateAndExecute1<T0, TRet>(System.Runtime.CompilerServices.CallSite, T0)

PowerShell 5:

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17763.1490
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.1490
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

> $type = [Type]::GetTypeFromCLSID("AF080472-F173-4D9D-8BE7-435776617347")
> $c = [Activator]::CreateInstance($type)
> $c.ComputePi()
Specified method is not supported.
At line:1 char:1
+ $c.ComputePi()
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], NotSupportedException
    + FullyQualifiedErrorId : System.NotSupportedException

PowerShell 7.1.0:

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.1.0
PSEdition                      Core
GitCommitId                    7.1.0
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

> $type = [Type]::GetTypeFromCLSID("AF080472-F173-4D9D-8BE7-435776617347")
> $c = [Activator]::CreateInstance($type)
> $c.ComputePi()
OperationStopped: Specified method is not supported.

I tried adding support for IDispatch to the OutOfProcCOM example, and changed the target to .NET 5, but the behavior remained unchanged. I guess I didn't add that IDispatch support correctly because I don't have the necessary knowledge.

By the way, an out-of-process COM server I created with .NET Framework 4.7 (what required some workarounds) works with PowerShell and C# dynamic clients and all other clients I know.

Target framework

  • .NET Core 3.1, 5
  • .NET Framework
  • .NET Standard
dotnet --info output or About VS info
.NET SDK (reflecting any global.json):
 Version:   5.0.102
 Commit:    71365b4d42

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17763
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.102\

Host (useful for support):
  Version: 5.0.2
  Commit:  cb5f173b96

.NET SDKs installed:
  5.0.102 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]```
</details>
@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label Jan 14, 2021
@Molinarius
Copy link
Author

Maybe the reason for this is the issue dotnet/runtime#47329.

@tdykstra
Copy link
Contributor

tdykstra commented May 4, 2021

@elinor-fung Can you help with this question?

@tdykstra tdykstra added support-request Support-style question;customer needs help solving a problem [org][type][category] waiting-on-feedback Waiting for feedback from SMEs before they can be merged and removed ⌚ Not Triaged Not triaged labels May 4, 2021
@elinor-fung
Copy link
Member

Hi @Molinarius - dotnet/runtime#47329 was definitely causing issues here. The fix for that should be in .NET 5.0.5 and newer.

Aside from that, there are few tweaks needed in the COM server to support IDispatch and specify a default COM interface in the implementations: dotnet/samples#4589. With that change, the sample should be usable with the dynamic keyword from a client running on .NET 5.0.5+.

@Molinarius
Copy link
Author

Thanks.

I noticed that with clients using .NET Framework 4.x, dynamic still doesn’t work, but native clients and clients using .NET 5.0.6 work as expected.

@Molinarius
Copy link
Author

As this issue is still open: I can't get it to work with PowerShell 7.2.1 (what uses .NET 6 what is > .NET 5.0.5):

  1. In ExeServer.csproj, change <TargetFramework>netcoreapp3.1</TargetFramework> to <TargetFramework>net6.0</TargetFramework>.

  2. Compile with or without -p:EmbedTypeLibrary=true.

  3. Register.

  4. Check that NativeClient.exe and ManagedClient.exe work.

  5. With pwsh:

    > $c = [System.Activator]::CreateInstance([Type]::GetTypeFromCLSID("af080472-f173-4d9d-8be7-435776617347"))
    > $c.ComputePi()
    OperationStopped: Typelib export: Type library is not registered. (0x80131165)
    

Am I doing something wrong, or is something missing in the example?

@lm-ls
Copy link

lm-ls commented May 11, 2023

.NET 7, still not working in PowerShell:

Typelib export: Type library is not registered. (Exception from HRESULT: 0x80131165)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pri3 support-request Support-style question;customer needs help solving a problem [org][type][category] waiting-on-feedback Waiting for feedback from SMEs before they can be merged
Projects
None yet
Development

No branches or pull requests

6 participants