Jump to content

Recommended Posts

Posted

i am having some difficulties on calling my own functions from my own dll.

this example is simpliest as i can imagine but still - doesn't work for me, why?

i'am using here dev-cpp dll template

(pure dll template as you can see, empty body of some dll class constructor/destructor is not even removed but think we can ignore it)

/* this is dllmain.cpp file */
/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>

DllClass::DllClass()
{

}

DllClass::~DllClass()
{

}

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;
}

/* myAdd function */
int myAdd (int value1, int value2)
{
return value1+value2;
}

/* myPow function */
double myPow (double value)
{
return value*value;
}

/* this is dll.h file */
#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 */

class DLLIMPORT DllClass
{
public:
 DllClass();
 virtual ~DllClass(void);

private:

};


extern "C"
{
 int DLLIMPORT myAdd(int value1, int value2);
}


extern "C"
{
 double DLLIMPORT myPow(double value);
}
#endif /* _DLL_H_ */

and at last, this is my autoit code.

;~ test.au3 file
#include <A3LLibrary.au3>

$result=MyAdd(5,3)
MsgBox('','My.dll::MyAdd',$result,1)
$result=MyPow(6)
MsgBox('','My.dll::MyPow',$result,1)
$result=MyAdd(5,3,1)
MsgBox('','My.dll::MyAdd, Method=1',$result,1)
$result=MyPow(6,1)
MsgBox('','My.dll::MyPow, Method=1',$result,1)



Func MyAdd($value1, $value2,$Method=0)
    Local $AddResult
    $AddResult = DllStructCreate('int AddResult')
    
    If $Method=0 Then
    $dll = DllOpen('my.dll')
    Else
    $dll = _API_LoadLibrary('my.dll')
    EndIf

    $AddResult = DllCall($dll,'int','myAdd','int',$value1,'int',$value2)
    
    Return $AddResult
    DllClose($dll)
EndFunc
    
Func MyPow($value,$Method=0)
    Local $PowResult
    $PowResult = DllStructCreate('double PowResult')

    If $Method=0 Then
    $dll = DllOpen('my.dll')
    Else
    $dll = _API_LoadLibrary('my.dll')
    EndIf

    $PowResult = DllCall($dll,'double','myAdd','double',$value)

    Return $PowResult
    DllClose($dll)
EndFunc

my code always return 0 and this is not a good sign i supose... can't figure out why it is like that.

Posted (edited)

THANKS for respond.

DllOpen isn't helping me at all, already tried this is that way ($Method=0/1 in my func) with no satisfying results.

im not so skilled on c++, and hope i didn't missed anything really important in this example (making such a simple dll / calling my dll functions from autoit) but something must be wrong in here if this code doesn't want to work even if i checked everything number of times. :)

EDITED:

woah, finally it works! :)

thanks for helping me solve the.... that would be a lie lol. thanks for solving my problem :)

Edited by DesireDenied
Posted (edited)

did you read my second repy?

i dont know how much about double but this way you can use float:

void myPow (float *value) {
 *value = *value * *value;
 }
$float = DllStructCreate("float")
DllStructSetData($float,1,5)
$x = DllCall("test.dll","none:cdecl","myPow","ptr",DllStructGetPtr($float))
ConsoleWrite(DllStructGetData($float,1) & @CRLF)

edit:

ok you did read it :)

Edited by piccaso
CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
  • 1 year later...

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
×
×
  • Create New...