Jump to content
Sign in to follow this  
DesireDenied

DllCall; Dev-cpp Dll Template

Recommended Posts

DesireDenied

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.


Share this post


Link to post
Share on other sites
piccaso

try DllOpen instead of _API_LoadLibrary

at least last time i checked the docs dllcall didnt support real dll handles

only the pseudo handles from DllOpen()


CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map

Share this post


Link to post
Share on other sites
piccaso

and there is no double type in dllcall (at least not jet)

you have to do it like Paul did here

you also forgot :cdecl

this one works:

$hDll = DllOpen("test.dll")
$result = DllCall($hDll,"int:cdecl","myAdd","int",1,"int",2)
ConsoleWrite("1+2=" & $result[0] & @CRLF)
Edited by piccaso

CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map

Share this post


Link to post
Share on other sites
DesireDenied

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

Share this post


Link to post
Share on other sites
piccaso

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

Share this post


Link to post
Share on other sites
DesireDenied

of course i did :)


Share this post


Link to post
Share on other sites
Ramnath

Hi All,

I need solution for the same problem. Could please send me if you found out already?

Thanks

Ramnath

Share this post


Link to post
Share on other sites
Ramnath

Hi All,

I need solution for the same problem. Could please send me if you found out already?

Thanks

Ramnath

Share this post


Link to post
Share on other sites
BrettF

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  

×