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

How to use CsWin32 generated interface / using newer Win32Metadata leads to compile time error #135

Closed
HaraldMuehlhoffCC opened this issue Feb 20, 2021 · 1 comment

Comments

@HaraldMuehlhoffCC
Copy link

Firstly thanks for your cool project!

We're currently using

<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.17134.1000" />

to access Sensor apis in a .NET Core 3.1 WinForms application.

We would like to migrate to .NET 5 but sadly only newer TargetFrameworks (e.g. net5-windows10.0.17763.0) are supported and our customers are still using older Windows 10 versions (special hardware).

Generating ISensorManager and other types using CsWin32 works. Where I still fail is at casting the COM object to ISensorManager:

Guid CLSID_SensorManager = new Guid("77A1C827-FCD2-4689-8915-9D613CC5FA3E");
Type type = Type.GetTypeFromCLSID(CLSID_SensorManager, true);
var sensorManager = Activator.CreateInstance(type);
var x = (ISensorManager) sensorManager;

"Unable to cast COM object of type 'System.__ComObject' to class type 'Microsoft.Windows.Sdk.ISensorManager'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface."

I tried to include newer Win32Metadata but anything newer than preview.5 leads to compile time errors even though unsafe code is allowed for the project:

<PackageReference Include="Microsoft.Windows.SDK.Win32Metadata" Version="10.0.19041.5-preview.20" />

Microsoft.Windows.CsWin32.SourceGenerator\PWSTR.g.cs(21,41,21,47): error CS0214: Zeiger und Puffer fester Größe können nur in einem unsicheren Kontext verwendet werden. (pointers & buffers of fixed size can only be used in an unsafe context)

Any help would be very much appreciated!

@HaraldMuehlhoffCC HaraldMuehlhoffCC changed the title Using CsWin32 generated interface / using newer Win32Metadata leads to compile time error How to use CsWin32 generated interface / using newer Win32Metadata leads to compile time error Feb 20, 2021
@AArnott
Copy link
Member

AArnott commented Feb 20, 2021

using newer Win32Metadata leads to compile time error

Duplicate of #121

How to use CsWin32 generated interface

We don't generate interfaces. We generate structs that look like the COM interface.
See #86 for more on this and our future plans.

@AArnott AArnott closed this as completed Feb 20, 2021
AArnott added a commit that referenced this issue May 6, 2022
Accommodates microsoft/azure-pipelines-tasks#13821 by publish symbols on a Windows agent

Closes #135
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

No branches or pull requests

2 participants