gpt4 book ai didi

返回 'Attempted to read or write protected memory' 错误的 C# 等效于 VB DLL 函数声明

转载 作者:行者123 更新时间:2023-11-28 06:12:30 25 4
gpt4 key购买 nike

如果我的 VBA 代码是:

Private Declare Function pca Lib "mc.dll" Alias "_pca@40" (ByVal n As Long, ByVal nsim As Long, ByVal nbucket As Long, ByVal percent As Double, ByRef cor As Double, ByRef result As Double, ByRef evalue As Double, ByRef evector As Double, ByRef np As Long) As Long

我的 C# 代码会是(见下文)吗?

[DllImport("mc.dll", EntryPoint = "_pca@40")]
private static extern long pca(long n, long maxsize, long nbucket, double percent, double[] cor, double[] result, double[] d1, double[] v1, long np);

在 Visual Studio 中,我尝试在 mc.dll 中使用 C++ 函数,但不断收到“试图读取或写入 protected 内存”错误。

有谁知道我做错了什么? VBA 程序能够正常访问 DLL,但我的 C# 不行。

我按照下面链接中先前提出的问题的格式编写了代码,但一直出现错误。 C# equivalent of VB DLL function declaration (InternetSetOption)?

最佳答案

您的 VB 定义包含您未在 C# P/Invoke 定义中通过引用传递的 ByRef 值。此外,您似乎在 VB6 中传递 double 组而不是 ByRef double 。最后,VB6 long 是 32 位类型,与 C# int 相同。

所以定义应该是:

[DllImport("mc.dll", EntryPoint = "_pca@40")]
private static extern int pca(int n, int maxsize, int nbucket, double percent, ref double cor, ref double result, ref double d1, ref double v1, ref int np);

关于返回 'Attempted to read or write protected memory' 错误的 C# 等效于 VB DLL 函数声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30984675/

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