Jump to content

How to correctly DllCall atoi ( char const* _Str ) ?


E1M1
 Share

Recommended Posts

Hi!

 

I would like to call atoi from ntdll.dll

If I call it without arguments it returns 0 and sets @error 0 so the function exists

$converted = DllCall ( "ntdll.dll", "int", "atoi")
ConsoleWrite(@error & @CRLF)
ConsoleWrite($converted[0] & @CRLF)

But I would like to call it with real arguments of course

$converted = DllCall ( "ntdll.dll", "int", "atoi", "str", "1234")
ConsoleWrite(@error & @CRLF)
ConsoleWrite($converted[0] & @CRLF)

But when I call this, autoit.exe simply crashes.  It doesn't even reach to ConsoleWrite anymore. What am I doing wrong?

Edited by E1M1

edited

Link to comment
Share on other sites

Your 2nd snippet, the one that passes valid data, works fine on my PC.

atoi returned $converted[0] as an Int32 with a value of 1234.

#include <Constants.au3>
#include <Debug.au3>

_DebugSetup()

$aResult = DllCall ("ntdll.dll", "int", "atoi", "str", "1234")
If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "DllCall failed with @error = " & @error)

_DebugReportVar('$aResult'   , $aResult)
_DebugReportVar('$aResult[0]', $aResult[0])

Debug output:

@@ Debug(9) : {Array 1D} -> $aResult[2]
[0] 1234    
[1] "1234"  
@@ Debug(10) : {Int32} -> $aResult[0] = 1234

 

Edited by TheXman
Link to comment
Share on other sites

I have a 64-bit OS.  If I run the snippet as 32bit, it crashes.  When I run it as 64bit, it works fine.

Edited by TheXman
Link to comment
Share on other sites

Hi!

I also have 64 bit Win 10.

 

How do you exactly run it in 64 bit mode? I tried adding #AutoIt3Wrapper_UseX64=y but it still crashes.

The funny thing is that if I do the same thing in 64bit node.js using ffi-napi then it works. If I call it from autoit without args it also works - it just returns 0. But if I call it autoit with argument then it crashes. Even if I compile it in 64 bit mode.

What version of autoit were you using?

I guess I could try it although I believe they haven't changed DllCall for at least last 10 years or so :P

edited

Link to comment
Share on other sites

#AutoIt3Wrapper_UseX64=Y

The directive above will force it to use the 64bit stub (autoit3_x64.exe) when run in the editor or compiled.

I am running AutoIt v3.3.14.5.  The PC I am currently using is Win7 64bit.  I didn't try it on my Win10 64bit PC.

Update:
I get the same results on my Win10 x64 PC as I do on my Win7 x64 PC.

Edited by TheXman
Link to comment
Share on other sites

5 minutes ago, E1M1 said:

How do you exactly run it in 64 bit mode? I tried adding #AutoIt3Wrapper_UseX64=y but it still crashes.

How are you running it?  Are you pressing F5 in the editor?  Are you right-clicking the script and executing it?  Is it compiled?

Link to comment
Share on other sites

You're using the wrong calling convention in X86 (stdcall is by default, as the help file says).

Use cdecl for calling such a X86 Windows standard C function unexpectedly exported by some system DLL.
X64
uses another calling convention (fastcall) and in this mode, the calling convention specified in DllCall is simply ignored.

This works in both X86 & X64:

#include <Constants.au3>
#include <Debug.au3>

_DebugSetup()

$aResult = DllCall("ntdll.dll", "int:cdecl", "atoi", "str", "1234")
If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "DllCall failed with @error = " & @error)

_DebugReportVar('$aResult'   , $aResult)
_DebugReportVar('$aResult[0]', $aResult[0])

 

Edited by jchd
Finally seems correct

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

3 hours ago, E1M1 said:

interesting to know how you found out that it was cdecl ?

The atoi function is declared in the C header file stdlib.h.  Depending on the compiler, the declaration may look a little different but they all are basically the same.  The declaration looks like:

__cdecl atoi   (_In_z_ char const* _String);

 

GetTickCount declaration is in sysinfoapi.h:

WINBASEAPI
DWORD
WINAPI
GetTickCount(
    VOID
    );

WINAPI, in the declaration above, resolves to __stdcall.

Edited by TheXman
Link to comment
Share on other sites

So it wasn't as dumb as it seemed. Re-editing the offending post!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

33 minutes ago, jchd said:

So it wasn't as dumb as it seemed. Re-editing the offending post!

No, it was a good catch.  I had found a generic declaration of the function that didn't specify the calling convention so I assumed it was stdcall.  I know better than that.  :)

Edited by TheXman
Link to comment
Share on other sites

Looking closer, there are two kinds of functions exported by ntdll.dll: those which are documented part of the official Windows API (they use stdcall for X86) and those (used internally but undocumented) which come from the standard C libraries (they use cdecl for X86) which atoi is part of.

Of course it isn't a good idea to rely on undocumented features and stuff, but it's currently unlikely that Windows libraries will completely change their design and undocumented interfaces today.

That offers interesting ways to perform fast operations which are slow, painful or hard to code in pure AutoIt. Also given the good reliability of many simple C functions, it's safe to use them.

For instance and as a trivial example, we now can easily display the full range of unsigned 64-bit integers, even in any radix (2 to 36) supported by the C functions. Here's a large int64 (treated as unsigned) displayed in base 13:

Local $s = ""
$aRes = DllCall("ntdll.dll", "str:cdecl", "_ui64toa", "int64", 0xFFF801201234567, "str", $s, "int", 13)
ConsoleWrite($aRes[0] & @LF)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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...