Jump to content

Autoit SDL2 DLL Help


Recommended Posts

Hello,

I am trying to figure out why when displaying the amount of CPUs, it says 0 when it should say how many CPUs your CPU has. Note that I have wrapped this from the SDL2 library. Note that you do not have to automaticlly intiailize SDL2 when using some functions. Hence no SDL_Init in the example code.

 

From SDL2.au3 Wrapper

;Open the SDL2 DLL
Global $SDL = DllOpen("SDL2.dll")

;Check to make sure SDL2 DLL is there
if $SDL = NULL Then
   MsgBox($MB_SYSTEMMODAL,"Error","Could not load SDL2.dll!\n")
EndIf

Func SDL_GetCPUCount()
   $xSDL_GetCPUCount = DllCall($SDL,"int:cdecl","SDL_GetCPUCount")
   Return $xSDL_GetCPUCount
EndFunc
#include "SDL2.au3"

Local $Count = SDL_GetCPUCount()

MsgBox(1,"CPU","CPUS:" & $Count)

The MessageBox says 0, but I know it should tell me how many physical cores are in there. It worked when I wrapped SDL2 for my Euphoria programming language wrapper.  I can post any other code if it is needed.

Link to comment
Share on other sites

From the DllCall documentation:

Quote

an array is returned that contains the function return value and a copy of all the parameters (including parameters that the function may have modified when passed by reference).
$return[0] = function return value

 

Link to comment
Share on other sites

Like @RTFC said, the return from the DLLCall function is an array where [0] element is the return from the call.  Look up DllCall in the help file for more details.

This code works for me.  Note that I renamed the DLL file because I put both of them in the same folder as the script.

sdl_get_cpu_test()

Func sdl_get_cpu_test()
    Local $aReturn = DllCall("sdl2_x64.dll", "int:cdecl", "SDL_GetCPUCount")
    If @error Then
        ConsoleWrite(StringFormat("@error = %i", @error) & @CRLF)
        Exit -1
    EndIf
    ConsoleWrite(StringFormat("CPU count: %i", $aReturn[0]) & @CRLF)
EndFunc

 

Edited by TheXman
Link to comment
Share on other sites

;SDL2.au3 file

;Open the SDL2 DLL
Global $SDL = DllOpen("SDL2.dll")

;Check to make sure SDL2 DLL is there
if $SDL = -1 Then
   MsgBox($MB_SYSTEMMODAL,"Error","Could not load SDL2.dll!\n")
EndIf

;Struct for SDL_RECT
Func SDL_Rect($x,$y,$w,$h)
   $xSDL_Rect = DllStructCreate("int;int;int;int")
   DllStructSetData($SDL_Rect,1,$x)
   DllStructSetData($SDL_Rect,2,$y)
   DllStructSetData($SDL_Rect,3,$w)
   DllStructSetData($SDL_Rect,4,$h)
   Return $xSDL_Rect
EndFunc

Func SDL_GetCPUCount()
   Global $xSDL_GetCPUCount = DllCall($SDL,"int:cdecl","SDL_GetCPUCount")
  Return $xSDL_GetCPUCount[0]
EndFunc

 

;SDLTest1.au3 file
#include "SDL2.au3"

MsgBox(1,"CPUs:","CPU:" & SDL_GetCPUCount())

 

Note that I am trying to call the function from the SDL2.au3 file into the test file. It still is not showing up correctly.

Link to comment
Share on other sites

First, you should add @error checking after the DLLCall to make sure that it is successful.  Second, you need to make sure you are referencing the correct DLL file (32 or 64 bit).  If the DLL files are in the same folder, then you need to rename one like I did.  If you are on a 64-bit OS and you are going to put the DLL files in the System32 folder, then you need to put the 64 bit file in the System32 folder and the 32 bit DLL file in the SysWOW64 folder.

Link to comment
Share on other sites

14 minutes ago, LeftyGuitar said:

Note that I am trying to call the function from the SDL2.au3 file into the test file. It still is not showing up correctly.

With all due respect, I would get it working before trying to split the functions out into a UDF file.  The example I gave you, if it references the correct DLL file, works. Get it working on your machine and then create your UDF file of SDL2 functions. 

When you do get to creating the UDF, unless there are going to be numerous calls to the SDL functions in succession, you don't need to do the DLLOpen().

Link to comment
Share on other sites

Func SDL_GetCPUCount()
   Global $xSDL_GetCPUCount = DllCall($SDL,"int:cdecl","SDL_GetCPUCount")
   If @error Then
      ConsoleWrite("Error")
      Exit -1
   EndIf
  ConsoleWrite(StringFormat("CPUs: %i", $xSDL_GetCPUCount[0]) & @CRLF)
  ;Return $xSDL_GetCPUCount[0]
EndFunc

SDL_GetCPUCount()

;This is from the SDL2.au3 file

 

Ok I got it to work. I just can't get it to work in the UDF file. Note: That when I tried to run it from the SDL2.au3 file, The return command was not commented out.

#include "SDL2.au3"

Local $x = SDL_GetCPUCount()

MsgBox(1,"CPUs:","CPU:" & $x[0])

 

Link to comment
Share on other sites

The return value from the SDL_GetCPUCount function in your UDF is an integer not an array.  Therefore, $x is just an integer not an array.

MsgBox(1,"CPUs:","CPU:" & $x[0])

should be

MsgBox(1,"CPUs:","CPU:" & $x)

 

Edited by TheXman
Link to comment
Share on other sites

Here's a very minimal example of one way to do it.  It assumes that the 64-bit dll is named sdl2_x64.dll and the 32-bit dll is named sdl2.dll.

SDL2.au3

#include-once

Global $sDllFile = (@AutoItX64) ? ("sdl2_x64.dll") : ("sdl2.dll")

Func sdl_get_cpu()
    Local $aReturn = DllCall($sDllFile, "int:cdecl", "SDL_GetCPUCount")
    If @error Then Return SetError(1, @error, -1)
    Return $aReturn[0]
EndFunc

Func sdl_get_system_ram()
    Local $aReturn = DllCall($sDllFile, "int:cdecl", "SDL_GetSystemRAM")
    If @error Then Return SetError(1, @error, -1)
    Return $aReturn[0]
EndFunc

 

Test script

#include <SDL2.au3>

Global $giCpuCount, $giSystemRam

$giCpuCount = sdl_get_cpu()
If @error Then
    MsgBox(0, "SDL CPU Count", StringFormat("Error encountered\r\n@error = %i\r\n@extended = %i", @error, @extended))
    Exit
EndIf

$giSystemRam = sdl_get_system_ram()
If @error Then
    MsgBox(0, "SDL System RAM", StringFormat("Error encountered\r\n@error = %i\r\n@extended = %i", @error, @extended))
    Exit
EndIf

MsgBox( _
    0, "SDL Test", _
    StringFormat("CPU Count = %i" , $giCpuCount) & @CRLF & _
    StringFormat("System RAM = %i", $giSystemRam)  _
    )

 

Link to comment
Share on other sites

Thanks for your help. It works in the SDL2.au3 file, however in the test script I get exit code 4294967295, I read this could be due to any number of factors,

so I'm not sure why its not working in the test script, but works fine in the normal SDL2.au3 file.

 

SDL2.au3 file (Note I'm using the normal 32-bit DLL)

;Open the SDL2 DLL
Global $SDL = DllOpen("SDL2.dll")

;Check to make sure SDL2 DLL is there
if $SDL = -1 Then
   MsgBox($MB_SYSTEMMODAL,"Error","Could not load SDL2.dll!\n")
EndIf

Func SDL_GetCPUCount()
   Local $xSDL_GetCPUCount = DllCall($SDL,"int:cdecl","SDL_GetCPUCount")
   If @error Then
      ConsoleWrite("Error")
      Exit -1
   EndIf
  ;ConsoleWrite(StringFormat("CPUs: %i", $xSDL_GetCPUCount[0]) & @CRLF)
  Return $xSDL_GetCPUCount[0]
EndFunc

Func SDL_GetSystemRAM()
   Local $xSDL_GetSystemRAM = DllCall($SDL,"int:cdecl","SDL_GetSystemRAM")
   ;ConsoleWrite(StringFormat("RAM: %i", $xSDL_GetSystemRAM[0]) & @CRLF)
   Return $xSDL_GetSystemRAM[0]
EndFunc

 

Test Script

#include <SDL2.au3>

Global $giCpuCount, $giSystemRam

$giCpuCount = SDL_GetCPUCount()
If @error Then
    MsgBox(0, "SDL CPU Count", StringFormat("Error encountered\r\n@error = %i\r\n@extended = %i", @error, @extended))
    Exit
EndIf

$giSystemRam = SDL_GetSystemRAM()
If @error Then
    MsgBox(0, "SDL System RAM", StringFormat("Error encountered\r\n@error = %i\r\n@extended = %i", @error, @extended))
    Exit
EndIf

MsgBox( _
    0, "SDL Test", _
    StringFormat("CPU Count = %i" , $giCpuCount) & @CRLF & _
    StringFormat("System RAM = %i", $giSystemRam)  _
    )

 

Edited by LeftyGuitar
Link to comment
Share on other sites

If you ran the test script that I supplied then the value of @extended would have pointed you to the reason that the dllcall failed.  If it is "1", then it can't find the dll file.  Are all of your files (test script, sdl2.au3, and dll files) in the same folder?  My guess is no.  The real question is in which folder did you put the dll files?

1 = unable to use the DLL file,
2 = unknown "return type",
3 = "function" not found in the DLL file,
4 = bad number of parameters,
5 = bad parameter.
Edited by TheXman
Link to comment
Share on other sites

Link to comment
Share on other sites

cut and paste the output of the console window

Link to comment
Share on other sites

So you aren't using the test script and sdl2.au3 that I supplied?  My code had additional error checking that would have identified the issue.

Edited by TheXman
Link to comment
Share on other sites

Ok I added the error checking to the SDL2.au3 file and now it comes up with a message box saying Error encountered

@error = 1

@extended = 1

 

So it isn't able to use the DLL? That's odd as its in the same directory as both the SDL2.au3 and the test script file.

Link to comment
Share on other sites

No we are getting somewhere.  It can't find the dll file.  Did you put both dll files in the same folder?  Did you rename the 64 bit dll file to sdl2_x64.dll? 

From the console output, I see you are executing the script using the 32-bit autoit.exe.  so it will be looking for sdl2.dll, if you used the sdl2.au3 that I supplied.

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