Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6699703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:43:37+00:00 2026-05-26T06:43:37+00:00

I have been attempting to get the MySQL Embedded Library working in my C#

  • 0

I have been attempting to get the MySQL Embedded Library working in my C# application for the past 12 hours or so, and have been stuck for quite some time. I am getting the following error when I call mysql_server_init()…

Unhandled Exception: System.AcessViolationException: Attempted to read
or write protected memory.

The C++ method takes an int and two char** as parameters, but I was told a null terminated string[] array would suffice in C#. As this is the first method that you’re supposed to call, I am somewhat at a loss as to this issue.

Here’s my code…

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:43:38+00:00Added an answer on May 26, 2026 at 6:43 am

    It seems to me that you have a tiny error, causing mysql_server_init to read beyond the array boundaries. Replace 3 with 2 and remove the third array entry. The default marshaler will do the rest.

    public static bool Start()
    {
        string[] argv = new string[3];
        argv[0] = "mysql_test";
        argv[1] = "--datadir=C:/MYSQLTEST";
    
        string[] groups = new string[3];
        groups[0] = "libmysqd_server";
        groups[1] = "libmysqd_client";
    
        int res;
    
        if ((res = mysql_server_init(2, argv, groups)) == 1)
        {
            Console.WriteLine("MySQL Library Init Failed with error code: %d", res);
            return false;
        }
    }
    

    Update

    From this thread, you should not use mysql_server_init at all, but mysql_library_init instead:

    1. Both ‘argv’ and ‘groups’ are stored by the library and used later. This is a serious problem, because if you use P/Invoke marshaling, the
      pointers that .NET passes to the function will become invalid as soon
      as the function returns. When the library later tries to refer to argv
      or groups, it’ll find nothing but garbage there, causing unexpected
      behavior or a crash.

    If you’re using MySQL 5.0.3 or later, try using mysql_library_init
    instead – it makes an internal copy of argv and groups, so you can
    keep using P/Invoke marshaling with no trouble (as long as you add a
    null entry to the end of groups). Otherwise, you’ll have to allocate
    the memory yourself for the arrays and each string, to make sure the
    pointers stay valid as long as your program is running.

    So, if you can switch to using mysql_library_init, I’d suggest doing so. By-hand marshaling is not a trivial undertaking (but not too hard either).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been attempting to get log4net logging in my asp.net web application with
I have been attempting to write some routines to read RSS and ATOM feeds
I have been attempting to compile/run a sample WCF application from Juval Lowy's website
Morning Everyone, I've been attempting to write an application that does some GETs from
For the past few days I have been attempting to write my own shell
I have been trying to get a 2-way ssl/https proxy working with Camel. I
I have been attempting to have a object that I can use across multiple
I have been attempting to enhance my GUI system written in Java to use
For the last few days I have been attempting to find a method to
We have been using CruiseControl for quite a while with NUnit and NAnt. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.