Jump to content

creating dll for autoit


dantay9
 Share

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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!"

Link to comment
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]')
Link to comment
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
Link to comment
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!

Link to comment
Share on other sites

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
Link to comment
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)
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...