c# - 在 C# 中调试 DLLImport

标签 c# .net mysql pinvoke dllimport

我一直在尝试获取 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/

相关文章:

c# - 使用 EF 数据库优先应用程序部署 ASP.NET Web API

c# - 如何创建不比较两个单独继承类的抽象基类 IComparable?

php - MySql - 按姓名和合作伙伴对人员进行排序

php - 如何选择具有条件记录 id + 1 的特定记录

mysql - EclipseLink (JPA 2.1) 查询未返回明显应该返回的结果

c# - 将 C# 库转换为 MSTest 项目

c# - .NET 4 中的 StackOverflowException

c# - 响应序列化问题

c# - 对于 i = 0,为什么 (i += i++) 等于 0?

c# - 我可以检测对象是否调用了 GC.SuppressFinalize 吗?