Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

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


  • Please log in to reply
3 replies to this topic

#1 shanet

shanet

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 313 posts

Posted 19 February 2012 - 07:52 AM

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:
Plain Text         
#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

Attached Files


My code does not have bugs! It just develops random features.My ProjectsPure randomness
  • Small Minds.......................................................................................................Simple progress bar that changes direction at either sides.
  • ChristmasIt AutoIt Christmas Theme..........................................................I WAS BOOOORED!
  • DriveToy..............................................................................................................Simple joke script. Trick your friends into thinking their computer drive is haywire!
In Development
  • Your Background Task Organiser
  • AInstall Second Generation
BEFORE POSTING ON THE FORUMS, TRY THIS:
%programfiles%/AutoIt3/autoit3.chm







#2 funkey

funkey

    New Dad

  • Active Members
  • PipPipPipPipPipPip
  • 602 posts

Posted 19 February 2012 - 08:06 AM

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, 19 February 2012 - 08:06 AM.

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.


#3 shanet

shanet

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 313 posts

Posted 19 February 2012 - 08:16 AM

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, 19 February 2012 - 08:24 AM.

My code does not have bugs! It just develops random features.My ProjectsPure randomness
  • Small Minds.......................................................................................................Simple progress bar that changes direction at either sides.
  • ChristmasIt AutoIt Christmas Theme..........................................................I WAS BOOOORED!
  • DriveToy..............................................................................................................Simple joke script. Trick your friends into thinking their computer drive is haywire!
In Development
  • Your Background Task Organiser
  • AInstall Second Generation
BEFORE POSTING ON THE FORUMS, TRY THIS:
%programfiles%/AutoIt3/autoit3.chm

#4 shanet

shanet

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 313 posts

Posted 19 February 2012 - 08:32 AM

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!
My code does not have bugs! It just develops random features.My ProjectsPure randomness
  • Small Minds.......................................................................................................Simple progress bar that changes direction at either sides.
  • ChristmasIt AutoIt Christmas Theme..........................................................I WAS BOOOORED!
  • DriveToy..............................................................................................................Simple joke script. Trick your friends into thinking their computer drive is haywire!
In Development
  • Your Background Task Organiser
  • AInstall Second Generation
BEFORE POSTING ON THE FORUMS, TRY THIS:
%programfiles%/AutoIt3/autoit3.chm




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users