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

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

Closed
peaceshi opened this issue Mar 17, 2023 · 5 comments · Fixed by #892
Closed

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

peaceshi opened this issue Mar 17, 2023 · 5 comments · Fixed by #892
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@peaceshi
Copy link

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

  • CsWin32 version: 0.2.188-beta
  • Target Framework: net35

Images

image

@peaceshi peaceshi added the bug Something isn't working label Mar 17, 2023
@AArnott
Copy link
Member

AArnott commented Mar 17, 2023

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.

@AArnott AArnott added the help wanted Extra attention is needed label Mar 17, 2023
@jnm2
Copy link
Contributor

jnm2 commented Mar 17, 2023

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:

// ------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------

#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
{

	/// <content>
	/// Contains extern methods from "OLE32.dll".
	/// </content>
	[global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Windows.CsWin32", "0.2.188-beta+852e176a3b")]
	internal static partial class PInvoke
	{
		/// <inheritdoc cref="CoCreateInstance(Guid*, object, global::Windows.Win32.System.Com.CLSCTX, Guid*, out object)"/>
		internal static unsafe global::Windows.Win32.Foundation.HRESULT CoCreateInstance<T>(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;
		}

		/// <inheritdoc cref="CoCreateInstance(global::System.Guid*, object, winmdroot.System.Com.CLSCTX, global::System.Guid*, out object)"/>
		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;
				}
			}
		}

		/// <summary>Creates a single uninitialized object of the class associated with a specified CLSID.</summary>
		/// <param name="rclsid">The CLSID associated with the data and code that will be used to create the object.</param>
		/// <param name="pUnkOuter">If <b>NULL</b>, indicates that the object is not being created as part of an aggregate. If non-<b>NULL</b>, pointer to the aggregate object's <a href="https://docs.microsoft.com/windows/desktop/api/unknwn/nn-unknwn-iunknown">IUnknown</a> interface (the controlling <b>IUnknown</b>).</param>
		/// <param name="dwClsContext">Context in which the code that manages the newly created object will run. The values are taken from the enumeration <a href="https://docs.microsoft.com/windows/desktop/api/wtypesbase/ne-wtypesbase-clsctx">CLSCTX</a>.</param>
		/// <param name="riid">A reference to the identifier of the interface to be used to communicate with the object.</param>
		/// <param name="ppv">Address of pointer variable that receives the interface pointer requested in <i>riid</i>. Upon successful return, *<i>ppv</i> contains the requested interface pointer. Upon failure, *<i>ppv</i> contains <b>NULL</b>.</param>
		/// <returns>
		/// <para>This function can return the following values. </para>
		/// <para>This doc was truncated.</para>
		/// </returns>
		/// <remarks>
		/// <para><see href="https://docs.microsoft.com/windows/win32/api//combaseapi/nf-combaseapi-cocreateinstance">Learn more about this API from docs.microsoft.com</see>.</para>
		/// </remarks>
		[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
Copy link
Member

AArnott commented Mar 17, 2023

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.

public void Everything(
MarshalingOptions marshaling,
bool useIntPtrForComOutPtr,
[CombinatorialMemberData(nameof(AnyCpuArchitectures))] Platform platform,
[CombinatorialMemberData(nameof(TFMDataNoNetFx35))] string tfm)

public static string[] TFMDataNoNetFx35 =>
new string[]
{
"net472",
"netstandard2.0",
"net6.0",
};

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
Copy link
Contributor

jnm2 commented Mar 18, 2023

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
Copy link
Author

#890

AArnott added a commit that referenced this issue Mar 20, 2023
We were excluding certain friendly overloads on net35 on the basis that `Span<T>` isn't supported there, even when the friendly overloads didn't require that type. With this change, we have fine-tuned the exclusion to only restrict the friendly overloads' parameter changes that would require `Span<T>`.
This fixes #888.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants