menu

Questions & Answers

mysql_init(NULL) crashes my C program using a wamp64 server

I'm working on a school project in C language, during which I need to make a connection with a local database. For this, I'm using a wamp64 local server. My problem, however, is entirely outside of that to my comprehension. The compiler SEEMS to detect and properly import the necessary library, this being I believe the MYSQL C API, however it crashes once I use the mysql = mysql_init(NULL) command, which is a bit problematic. Everytime the result is the same:

Process finished with exit code -1073741515 (0xC0000135) Here is the code of the file main.c, where I'm running my test commands: (Yes, I am using GTK as well if that causes an issue)


#include <stdio.h>
#include <winsock.h>
#include <MYSQL/mysql.h>
#include <gtk-4.0/gtk/gtk.h>
#include <stdlib.h>
#include <string.h>

#include "createMenu.h"
#include "databaseMenu.h"

static void activate(GtkApplication* app, gpointer user_data);
int main (int argc, char **argv);

static void activate (GtkApplication* app, gpointer user_data)
{
    GtkWindow *window;

    window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), "Database C Editor");
    gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);

    GtkWidget* content;
    content = prepareDataBaseScreen();

    gtk_window_set_child(window, content);


    gtk_widget_show (window);
}

int main (int argc, char **argv)
{
    GtkApplication *app;
    int status;

    MYSQL * mysql;
    mysql = mysql_init(NULL); // Erreur ici
    mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "option");

    if (mysql_real_connect(mysql, "localhost", "root", "", NULL, 0, NULL, 0))
    {
        g_print("Success");
    } else {
        g_print("Failure");
    }
    mysql_close(mysql);

    app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
    status = g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);

    return status;
}

I doubt it comes from anywhere except the main function but just in case I put the entire code in there. Now since that might be linked as well, I want to give you the CMakeLists.txt of my project, where I'm importing both libraries I'm using:

cmake_minimum_required(VERSION 3.18)
project(ProjectCESGI LANGUAGES C)

find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK4 REQUIRED IMPORTED_TARGET gtk4)
include_directories("C:/wamp64/bin/mariadb/mariadb10.6.5/include")
link_directories("C:/wamp64/bin/mariadb/mariadb10.6.5/lib")
link_libraries(libmariadb)

add_executable(ProjectCESGI main.c createMenu.h databaseMenu.h columnWidget.h)
target_link_libraries(ProjectCESGI PRIVATE PkgConfig::GTK4 libmariadb)

Finally, my teacher gave us a sample code in which he uses the same library. Alarmingly, this one actually works on my machine. Here's his main.c:

#include <stdio.h>
#include <winsock.h>
#include <MYSQL/mysql.h>
#include <stdlib.h>

int main() {

    MYSQL *mysql;
    mysql = mysql_init(NULL);
    mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "option");

    if (mysql_real_connect(mysql, "localhost", "root", "", NULL, 0, NULL, 0)) {
        printf("MySQL client version: %s\n", mysql_get_client_info());
        mysql_query(mysql, "CREATE DATABASE IF NOT EXISTS dbexample");
        mysql_query(mysql, "USE dbexample");
        mysql_query(mysql, "DROP TABLE IF EXISTS students");
        mysql_query(mysql, "CREATE TABLE students(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), class INT)");
        mysql_query(mysql, "INSERT INTO students VALUES(1,'Mathieu',1)");
        mysql_query(mysql, "INSERT INTO students VALUES(2,'Ryan',12)");
        mysql_close(mysql);
    } else {
        printf("Erreur connexion à la BDD!");
    }
    return EXIT_SUCCESS;
}

If you need anything else, please do tell as I need this problem fixed and I've gotten kinda desperate. Thank you for any answer!

EDIT: Someone requested that I put my teacher's CMakeLists.txt as well so here it is:

cmake_minimum_required(VERSION 3.12)
project(test C)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "-Wall")
include_directories("C:/wamp64/bin/mariadb/mariadb10.6.5/include")
link_directories("C:/wamp64/bin/mariadb/mariadb10.6.5/lib")
link_libraries(libmariadb)
add_executable(test main.c)
target_link_libraries(test libmariadb)

# les fichiers libmariadb.dll et libmariadb.lib sont copiés de
# C:\wamp64\bin\mariadb\mariadb10.4.10\lib
# dans le dossier de compilation
# C:\Users\fredguy\CLionProjects\test\cmake-build-debug\

Do notice that I've changed every mention of "mariadb10.4.10" to "mariadb10.6.5" as that's the version I have installed. Keep in mind it's working just fine on his exemple project.

EDIT2: After someone pointed out that this specific error code meant DLL files were missing, I investigated in my debug folder and indeed it seems the program doesn't copy the libmariadb.dll an libmysql.dll files, while of course it works on the teacher's exemple. Any idea why that is?

-Tracking down the bug using a few methods to see where exactly the program crashes, letting me know that mysql_init seems to be indeed the problem.

-Googling the problem; Only forums are on a MYSQL forum which seems to have never been answered

-Replacing 'NULL' with a few things, like '0' or 'mysql'.

-Making another project just to import it on that other project (Couldn't work either)

Comments:
2023-01-11 09:12:33
you're sure it crashes on mysql = mysql_init(NULL) (and not g_print for instance) in your code but not your teacher's? The only difference until then is GtkApplication *app;, what if you add that to your teacher's? Does your teacher use a different CMake file? If you have no idea where the problem is, I think the only thing you can do is gradually morph your teacher's working code into yours and see what causes it to break. I'm guessing it's something to do with GTK if your teacher's code works.
2023-01-11 09:12:33
I've already tried in another project where I literally copy-pasted the teacher's code and it somehow didn't work. I just doesn't work outside of his project file for some reason. We don't exactly have the same CMakeLists file, but after a little testing I really don't think that's where the problem lies unfortunately...
2023-01-11 09:12:33
Error code 0xC0000135 is defined as STATUS_DLL_NOT_FOUND in WinNT.h. Are you missing a dll file in your executable directory? Perhaps GTK?
2023-01-11 09:12:33
@SafelyFast GTK works just fine but that IS a good lead. I'll see what I can find concerning DLLs
Answers(0) :