Jump to content

Problem calling a DLL


Recommended Posts

Hi im testing to call dll functions from AutoIt. So i made a simple dll with an Add() function. But, the DllCall always returns 0.

AutoIt code:

$AutoGLDLL = DllOpen("C:\Documents and Settings\Emanuel Claesson\My Documents\Visual Studio 2008\Projects\Tobacco3D\Debug\AutoGL.dll")

If $AutoGLDLL == -1 Then
    MsgBox(16, "Error", "AutoGL.dll couldn't be opened. Make sure you have it in the same folder as this file, or use SetDLLPath(path)!")
    Exit
EndIf

$c = DllCall($AutoGLDLL, "int", "Add", "int", 5, "int", 10)

MsgBox(16, "Add(5, 10)", String($c))

DllClose($AutoGLDLL)

The DLL doesnt export symbols (the VC++ option while creating a project). Here is the Add() function:

int Add(int x, int y)
{
    return x+y;
}
Link to comment
Share on other sites

Change

MsgBox(16, "Add(5, 10)", String($c))

To

MsgBox(16, "Add(5, 10)", String($c[0]))

Return code from DllCall is stored in an array, the return code from the dll function itself is stored in element [0]

If i change that i get "Error: Subscript used with a non-Array variable." on that line.

I checked what error was after the call. @error == 3. "Function not found in the DLL file."

Edited by OnTheFly
Link to comment
Share on other sites

I believe you have to export the Symbols for autoit to "see" it. I've never made a DLL file before so I don't know. :s

Edit:

I managed to create a dll file that adds a number together with DevC++:

dllmain.c:

/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
//#include <stdio.h>
//#include <stdlib.h>

DLLIMPORT int Add (int x,int y)
{
          return x+y;
}


BOOL APIENTRY DllMain (HINSTANCE hInst   /* Library instance handle. */ ,
                       DWORD reason     /* Reason this function is being called. */ ,
                       LPVOID reserved   /* Not used. */ )
{
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:
        break;

      case DLL_PROCESS_DETACH:
        break;

      case DLL_THREAD_ATTACH:
        break;

      case DLL_THREAD_DETACH:
        break;
    }

    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
}

dll.h:

#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */


DLLIMPORT int Add (int x,int y);


#endif /* _DLL_H_ */

And you call it with this code:

$a = DllCall("D:\Annat\Blandade_Program\Dev-Cpp\Projects\Project1.dll", "int:cdecl", "Add", "int", 10, "int", 5)
If @error Then Exit(@error)

ConsoleWrite($a[0] & @LF)

Note the :cdecl part in the return type, for some reason the autoit script kept crashing when I tried to call it the 'normal' way, and I just by accident tried adding :cdecl at the return type(as specified by the helpfile), and it started working. :)

Edited by FreeFry
Link to comment
Share on other sites

I believe you have to export the Symbols for autoit to "see" it. I've never made a DLL file before so I don't know. :s

Edit:

I managed to create a dll file that adds a number together with DevC++:

dllmain.c:

/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
//#include <stdio.h>
//#include <stdlib.h>

DLLIMPORT int Add (int x,int y)
{
          return x+y;
}


BOOL APIENTRY DllMain (HINSTANCE hInst   /* Library instance handle. */ ,
                       DWORD reason     /* Reason this function is being called. */ ,
                       LPVOID reserved   /* Not used. */ )
{
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:
        break;

      case DLL_PROCESS_DETACH:
        break;

      case DLL_THREAD_ATTACH:
        break;

      case DLL_THREAD_DETACH:
        break;
    }

    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
}

dll.h:

#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */


DLLIMPORT int Add (int x,int y);


#endif /* _DLL_H_ */

And you call it with this code:

$a = DllCall("D:\Annat\Blandade_Program\Dev-Cpp\Projects\Project1.dll", "int:cdecl", "Add", "int", 10, "int", 5)
If @error Then Exit(@error)

ConsoleWrite($a[0] & @LF)

Note the :cdecl part in the return type, for some reason the autoit script kept crashing when I tried to call it the 'normal' way, and I just by accident tried adding :cdecl at the return type(as specified by the helpfile), and it started working. :)

It's still the same. It can't find the function. Even thoug i have the exact code you have. (Also used Dev-Cpp)
Link to comment
Share on other sites

:)

That's really odd... I had a lot of problems when I was testing "different" ways of creating the DLL(note again that I've never even looked at creating DLL's before. :)).

But when I used the template DLL project, and just added the Add function too it, autoit didn't error out with "function not found", but instead it started crashing, so I figured something must be wrong with the way I was calling it. Therefore I tried changing the return type parameter from "int", to "int:cdecl", and woooop it magically worked. :s

Try using my compiled DLL and see if it works: http://freefry.dynalias.com/files/Project1.dll

Link to comment
Share on other sites

:)

That's really odd... I had a lot of problems when I was testing "different" ways of creating the DLL(note again that I've never even looked at creating DLL's before. :)).

But when I used the template DLL project, and just added the Add function too it, autoit didn't error out with "function not found", but instead it started crashing, so I figured something must be wrong with the way I was calling it. Therefore I tried changing the return type parameter from "int", to "int:cdecl", and woooop it magically worked. :s

Try using my compiled DLL and see if it works: http://freefry.dynalias.com/files/Project1.dll

It found the function in your DLL, but when i tries to show the result using
MsgBox(0, "Result", String($c))
is shows an empy MsgBox. :/
Link to comment
Share on other sites

As always with DllCall, the return value of the function called is stored in an array, in element [0]. You made the same error as before. :)

MsgBox(0, "Result", String($c[0]))
Of course i make the same error twice... xD
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...