gpt4 book ai didi

c# - 我应该在调用 GetSystemWow64Directory 之前固定我的阵列吗

转载 作者:行者123 更新时间:2023-11-30 21:00:24 28 4
gpt4 key购买 nike

我正在尝试使用 GetSystemWow64Direcory我的应用程序中的方法。来自 PInvoke.net 的样本对我来说似乎不正确。在传递给非托管调用之前,我不需要固定数组吗?

[DllImport("Kernel32.dll")]
public static extern int GetSystemWow64Directory([In, Out] char[] lpBuffer
[MarshalAs(UnmanagedType.U4)] uint size);

char[] path = new char[256];

int result = GetSystemWow64Directory(path, (uint)path.Length);
if (result != 0)
MessageBox.Show(new String(path, 0, result));

最佳答案

好吧,您不需要固定,编码器会为您处理。

但是,这是一个糟糕的 p/invoke。我的建议是不要相信你在 pinvoke.net 上找到的东西,质量千差万别。我会在这里使用 StringBuilder

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern int GetSystemWow64Directory(
StringBuilder Buffer,
int Size
);
....
StringBuilder Buffer = new StringBuilder(260);
int retVal = GetSystemWow64Directory(Buffer, Buffer.Capacity);
if (retVal != 0)
MessageBox.Show(Buffer.ToString());

关于c# - 我应该在调用 GetSystemWow64Directory 之前固定我的阵列吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14898138/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com