E1M1 Posted October 9, 2020 Share Posted October 9, 2020 (edited) 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 October 9, 2020 by E1M1 edited Link to comment Share on other sites More sharing options...
TheXman Posted October 9, 2020 Share Posted October 9, 2020 (edited) 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 October 9, 2020 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
TheXman Posted October 9, 2020 Share Posted October 9, 2020 (edited) 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 October 9, 2020 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
E1M1 Posted October 9, 2020 Author Share Posted October 9, 2020 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 edited Link to comment Share on other sites More sharing options...
TheXman Posted October 9, 2020 Share Posted October 9, 2020 (edited) #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 October 9, 2020 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
TheXman Posted October 9, 2020 Share Posted October 9, 2020 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? CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
jchd Posted October 9, 2020 Share Posted October 9, 2020 (edited) 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 October 11, 2020 by jchd Finally seems correct E1M1 1 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
E1M1 Posted October 10, 2020 Author Share Posted October 10, 2020 Thanks! @jchd, would be interesting to know how you found out that it was cdecl ? For example: DllCall ( "kernel32.dll", "int", "GetTickCount" ) works fine with stdcall. edited Link to comment Share on other sites More sharing options...
TheXman Posted October 10, 2020 Share Posted October 10, 2020 (edited) 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 October 10, 2020 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
jchd Posted October 10, 2020 Share Posted October 10, 2020 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
TheXman Posted October 10, 2020 Share Posted October 10, 2020 (edited) 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 October 10, 2020 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
jchd Posted October 10, 2020 Share Posted October 10, 2020 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) Danyfirex 1 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now