Sign in to follow this  
Followers 0
dantay9

creating dll for autoit

13 posts in this topic

#1 ·  Posted (edited)

Since the use of this dll is for autoit and I know there are some c++ experts here, I will ask here first. I am trying to put _PreviousPrime from here into a dll. I found an example on the internet with everything created already; all I had to do was change the function to what I wanted. There is something going wrong with the "long long" part of my function. At the very beginning of my function, I am returning the same value as the parameter. The return value here is a negative number so it is looping back on itself like it would if you assign a big number to an int. I also made sure the dll file was in the correct spot. I don't know much about c++, so can someone help me out a bit?

Here is the autoit code:

$a = DllCall(@ScriptDir & "\example1.dll", "int64", "previousprime", "int64", 9223372036854775783)
ConsoleWrite(@error & @CRLF)
_ArrayDisplay($a)

Here is the code for example1.cpp:

#include "stdafx.h"
#include <math.h>

BOOL APIENTRY DllMain( HANDLE hModule, 
    DWORD ul_reason_for_call, 
    LPVOID lpReserved
                 )
{
    return TRUE;
}

long long _stdcall previousprime(long long s_number)
{
    return (long long) s_number; // I am returning the parameter so it should return whatever was entered

    if(s_number == 3)
        return 2;
    if(s_number < 6)
        return 3;
    if(s_number < 8)
        return 5;

    long long i;
    long long number = 0;
    if(s_number % 2 == 0)
        number = s_number + 1; //make the number odd

    while(true)
    {
        number -= 2;
        if(number % 3 == 0)
            number -= 2;
        long long s_limit = (long long) sqrt((double) s_number);

        for(i = 5; i <= s_limit; i+= 2)
        {
            if(number % i == 0)
                break;
        }
        if(s_limit < i)
            return number;
    }
    return -1;
}

Here is example1.def:

LIBRARY example1

EXPORTS

previousprime @1

There are other files, but I think that is all you need. If you need other files, I will post them.

Edited by dantay9

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You didn't specify you're exporting the function using the __declspec keyword.

Edit: Yeah, I see the problem, it's definitely not __declspec.

What is more funny is that sending it 2^63-1 results in -(2^63) and the return value is 2^31...

Edited by Authenticity

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

OK. Read through that page and tried a bit out, but it didn't work. I tried putting "__declspec(dllexport)" in front of the function and also tried putting "__declspec(dllexport) long long _stdcall test(long long s_number);" right after the includes. Neither of these worked. Can you provide me with an example?

Edit: Just saw your edited post. Do you have any other ideas? Here are are the files before I built the dll.

Edited by dantay9

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I found out that if I use DllCall(@ScriptDir & "\example1.dll", "int", "previousprime", "int64", 65), it seems to work. Of course it only works for numbers below 2 billion though. But it works more than the original. Don't know why.

Edited by dantay9

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites

This seems to be the day for people to post non-autoit support questions in the AutoIt General Help and Support forum.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

I see that using a MessageBox you can confirm that the dll receives the true value but when the value is returned something is buggy, I'm tending to think it's something about how AutoIt expects the return value. Not every compiler return the value as EAX:EDX so it's hard to predict.

Example:

LONGLONG __stdcall previousprime(LONGLONG s_number)
{
    TCHAR szBuff[80];

    swprintf(szBuff, _T("%lld"), s_number);
    MessageBox(NULL, szBuff, _T("Test"), MB_OK);
    return s_number; // I am returning the parameter so it should return whatever was entered

    if(s_number == 3)
        return 2;
    if(s_number < 6)
        return 3;
    if(s_number < 8)
        return 5;

    long long i;
    long long number = 0;
    if(s_number % 2 == 0)
        number = s_number + 1; //make the number odd

    while(true)
    {
        number -= 2;
        if(number % 3 == 0)
            number -= 2;
        long long s_limit = (long long) sqrt((double) s_number);

        for(i = 5; i <= s_limit; i+= 2)
        {
            if(number % i == 0)
                break;
        }
        if(s_limit < i)
            return number;
    }
    return -1;
}

..don't forget to include stdio.h and tchar.h.

What I think you can do right now is to return a pointer or require additional parameter which will be something like:

$tagLargeInteger = DllStructCreate('int64')

; Or
$tagLONGLONG = DllStructCreate('int;int')

; Or
$tagInt64 = DllStructCreate('int[2]')

Share this post


Link to post
Share on other sites

Thank you for your help. I will try that out.


[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites

It might be the problem, Zedna.

But even without checking to see first, I've made a test using only AutoIt's number system and ConsoleWrite() types representations. If the third result is what dantay9 is getting then something is not correct about how EAX:EDX is expected or restored back to the original meaningful value:

ConsoleWrite(9223372036854775807 & @CRLF) ; 0x7FFFFFFFFFFFFFFF
ConsoleWrite(2147483647*4294967296+4294967295 & @CRLF) ; 0x7FFFFFFF << 32 | 0xFFFFFFFF
ConsoleWrite(4294967295*4294967296+2147483647 & @CRLF) ; 0xFFFFFFFF << 32 | 0x7FFFFFFF

Share this post


Link to post
Share on other sites

extern "C" is needed otherwise the exported name would be scrambled beyond recognition. Also, if you use stdcall (which is better than cdecl) your functions names will be decorated. For example int test(int) will be test@4 and int test(int,int) would be test@8 (it's simply the parameter size in bytes after the @ sign, it's a linker thing). You can avoid this with a definition file in your project.

Quck guide here to compiling and using dll.

http://www.autoitscript.com/forum/index.php?showtopic=87287&view=findpost&p=626480


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I did a little more testing and found out that it seems to only be a problem when "int64" is used as a return value when calling the dll function in autoit. From what I can conclude, there isn't anything wrong with the c++ function, it is autoit's dllcall. There could be something wrong with the compiling of the dll and that might be what is being discussed now. I can't be of much support because all this talk is WAY over my head.

Edit: returning an unsigned long works

Edited by dantay9

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites

I think that you should submit a bug track because there is a problem when the return value is int64 and I think that you can assume what is the problem. AutoIt swaps the dwords on function. Take this for example:

example.dll

extern "C"{
    LONGLONG __stdcall test(LONGLONG val)
    {
        LONGLONG temp = (((val & 0xFFFFFFFF) << 32) | (val >> 32));

        return temp;
    }
}

The code is built without any warnings and is perfectly correct. Also, a .def module file was added to export the function without name mangling and the result in AutoIt is of course, LO_DWORD becomes HI_DWORD, vice versa.

$a = DllCall(@ScriptDir & '\example.dll', 'int64', 'test', 'int64', 9223372036854775807)
If Not @error Then ConsoleWrite($a[0] & @CRLF)

Share this post


Link to post
Share on other sites

I think it would be better if you were to submit the bug track because you know a lot more about this than I do. I have only been using c++ for a week and a half.


[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

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