- 
                Notifications
    You must be signed in to change notification settings 
- Fork 114
Description
Description
In full disclosure, I'm a COM newb. Feel free to slap me and tell me i'm doing it completely wrong.
I'm using CsWin32 to read the target of an .lnk file. Some searching lead me to this solution, which does work for me. I thought I'd supplement the hand-written bits with types generated from CsWin32. Everything works except the definition of ShellLink, which appears to be a struct instead of a class and lacking the necessary ComImport attributes to allow you to cast it to an IPersistFile and an IShellLInkW.
Actual behavior
The generated code for Windows.Win32.ShellLink.g.cs:
// ------------------------------------------------------------------------------
// <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
namespace Windows.Win32
{
	using global::System;
	using global::System.Diagnostics;
	using global::System.Runtime.CompilerServices;
	using global::System.Runtime.InteropServices;
	using global::System.Runtime.Versioning;
	using winmdroot = global::Windows.Win32;
	namespace UI.Shell
	{
		[Guid("00021401-0000-0000-C000-000000000046")]
		internal partial struct ShellLink
		{
		}
	}
}Expected behavior
Again, I'm bashing rocks together from StackOverflow, but the signature there includes a ComImport attribute that I believe is necessary and the type is also a class instead of a struct. If I omit ShellLink from NativeMethods.txt and use this definition instead, things do work.
    [
        ComImport(),
        Guid("00021401-0000-0000-C000-000000000046")
    ]
    internal class ShellLink
    {
    }Repro steps
- NativeMethods.txtcontent:
IPersistFile
IShellLinkW
MAX_PATH
ShellLink
STGM_READ
- 
NativeMethods.jsoncontent (if present):
 Not present.
- 
Attempt to resolve the target of a .lnk:
    public static unsafe string ResolveShortcut(string shortcutFilePath)
    {
        ShellLink shellLink = new ShellLink();
        IPersistFile persistFile = (IPersistFile)shellLink;
        fixed (char* shortcutFilePathPcwstr = shortcutFilePath)
        {
            persistFile.Load(shortcutFilePathPcwstr, PInvoke.STGM_READ);
        }
        Span<char> szShortcutTargetPath = stackalloc char[(int)PInvoke.MAX_PATH];
        fixed (char* cShortcutTargetPath = szShortcutTargetPath)
        {
            IShellLinkW shellLinkW = (IShellLinkW)shellLink;
            WIN32_FIND_DATAW data;
            shellLinkW.GetPath(cShortcutTargetPath, (int)PInvoke.MAX_PATH, &data, 0);
            return new string(cShortcutTargetPath);
        }
    }Context
- CsWin32 version: 0.1.619-beta
- Win32Metadata version (if explicitly set by project): Not explicitly set
- Target Framework: net6.0
- LangVersion(if explicitly set by project): 10.0