我一直在尝试获取 MySQL Embedded Library在过去的 12 个小时左右的时间里,我在 C# 应用程序中工作,并且已经停滞了很长一段时间。当我调用 mysql_server_init()
...
Unhandled Exception: System.AcessViolationException: Attempted to read or write protected memory.
C++ 方法采用一个 int
和两个 char**
作为参数,但有人告诉我一个空终止的 string[]
数组会在 C# 中就足够了。由于这是您应该调用的第一个方法,因此我对这个问题有些不知所措。
这是我的代码...
public class MySQLServer
{
[DllImport("libmysqld.dll")]
static extern int mysql_server_init(int argc, string[] argv, string[] groups);
[DllImport("libmysqld.dll")]
static extern void mysql_server_end();
public static bool Start()
{
string[] argv = new string[3];
argv[0] = "mysql_test";
argv[1] = "--datadir=C:/MYSQLTEST";
argv[2] = null;
string[] groups = new string[3];
groups[0] = "libmysqd_server";
groups[1] = "libmysqd_client";
groups[2] = null;
int res;
if ((res = mysql_server_init(3, argv, groups)) == 1)
{
Console.WriteLine("MySQL Library Init Failed with error code: %d", res);
return false;
}
Console.WriteLine("MySQL Library Started Successfully!");
return true;
}
}
最佳答案
如果正在读取字符串,您应该能够作为字符串数组传递。
如果要写入它们,则必须将参数声明为 IntPtrs
,为它们分配内存,然后自己编码数据(此外,您还需要取消分配内存,因为好吧)。
如果你这样调用会发生什么 (argc := 2):
if ((res = mysql_server_init(2, argv, groups)) == 1)
{
Console.WriteLine("MySQL Library Init Failed with error code: %d", res);
return false;
}
关于c# - 在 C# 中调试 DLLImport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7822686/