“预期类型” PowerShell附加型代码块中的错误。 dllimport,Windows已知文件夹

发布于 2025-01-26 16:30:17 字数 1586 浏览 5 评论 0原文

我尝试.ps1

Add-Type -TypeDefinition @'
using System;       // IntPtr
using System.Runtime;       // guid Type
using System.Runtime.InteropServices;       // DllImport

public class Win32 {
    public static class KnownFolderId {
        public static readonly Guid Desktop = new(0xB4BFCC3A, 0xDB2C, 0x424C, 0xB0, 0x29, 0x7F, 0xE9, 0x9A, 0x87, 0xC6, 0x41);
    }

    [DllImport("shell32.dll")] static extern int SHGetKnownFolderPath(
        [MarshalAs(UnmanagedType.LPStruct)] Guid rfid,
        uint dwFlags,
        IntPtr hToken,
        out IntPtr ppszPath
    );
                
    public static string? GetKnownFolderPath() {
        IntPtr ppszPath = default;
        try {
            int hr = SHGetKnownFolderPath(Win32.KnownFolderId.Desktop, 0, IntPtr.Zero, out ppszPath);
            Marshal.ThrowExceptionForHR(hr); // alternatively, check success with hr >= 0
            return Marshal.PtrToStringUni(ppszPath);
        }
        finally {
            Marshal.FreeCoTaskMem(ppszPath);
        }
    }

}
'@
[Win32]::GetKnownFolderPath() | write-host

read-host 'end'

,当然要获取此

Type expected
<<..>>\h1c5k4r2.0.cs(5) : public static class KnownFolderId {
<<..>>\h1c5k4r2.0.cs(6) : >>>     public static readonly Guid Desktop = new(0xB4BFCC3A,<<..>>

1234567890123456789012345678901234567890123456789012345678978901234901234567890122452452452452452456245623456234562345623456234562345623456234.2345623456234.234562345623456.23456234.23456234.23454545 . 67890

Im trying .ps1

Add-Type -TypeDefinition @'
using System;       // IntPtr
using System.Runtime;       // guid Type
using System.Runtime.InteropServices;       // DllImport

public class Win32 {
    public static class KnownFolderId {
        public static readonly Guid Desktop = new(0xB4BFCC3A, 0xDB2C, 0x424C, 0xB0, 0x29, 0x7F, 0xE9, 0x9A, 0x87, 0xC6, 0x41);
    }

    [DllImport("shell32.dll")] static extern int SHGetKnownFolderPath(
        [MarshalAs(UnmanagedType.LPStruct)] Guid rfid,
        uint dwFlags,
        IntPtr hToken,
        out IntPtr ppszPath
    );
                
    public static string? GetKnownFolderPath() {
        IntPtr ppszPath = default;
        try {
            int hr = SHGetKnownFolderPath(Win32.KnownFolderId.Desktop, 0, IntPtr.Zero, out ppszPath);
            Marshal.ThrowExceptionForHR(hr); // alternatively, check success with hr >= 0
            return Marshal.PtrToStringUni(ppszPath);
        }
        finally {
            Marshal.FreeCoTaskMem(ppszPath);
        }
    }

}
'@
[Win32]::GetKnownFolderPath() | write-host

read-host 'end'

And of course getting this

Type expected
<<..>>\h1c5k4r2.0.cs(5) : public static class KnownFolderId {
<<..>>\h1c5k4r2.0.cs(6) : >>>     public static readonly Guid Desktop = new(0xB4BFCC3A,<<..>>

123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2025-02-02 16:30:17

您使用的是 add-type Windows PowerShell 中不支持,特别是:

  • new(...),无类型名称的构造函数调用 - 替换为新GUID(...)


  • default to to to指示类型的默认值 - 替换为intptr.zero

  • 字符串?,一个无效的注释 - 删除

因此,使用以下内容:

Add-Type -TypeDefinition @'
using System;       // IntPtr
using System.Runtime;       // guid Type
using System.Runtime.InteropServices;       // DllImport

public class Win32 {
    public static class KnownFolderId {
        public static readonly Guid Desktop = new Guid(0xB4BFCC3A, 0xDB2C, 0x424C, 0xB0, 0x29, 0x7F, 0xE9, 0x9A, 0x87, 0xC6, 0x41);
    }

    [DllImport("shell32.dll")] static extern int SHGetKnownFolderPath(
        [MarshalAs(UnmanagedType.LPStruct)] Guid rfid,
        uint dwFlags,
        IntPtr hToken,
        out IntPtr ppszPath
    );
                
    public static string GetKnownFolderPath() {
        IntPtr ppszPath = IntPtr.Zero;
        try {
            int hr = SHGetKnownFolderPath(Win32.KnownFolderId.Desktop, 0, IntPtr.Zero, out ppszPath);
            Marshal.ThrowExceptionForHR(hr); // alternatively, check success with hr >= 0
            return Marshal.PtrToStringUni(ppszPath);
        }
        finally {
            Marshal.FreeCoTaskMem(ppszPath);
        }
    }

}
'@

[Win32]::GetKnownFolderPath()

注意:

  • in powershell(core)7+add-type 确实支持这些语言功能。


  • 在那里,唯一需要的更改是将#nullable enable放在源代码的第一行,以启用无效的注释。

You're using C# language features that the compiler used by Add-Type in Windows PowerShell doesn't support, specifically:

  • new(...), a constructor call without a type name - replace with new Guid(...)

  • default to indicate a type's default value - replace with IntPtr.Zero.

  • string?, a nullable annotation - remove the ?

Therefore, use the following:

Add-Type -TypeDefinition @'
using System;       // IntPtr
using System.Runtime;       // guid Type
using System.Runtime.InteropServices;       // DllImport

public class Win32 {
    public static class KnownFolderId {
        public static readonly Guid Desktop = new Guid(0xB4BFCC3A, 0xDB2C, 0x424C, 0xB0, 0x29, 0x7F, 0xE9, 0x9A, 0x87, 0xC6, 0x41);
    }

    [DllImport("shell32.dll")] static extern int SHGetKnownFolderPath(
        [MarshalAs(UnmanagedType.LPStruct)] Guid rfid,
        uint dwFlags,
        IntPtr hToken,
        out IntPtr ppszPath
    );
                
    public static string GetKnownFolderPath() {
        IntPtr ppszPath = IntPtr.Zero;
        try {
            int hr = SHGetKnownFolderPath(Win32.KnownFolderId.Desktop, 0, IntPtr.Zero, out ppszPath);
            Marshal.ThrowExceptionForHR(hr); // alternatively, check success with hr >= 0
            return Marshal.PtrToStringUni(ppszPath);
        }
        finally {
            Marshal.FreeCoTaskMem(ppszPath);
        }
    }

}
'@

[Win32]::GetKnownFolderPath()

Note:

  • In PowerShell (Core) 7+, Add-Type does support these language features.

  • There, the only change that is needed is to place #nullable enable before the first line of source code, so as to enable nullable annotations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文