简体   繁体   English

在C#pt中调试DLLImport。 2

[英]Debugging DLLImport in C# pt. 2

I am using the MySQL Embedded Library and using P/Invoke to call the necessary functions to start the server. 我正在使用MySQL嵌入式库并使用P / Invoke调用必要的函数来启动服务器。 We resolved some issues regarding it in this topic, however another issue has presented itself. 我们在主题中解决了有关它的一些问题,但另一个问题已经出现。

The mysql_server_init() function returns 0 if success, 1 if error. mysql_server_init()函数如果成功则返回0,如果错误则返回1。 Unfortunately, in my code when it returns 1, and I use Marshal.GetLastWin32Error() the error code is 0. I am assuming that it is not picking up on the error being generated by mysql_server_init(), but I am at a loss as to how to find out where the problem is. 不幸的是,在我的代码中,当它返回1时,我使用Marshal.GetLastWin32Error(),错误代码为0.我假设它没有找到mysql_server_init()生成的错误,但我感到茫然如何找出问题所在。

Here is the relevant code block... 这是相关的代码块......

    [DllImportAttribute("libmysqld.dll", SetLastError = true)]
    static extern int mysql_server_init(int argc, string[] argv, string[] groups);

    static string[] server_options = new string[2];
    static string[] server_groups = new string[3];

    public static bool Start()
    {
        server_options[0] = "mysql_test"; // not used?
        server_options[1] = "--defaults-file=./my.ini";

        server_groups[0] = "client";
        server_groups[1] = "server";
        server_groups[2] = "\0";

        if (mysql_server_init(2, server_options, server_groups) != 0)
        {
            int lastError = Marshal.GetLastWin32Error();
            Console.WriteLine("MySQL Library Init Failed with error code: " + lastError);
            return false;
        }

        Console.WriteLine("MySQL Library Started Successfully!");
        return true;
    }

The mysql_server_init function does not report errors via the Win32 error reporting mechanism SetLastError() and GetLastError() . mysql_server_init函数不会通过Win32错误报告机制SetLastError()GetLastError()报告错误。 So, you can"t use Marshal.GetLastWin32Error() to obtain the last error. The embedded mysql database reports errors via the functions mysql_error() and mysql_errno() . However, those functions seem to only report errors AFTER a successful call to mysql_server_init() . 所以,你不能使用Marshal.GetLastWin32Error()来获取最后一个错误。嵌入式mysql数据库通过函数mysql_error()mysql_errno()报告错误。但是,这些函数似乎只在成功调用mysql_server_init() 报告错误mysql_server_init()

I think the problem of your code lies in the way you terminate your server_groups array. 我认为您的代码问题在于终止server_groups数组的方式。 You should use "null" instead of "\\0" to "terminate" your array: 您应该使用“null”而不是“\\ 0”来“终止”您的数组:

public static bool Start() 
{ 
    server_options[0] = "mysql_test"; // not used? 
    server_options[1] = "--defaults-file=./my.ini"; 

    server_groups[0] = "client"; 
    server_groups[1] = "server"; 
    server_groups[2] = null; 

    if (mysql_server_init(2, server_options, server_groups) != 0) 
    { 
        int lastError = Marshal.GetLastWin32Error(); 
        Console.WriteLine("MySQL Library Init Failed with error code: " + lastError); 
        return false; 
    } 
}

Errors regarding your configuration should be printed to the console window by the mysql_server_init() function. 有关配置的错误应通过mysql_server_init()函数打印到控制台窗口。

Hope, this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM