“预期类型” PowerShell附加型代码块中的错误。 dllimport,Windows已知文件夹
我尝试.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的是
add-type
在 Windows PowerShell 中不支持,特别是:new(...),无类型名称的构造函数调用 - 替换为
新GUID(...)
default
to to to指示类型的默认值 - 替换为intptr.zero
。字符串?
,一个无效的注释 - 删除?
因此,使用以下内容:
注意:
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 withnew Guid(...)
default
to indicate a type's default value - replace withIntPtr.Zero
.string?
, a nullable annotation - remove the?
Therefore, use the following:
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.