Sign in to follow this  
Followers 0

Creating DLL in C++ for use with AutoIt script - having issues.

4 posts in this topic

#1 ·  Posted

Hi guys,

As the title states, I am having issues creating a DLL for use with my AutoIt script.

It compiles without error, however I can not get it to work with C++ or AutoIt - however I have not worked with DLLs in AutoIt before. Are there any good resources to aid in creation of DLLs? I am guessing there is a problem with my code.

As you will see in the header, I want to return a string (or array of chars). The two parameters are also string/char arrays.

My header:

#define DLL_EXPORT __declspec (dllexport)
#ifdef __cplusplus
extern "C" {
#endif
char * DLL_EXPORT licenseCheck(char * salt, char * user);

That is the header of the actual DLL.

The following is the C++ code I am using in an attempt to access this DLL:

#include <iostream>
#include <windows.h>

using namespace std;

typedef char (*LicFunc)(char, char);

HINSTANCE hinstDLL;

int main() {
//  LicFunc licenseCheck("","");
    hinstDLL = LoadLibrary("../../../MichaelsFuncs/bin/Release/MichaelsFuncs.dll");
    FARPROC licenseCheck = GetProcAddress(hinstDLL, "licenseCheck");
    if (licenseCheck == 0) {
        cout << "licenseCheck is NULL\n";
        return 0;
    }

    typedef char (__stdcall * pICFUNC)( char *, char *);
    pICFUNC licenseCheck(char, char);
    licenseCheck = pICFUNC(licenseCheck);

    cout << licenseCheck(*"<|+5 :{ax?H_+,^o", *"Test");
    FreeLibrary(hinstDLL);
    return 0;
}

And this is my AutoIt code, which is also trying to access this DLL:

$dll = DllOpen(@ScriptDir&"\MichaelsFuncs.dll")
if ($dll = -1) Then MsgBox(0, '', 'Could not open DLL')
MsgBox(0, '', DllCall($dll, "str", "licenseCheck", "str", "<|+5 :{ax?H_+,^o", "str", "test@user.com"))
DllClose($dll)

I am sure the errors are in the DLL - can anyone please guide me along to get this DLL working properly?

The compiled DLL is attached.

Thanks

myDLL.dll


[font="Comic Sans MS"]My code does not have bugs! It just develops random features.[/font]My Projects[list][*]Live Streaming (Not my project, but my edited version)[right]AutoIt Wrappers![/right][/list]Pure randomness[list][*]Small Minds.......................................................................................................[size="1"]Simple progress bar that changes direction at either sides.[/size][*]ChristmasIt AutoIt Christmas Theme..........................................................[size="1"]I WAS BOOOORED![/size][*]DriveToy..............................................................................................................[size="1"]Simple joke script. Trick your friends into thinking their computer drive is haywire![/size][/list]In Development[list][*]Your Background Task Organiser[*]AInstall Second Generation[/list]BEFORE POSTING ON THE FORUMS, TRY THIS:
%programfiles%/AutoIt3/autoit3.chm

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Do not return string. It is common to pass them by reference.

int DLL_EXPORT licenseCheck(char * salt, char * user, char * result);

Edited by funkey

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Do not return string. It is common to pass them by reference.

int DLL_EXPORT licenseCheck(char * salt, char * user, char * result);

I have to return a string - and AutoIt does support returning strings - right?

Sorry I mis-read that - passing by reference may work.

The crash is because you need to use the 'cdecl' calling method. Change your return value to "str:cdecl".

Also, you are trying to use a MessageBox to display an array.

No I am not using a messagebox to display an array.

So I need to change

#define DLL_EXPORT __declspec (dllexport)

to

#define DLL_EXPORT str:cdecl (dllexport)

correct?

Edited by shanet

[font="Comic Sans MS"]My code does not have bugs! It just develops random features.[/font]My Projects[list][*]Live Streaming (Not my project, but my edited version)[right]AutoIt Wrappers![/right][/list]Pure randomness[list][*]Small Minds.......................................................................................................[size="1"]Simple progress bar that changes direction at either sides.[/size][*]ChristmasIt AutoIt Christmas Theme..........................................................[size="1"]I WAS BOOOORED![/size][*]DriveToy..............................................................................................................[size="1"]Simple joke script. Trick your friends into thinking their computer drive is haywire![/size][/list]In Development[list][*]Your Background Task Organiser[*]AInstall Second Generation[/list]BEFORE POSTING ON THE FORUMS, TRY THIS:
%programfiles%/AutoIt3/autoit3.chm

Share this post


Link to post
Share on other sites

#4 ·  Posted

No, I meant you need to change the return value in your AutoIt code. The second parameter of DllCall is the return value, set it to "str:cdecl".

You are using a MessageBox to display the return value of your DllCall, and the return value of DllCall is always an array (unless the DllCall fails).

This code works for me and returns 5.

#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#include "Array.au3"
#NoTrayIcon

Global $hDll = DllOpen(@ScriptDir & "Test.dll")
If $hDll = -1 Then Exit MsgBox(0, "", "Failed to open Test.dll")
Global $aRet = DllCall($hDll, "str:cdecl", "licenseCheck", "str", "test", "str", "test@user.com")
ConsoleWrite("!" & @error & "-" & @extended & @CRLF)
DllClose($hDll)

_ArrayDisplay($aRet)
MsgBox(0, "", $aRet[0])
Ah. Sorry I thought you were talking about the DLL.

Thanks 69225, I will try that!


[font="Comic Sans MS"]My code does not have bugs! It just develops random features.[/font]My Projects[list][*]Live Streaming (Not my project, but my edited version)[right]AutoIt Wrappers![/right][/list]Pure randomness[list][*]Small Minds.......................................................................................................[size="1"]Simple progress bar that changes direction at either sides.[/size][*]ChristmasIt AutoIt Christmas Theme..........................................................[size="1"]I WAS BOOOORED![/size][*]DriveToy..............................................................................................................[size="1"]Simple joke script. Trick your friends into thinking their computer drive is haywire![/size][/list]In Development[list][*]Your Background Task Organiser[*]AInstall Second Generation[/list]BEFORE POSTING ON THE FORUMS, TRY THIS:
%programfiles%/AutoIt3/autoit3.chm

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  
Followers 0