microsoft / CsWin32

A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.
MIT License
2.1k stars 90 forks source link

CS1503: Argument cannot convert from System.GUID to System.GUID* at TFM net35 #888

Closed peaceshi closed 1 year ago

peaceshi commented 1 year ago

Actual behavior

CS1503: Argument cannot convert from System.GUID to System.GUID*

Expected behavior

Everything works well after changing to TFM net7.0

Repro steps

Context

AArnott commented 1 year ago

Thanks for using CsWin32 and reporting on your experience. I'm sorry to say that net35 is not supported. We have some code to help it work in some cases, but much of that is contributed by the community. If you'd like to submit a PR to make this particular area better, we'd welcome it if it includes a regression test. We already have test infrastructure in place including for net35.

jnm2 commented 1 year ago

The generated code is invalid C# (Guid expression passed to a Guid* parameter). The bug also afflicts net461 and earlier, but not net462 and later.

Invalid generated code:

```cs // ------------------------------------------------------------------------------ // // This code was generated by a tool. // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // // ------------------------------------------------------------------------------ #pragma warning disable CS1591,CS1573,CS0465,CS0649,CS8019,CS1570,CS1584,CS1658,CS0436,CS8981 using global::System; using global::System.Diagnostics; using global::System.Diagnostics.CodeAnalysis; using global::System.Runtime.CompilerServices; using global::System.Runtime.InteropServices; using winmdroot = global::Windows.Win32; namespace Windows.Win32 { /// /// Contains extern methods from "OLE32.dll". /// [global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Windows.CsWin32", "0.2.188-beta+852e176a3b")] internal static partial class PInvoke { /// internal static unsafe global::Windows.Win32.Foundation.HRESULT CoCreateInstance(in Guid rclsid, object pUnkOuter, global::Windows.Win32.System.Com.CLSCTX dwClsContext, out T ppv) where T : class { global::Windows.Win32.Foundation.HRESULT hr = CoCreateInstance(rclsid, pUnkOuter, dwClsContext, typeof(T).GUID, out object o); ppv = (T)o; return hr; } /// internal static unsafe winmdroot.Foundation.HRESULT CoCreateInstance(in global::System.Guid rclsid, object pUnkOuter, winmdroot.System.Com.CLSCTX dwClsContext, in global::System.Guid riid, out object ppv) { fixed (global::System.Guid* riidLocal = &riid) { fixed (global::System.Guid* rclsidLocal = &rclsid) { winmdroot.Foundation.HRESULT __result = PInvoke.CoCreateInstance(rclsidLocal, pUnkOuter, dwClsContext, riidLocal, out ppv); return __result; } } } /// Creates a single uninitialized object of the class associated with a specified CLSID. /// The CLSID associated with the data and code that will be used to create the object. /// If NULL, indicates that the object is not being created as part of an aggregate. If non-NULL, pointer to the aggregate object's IUnknown interface (the controlling IUnknown). /// Context in which the code that manages the newly created object will run. The values are taken from the enumeration CLSCTX. /// A reference to the identifier of the interface to be used to communicate with the object. /// Address of pointer variable that receives the interface pointer requested in riid. Upon successful return, *ppv contains the requested interface pointer. Upon failure, *ppv contains NULL. /// /// This function can return the following values. /// This doc was truncated. /// /// /// Learn more about this API from docs.microsoft.com. /// [DllImport("OLE32.dll", ExactSpelling = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern unsafe winmdroot.Foundation.HRESULT CoCreateInstance(global::System.Guid* rclsid, [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, winmdroot.System.Com.CLSCTX dwClsContext, global::System.Guid* riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppv); } } ```
AArnott commented 1 year ago

The C# syntax is valid, but the code isn't coherent, which doesn't surprise me because we barely test net35 code generation, whereas we thoroughly test code generation for net472, netstandard2.0 and net6.0.

https://github.com/microsoft/CsWin32/blob/27f494b7eaa2101c3073c09fb74d6269b5acf868/test/Microsoft.Windows.CsWin32.Tests/FullGenerationTests.cs#L23-L27

https://github.com/microsoft/CsWin32/blob/27f494b7eaa2101c3073c09fb74d6269b5acf868/test/Microsoft.Windows.CsWin32.Tests/GeneratorTestBase.cs#L50-L56

The fact that this works on net462 is on target. net461 is not a goal, as Microsoft doesn't support that version as a runtime any more.

Microsoft does support net35 as a runtime, but CsWin32 does not, except as I mentioned earlier. The cost/benefit ratio for adding net35 as a fully supported target simply isn't a good bargain.

jnm2 commented 1 year ago

Yes, no one should be targeting net40-net461, but it could be useful in tracking down what makes the difference. (E.g. the presence of an API from the System.Memory package?)

peaceshi commented 1 year ago

890