Jump to content
Sign in to follow this  
tsvety

Dllcall problem

Recommended Posts

tsvety

Hi guys!

Short description of my problem.

1) Created a DLL. It is supposed to make a MYSQL connection and to get some values.

They are just displayed in MsgBox-es. Database name: "menagerie". Table name: "pet".

Here's the code:

#include <stdio.h>
#include <Windows.h>
#include <mysql.h> 
#include <atlstr.h>
#include <string.h>


extern "C"
{

__declspec(dllexport) void DisplayHelloFromDLL(char SERVER_NAME[80], char DB_USER[80], char DB_USERPASS[80], char DB_NAME[80])
{       
    MYSQL mysql;
    MYSQL_RES *res;
    MYSQL_ROW row;
    MYSQL *connection;

    mysql_init(&mysql);

    connection = mysql_real_connect(&mysql, SERVER_NAME, DB_USER, DB_USERPASS, DB_NAME, 0, 0, 0);

    if (connection == NULL)
    {
        int msgboxID = MessageBox(NULL, (LPCWSTR)L"Error connecting MySQL!",
            (LPCWSTR)L"MySQL Test", MB_ICONINFORMATION | MB_OKCANCEL | MB_DEFBUTTON2);          
    }
    else
    {
        int msgboxID = MessageBox(NULL, (LPCWSTR)L"Connection successful!",
            (LPCWSTR)L"MySQL Test", MB_ICONINFORMATION | MB_OKCANCEL | MB_DEFBUTTON2);

        if (!(mysql_query(connection, "SELECT name FROM menagerie.pet p;")))
        {
            
            res = mysql_use_result(connection);

            while ((row = mysql_fetch_row(res)))
            {
                int msgboxID = MessageBox(NULL, (CString)(row[0]),
                    (LPCWSTR)L"MySQL Test", MB_ICONINFORMATION | MB_OKCANCEL | MB_DEFBUTTON2);
            }
            mysql_free_result(res);
        }

        else
        {                
            int msgboxID = MessageBox(NULL, (CString)(mysql_error(&mysql)),
                (LPCWSTR)L"MySQL Test", MB_ICONINFORMATION | MB_OKCANCEL | MB_DEFBUTTON2);
        }

    }
    

    mysql_close(&mysql);

    int msgboxID3 = MessageBox(NULL, (LPCWSTR)L"Connection closed",
        (LPCWSTR)L"MySQL Test", MB_ICONINFORMATION | MB_OKCANCEL | MB_DEFBUTTON2);

}

}

2) And this is the Autoit program:

#include <GuiConstantsEx.au3>

; GUI
GuiCreate("Sample GUI", 290, 250)

; Button
$buton = GUICtrlCreateButton("Connect to DB!", 30, 75)

GUISetState()

Func test()     
   $resultDll = DllCall("MyDll.dll", "int", "DisplayHelloFromDLL", "str", "localhost", "str", "user", "str", "passwd", "str", "menagerie")
            
    If @error Then
        SetError(1)             
        MsgBox(0, "", "Error opening dll")
    Else        
        MsgBox(0, "", "Success!")
    EndIf
EndFunc


; WHILE PROGRAM IS RUNNING
While 1             
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $buton
                test()
        EndSelect
    WEnd

THE PROBLEM:

It works fine, it displays all the values, but at the end, after showing "Connection closed", I get

(Visual Studio Just-In-Time Debugger message)

An unhandled win32 exception occurred in Autoit3.exe [3904].

I'm asked to choose between debuggers. After selecting one and clicking on Yes,

I get:

Unhandled exception at 0x00373034 in Autoit3.exe. Access violation reading location 0x00373034.

So for some reasons I can not pass to the next line after Dllcall:

If @error Then
        SetError(1)             
        MsgBox(0, "", "Error opening dll")
etc.

Do you have any idea what's wrong?

Share this post


Link to post
Share on other sites
oMBRa

the return value of your function is ''void'' but you are calling it expecting an int as return type

$resultDll = DllCall("MyDll.dll", "int", "DisplayHelloFromDLL", "str", "localhost", "str", "user", "str", "passwd", "str", "menagerie")

change it with ''none''

Share this post


Link to post
Share on other sites
Richard Robertson

May I ask what is with "char SERVER_NAME[80]" as your parameter declarations? Rather nonsense if you ask me. Most interop languages (including AutoIt) will not know to stop at 80, and also, when you are declaring it 80, you expect there to be 80 data positions which there very well won't be in shorter strings.

Use "char *SERVER_NAME" instead, and for the other parameters.

Share this post


Link to post
Share on other sites
tsvety

Hello again! :D

Thanks for the answers!

Changed char <parameters>[80] to char* <parameters>.

And "int" to "none".

However still getting the same exception... :D

Share this post


Link to post
Share on other sites
Richard Robertson

Have you tried calling your dll from a C++ program and running it in debug mode? That will show you if the error is in your AutoIt code or your C++ code.

Share this post


Link to post
Share on other sites
tsvety

Have you tried calling your dll from a C++ program and running it in debug mode? That will show you if the error is in your AutoIt code or your C++ code.

Yes, I did. And there were no errors. I created another very simple DLL.

And something really strange has happened!

The following code works:

(C++)
     __declspec(dllexport) int DLLfunction()
      {                     
        return 5;
      }


(Autoit3)
     $res = DllCall("MyDLL.dll", "int", "DLLfunction")
     MsgBox(0, "", $res[0])

And this one does not :D

(C++)
    __declspec(dllexport) int DLLfunction(int a)
      {                     
        return a;
      }


(Autoit3)
     $res = DllCall("MyDLL.dll", "int", "DLLfunction", "int", 5)

I'm getting again "An unhandled win32 exception occurred in Autoit3.exe [6104]."

So it's like autoit3 does not want to accept any parameters.

Share this post


Link to post
Share on other sites
monoceres

It's because you're using the wrong calling convention.

Add ":cdecl" to the return type in the autoit dllcall.


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
tsvety

It's because you're using the wrong calling convention.

Add ":cdecl" to the return type in the autoit dllcall.

Problem solved!

Many thanks! :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.