Jump to content
Sign in to follow this  
johnmcloud

_WinAPI_CreateCompatibleBitmap to file

Recommended Posts

Hi guys

I'm seaching an alternative way to ScreenCapture UDF. Why? I think GDI+ is slow if you need to capture many images  :D

Searching on the web for the fastest method i have found:

1) Using Surface DirectX method

2) Using BitBlt method

The Directx method ( here you can find and example in C# ) is probably tha fastest but it's totally out of my knowledge, and i'm not sure can be done in autoit.

For the second i'm sure can be done in autoit and this is what i have make:

#include <WinAPI.au3>
;~ #include <ScreenCapture.au3> ; Testin purpose

Global Const $SRCCOPY = 0x00CC0020

$hDDC = _WinAPI_GetDC(0)
$hCDC = _WinAPI_CreateCompatibleDC($hDDC)
$hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, @DesktopWidth, @DesktopHeight)
_WinAPI_SelectObject($hCDC, $hBMP)
_WinAPI_BitBlt($hCDC, 0, 0, @DesktopWidth, @DesktopHeight, $hDDC, 0, 0, $SRCCOPY)
_WinAPI_ReleaseDC(0, $hDDC)
_WinAPI_DeleteDC($hCDC)
;~ _ScreenCapture_SaveImage(@ScriptDir & "\GDIPlus_Image.jpg", $hBMP) ; testing purpose
_WinAPI_SaveBMP(@ScriptDir & "\GDIPlus_Image.bmp", $hBMP) ; BMP is sure, PNG if possible. 

Func _WinAPI_SaveBMP($sFilename, $hData)
    $file = _WinAPI_CreateFile($sFileName)
    _WinAPI_CloseHandle($file)
;~  _WinAPI_WriteFile ; ---> Write file header and info header
;~  _WinAPI_CloseHandle($file) ; Close the handle
;~  _WinAPI_WriteFile ; ---> Write data of the bitmap
;~  _WinAPI_CloseHandle($file)
EndFunc

As you can see, work if i use GDI-ScreenCapture UDF but the goal is avoid that, if i need to use GDI i'll use the UDF :D

The main problem is i don't know how to write in the file the information stored in _WinAPI_CreateCompatibleBitmap ( so Save HBITMAP to file trans.gif?cver=0%0D%0A)

I need to create the file header, info header and then write the data? ( i think :D ) but i'm here because i need an help for do the last part of the code.

Thanks

Edited by johnmcloud

Share this post


Link to post
Share on other sites

Try this:

 

#include <WinAPI.au3>
#include <GDIPlus.au3>

Global Const $SRCCOPY = 0x00CC0020
_GDIPlus_Startup()

Local $t = TimerInit()
Local $hWnd = _WinAPI_GetDesktopWindow()
Local $hDDC = _WinAPI_GetDC($hWnd)
Local $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
Local $hHBMP = _WinAPI_CreateCompatibleBitmap($hDDC, @DesktopWidth, @DesktopHeight)
_WinAPI_SelectObject($hCDC, $hHBMP)
_WinAPI_BitBlt($hCDC, 0, 0, @DesktopWidth, @DesktopHeight, $hDDC, 0, 0, $SRCCOPY)
Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBMP)
_GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\Test.jpg")
_GDIPlus_BitmapDispose($hBitmap)
_WinAPI_DeleteObject($hHBMP)
_WinAPI_ReleaseDC($hWnd, $hDDC)
_WinAPI_DeleteDC($hCDC)
ConsoleWrite(TimerDiff($t) & @LF)

_GDIPlus_Shutdown()
Exit
 

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Hi UEZ, thanks for the answer.

As i said before, i'd like to avoid gdi. I'd like to make at least 1 screen every 41ms, with the _ScreenCapture UDF( it use gdi ) i can make 1 screen every 100ms

I'll test your solution and let you know, anyway i'm searching a method like c++ with saving file creating header etc.

Share this post


Link to post
Share on other sites

Look at WinAPIEx to get some ideas how to save GDI bitmaps. I don't know whether those methods are faster. Try it.

You can speed up the code above by using directly the calls instead of calling functions. This can give you some ms back.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Here the nacked version from post#2:

#include <GDIPlus.au3>

Global Const $SRCCOPY = 0x00CC0020
_GDIPlus_Startup()
Local $hDLL_user32 = DllOpen("user32.dll")
Local $hDLL_gdi32 = DllOpen("gdi32.dll")
Local $hDLL_ole32 = DllOpen("ole32.dll")

Local $t = TimerInit()
Local $aResult = DllCall($hDLL_user32, "hwnd", "GetDesktopWindow")
Local $hWnd = $aResult[0]
$aResult = DllCall($hDLL_user32, "handle", "GetDC", "hwnd", $hWnd)
Local $hDDC = $aResult[0]
$aResult = DllCall($hDLL_gdi32, "handle", "CreateCompatibleDC", "handle", $hDDC)
Local $hCDC = $aResult[0]
$aResult = DllCall($hDLL_gdi32, "handle", "CreateCompatibleBitmap", "handle", $hDDC, "int", @DesktopWidth, "int", @DesktopHeight)
Local $hHBMP = $aResult[0]
DllCall($hDLL_gdi32, "handle", "SelectObject", "handle", $hCDC, "handle", $hHBMP)
DllCall($hDLL_gdi32, "bool", "BitBlt", "handle", $hCDC, "int", 0, "int", 0, "int", @DesktopWidth, "int", @DesktopHeight, "handle", $hDDC, "int", 0, "int", 0, "dword", $SRCCOPY)
$aResult = DllCall($ghGDIPDll, "int", "GdipCreateBitmapFromHBITMAP", "handle", $hHBMP, "handle", 0, "ptr*", 0)
Local $hBitmap = $aResult[3]
Local $sCLSID = "{557CF401-1A04-11D3-9A73-0000F81EF32E}" ;JPG
Local $tGUID = DllStructCreate($tagGUID)
DllCall($hDLL_ole32, "long", "CLSIDFromString", "wstr", $sCLSID, "struct*", $tGUID)
DllCall($ghGDIPDll, "int", "GdipSaveImageToFile", "handle", $hBitmap, "wstr", @ScriptDir & "\Test.jpg", "struct*", $tGUID, "struct*", 0)
DllCall($ghGDIPDll, "int", "GdipDisposeImage", "handle", $hBitmap)
DllCall($hDLL_gdi32, "bool", "DeleteObject", "handle", $hHBMP)
DllCall($hDLL_user32, "int", "ReleaseDC", "hwnd", $hWnd, "handle", $hDDC)
DllCall($hDLL_gdi32, "bool", "DeleteDC", "handle", $hCDC)
ConsoleWrite(TimerDiff($t) & @LF)

DllClose($hDLL_gdi32)
DllClose($hDLL_ole32)
DllClose($hDLL_user32)

_GDIPlus_Shutdown()
Exit

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

So UEZ, i have tested both version but give me the same result. i can make 1 screen every 45,5ms, much better then the ScreenCapture UDF ( but for this i don't have any doubt ) but not like the result i'd like to make, but we are very close ;)

This is the test script i have make for check the screen for second:

DirCreate(@ScriptDir & "\Test")

$iCounter = 1
$iFramePerSec = 24
$iPause = 1000 / $iFramePerSec
$time = TimerInit()

Do
    $iStart = TimerInit()
    ;code @ScriptDir & "\Test\" & $iCounter & ".jpg"
    $iPauseLess = TimerDiff($iStart)
    $iStart2 = TimerInit()
    Sleep(Int($iPause - $iPauseLess))
    $iCounter += 1
Until TimerDiff($time) >= 1000

My last try is to use the WinAPIEx for save the file instead of GDI, but i don't know how to start, there are many func there :D

Suggestion?

Thanks

Edited by johnmcloud

Share this post


Link to post
Share on other sites

Well, i have found in WinAPIEx the _WinAPI_SaveHBITMAPToFile. I think that funcion do many "unecessary check" before creating the file ( in comparison with a C++ version like this ) and is more slow than the GDI, only 71ms for second

So for now the best was my first version + saving image in gdi by uez, but the goal I have set for myself i was not able to reach. I'm open to any other ideas ;)

Edited by johnmcloud

Share this post


Link to post
Share on other sites

For me the real bottleneck seems to be the actual "save to disk" call.

#include <GDIPlus.au3>

Global Const $SRCCOPY = 0x00CC0020
_GDIPlus_Startup()
Local $hDLL_user32 = DllOpen("user32.dll")
Local $hDLL_gdi32 = DllOpen("gdi32.dll")
Local $hDLL_ole32 = DllOpen("ole32.dll")

Local $t = TimerInit()
Local $aResult = DllCall($hDLL_user32, "hwnd", "GetDesktopWindow")
Local $hWnd = $aResult[0]
$aResult = DllCall($hDLL_user32, "handle", "GetDC", "hwnd", $hWnd)
Local $hDDC = $aResult[0]
$aResult = DllCall($hDLL_gdi32, "handle", "CreateCompatibleDC", "handle", $hDDC)
Local $hCDC = $aResult[0]
$aResult = DllCall($hDLL_gdi32, "handle", "CreateCompatibleBitmap", "handle", $hDDC, "int", @DesktopWidth, "int", @DesktopHeight)
Local $hHBMP = $aResult[0]
DllCall($hDLL_gdi32, "handle", "SelectObject", "handle", $hCDC, "handle", $hHBMP)
DllCall($hDLL_gdi32, "bool", "BitBlt", "handle", $hCDC, "int", 0, "int", 0, "int", @DesktopWidth, "int", @DesktopHeight, "handle", $hDDC, "int", 0, "int", 0, "dword", $SRCCOPY)
$aResult = DllCall($ghGDIPDll, "int", "GdipCreateBitmapFromHBITMAP", "handle", $hHBMP, "handle", 0, "ptr*", 0)
Local $hBitmap = $aResult[3]
Local $sCLSID = "{557CF401-1A04-11D3-9A73-0000F81EF32E}" ;JPG
Local $tGUID = DllStructCreate($tagGUID)
DllCall($hDLL_ole32, "long", "CLSIDFromString", "wstr", $sCLSID, "struct*", $tGUID)
ConsoleWrite(TimerDiff($t) & @LF)
DllCall($ghGDIPDll, "int", "GdipSaveImageToFile", "handle", $hBitmap, "wstr", @ScriptDir & "\Test.jpg", "struct*", $tGUID, "struct*", 0)
ConsoleWrite(TimerDiff($t) & @LF)
DllCall($ghGDIPDll, "int", "GdipDisposeImage", "handle", $hBitmap)
DllCall($hDLL_gdi32, "bool", "DeleteObject", "handle", $hHBMP)
DllCall($hDLL_user32, "int", "ReleaseDC", "hwnd", $hWnd, "handle", $hDDC)
DllCall($hDLL_gdi32, "bool", "DeleteDC", "handle", $hCDC)
ConsoleWrite(TimerDiff($t) & @LF)

DllClose($hDLL_gdi32)
DllClose($hDLL_ole32)
DllClose($hDLL_user32)

_GDIPlus_Shutdown()
Exit

 

Two things to overcome this come to my mind.

Either use a second child process for the saving (use some kind of stack for the bitmaps).

Or maybe take a look in the example section for how to save bitmaps as blobs to an (in memory) sqlite database for later processing.

Share this post


Link to post
Share on other sites

I'm not totally conviced about the "save to disk" problem, i think is only a problem of "speed" of execution. If was a save to disk problem how can DirectX method saves 15-25ms at images? Tested a demo code on a couple of VM

Anyway i don't need a funcion faster like that but something in 41ms, so i can use a thing like _GDIPlus_SaveImageToStream for multiple images and than saves later?

Edited by johnmcloud

Share this post


Link to post
Share on other sites

What also came to my mind... saving pictures in such a short succession, wouldn't it be better to save them as a movie file? I remember seeing some code for that... by UEZ too?

Edit: Yep, by UEZ :), >here it is. I did not compare the performance, but might be worth a look. An option would be to save as avi and then, if really needed, to extract the single pictures afterwards

Edited by KaFu

Share this post


Link to post
Share on other sites

Many things comes to your mind :D

It save the hbitmap directly to AVI but the question is, how fast it is? It uses _ScreenCapture, from my test it's the worst method in terms of speed but now i'll check it and let you know

Edited by johnmcloud

Share this post


Link to post
Share on other sites

TESTED: If i put 24fps and one of the compression...the video is superfaster :D

If i use non-compressed format seems "normal" ( i don't know how many screen for second ) but the output for 5 second of video is 150MB.

Just for know, I don't have understand how to select the video compression without popop, example the Microsoft Video 1 with 100% Quality. I think is in the UDF, i don't have found any parameter in _CreateAVI

Edited by johnmcloud

Share this post


Link to post
Share on other sites

TESTED: If i put 24fps and one of the compression...the video is superfaster :D

If i use non-compressed format seems "normal" ( i don't know how many screen for second ) but the output for 5 second of video is 150MB.

Just for know, I don't have understand how to select the video compression without popop, example the Microsoft Video 1 with 100% Quality. I think is in the UDF, i don't have found any parameter in _CreateAVI

 

I guess that the answer to this questions might be buried somewhere in this >post (esp. the last post might give a clue).

Edited by KaFu

Share this post


Link to post
Share on other sites

I'm checking what i can do with AVIWriter UDF. I have only a problem, based on the example by UEZ posted by KaFu's post, if i set the $rec_duration = 10 ; ten second and i want to stop it with a hotkey, how i can do?

I have try to:

1) Exit the main loop

2) _CloseAvi

But everytime autoit crash with -1073741819. Thanks for any help

EDIT: Wait, is with the HotKeySet the problem. If i use:

Func _Test1()
 Start()
EndFunc

Func _Test2()
 Stop()
EndFunc

With two different hotkey i don't have problem, if i use the same hotkey in this way:

Func _StartStop()
    If $fFlag Then
        Start()
    Else
        Stop()
    EndIf
    $fFlag = Not $fFlag
EndFunc   ;==>_StartStop

Crash! Yes, i have declared the flag, if i use ConsoleWrite work...that's strange

Edited by johnmcloud

Share this post


Link to post
Share on other sites

Try something like this here:

#include "AVIWriter Extented.au3"
#include <Misc.au3>
#include <ScreenCapture.au3>
#include <WindowsConstants.au3>

_GDIPlus_Startup()

Global Const $sFile = @ScriptDir & "\test.avi"
FileDelete($sFile)

Global $rec_duration = 20 ;20 seconds
Global $fps = 10

Global Const $dll = DllOpen("user32.dll")
Global $hGUI_Grab2AVI = GUICreate("", 0, 0, 0, 0, $WS_POPUP, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW, WinGetHandle(AutoItWinGetTitle()))
GUISetBkColor(0xFF0000, $hGUI_Grab2AVI)
GUISetState(@SW_SHOW, $hGUI_Grab2AVI)
Global $aMPos, $hWin, $hWinAncestor, $hWnd, $aRet_prev, $aPos
Global Const $frame_size = 3
Global $tPoint = DllStructCreate($tagPOINT)
Global $esc = True
Global $mxo, $myo

While Not _IsPressed("1B", $dll) * Sleep(30)
    $aMPos = MouseGetPos()
    DllStructSetData($tPoint, 1, $aMPos[0])
    DllStructSetData($tPoint, 2, $aMPos[1])
    $hWin = _WinAPI_WindowFromPoint($tPoint)
    $hWinAncestor = _WinAPI_GetAncestor($hWin, 2)
    $hWnd = HWnd($hWinAncestor)
    $aRet_prev = -1
    $aPos  = WinGetPos($hWnd)
    If $hWnd <> $hGUI_Grab2AVI And $hWnd <> $aRet_prev Then
        $aRet_prev = $hWnd
        If $aMPos[0] <> $mxo Or $aMPos[1] <> $myo Then
            WinMove($hGUI_Grab2AVI, "", $aPos[0], $aPos[1], $aPos[2], $aPos[3], 1)
            _GuiHole($hGUI_Grab2AVI, $frame_size, $frame_size, $aPos[2] - 2 * $frame_size, $aPos[3] - 2 * $frame_size, $aPos[2], $aPos[3])
            WinSetOnTop($hGUI_Grab2AVI, 0, 1)
            ToolTip("Press CTRL to start capturing of" & @LF & "marked window to a AVI file!" & @LF & @LF & _
                         "Duration: " & $rec_duration & " seconds @ " & $fps & " fps" & @LF & _
                          "Windows Size: " & $aPos[2] & " x " & $aPos[3], $aMPos[0] + 20, $aMPos[1] + 20)
            $mxo = $aMPos[0]
            $myo = $aMPos[1]
        EndIf
    EndIf
    If _IsPressed("11", $dll) Then
        $esc = False
        ExitLoop
    EndIf
WEnd
ToolTip("")
GUIDelete($hGUI_Grab2AVI)
If $esc Then Exit MsgBox(0, "Information", "Aborted", 10)

_StartAviLibrary()
Global $aAVI = _CreateAvi($sFile, $fps, $aPos[2], $aPos[3])
If @error Then close()

Global $hBmp

Global $fSleep = 1000 / $fps, $t, $td
Global $total_FPS = $rec_duration * $fps, $fps_c = 1
$k32_dll = DllOpen("kernel32.dll")
Do
    $fTimer = TimerInit()

    $hBmp = _ScreenCapture_CaptureWnd("", $hWnd)
    _AddHBitmapToAvi($aAVI, $hBmp)
    _WinAPI_DeleteObject($hBmp)

    $fps_c += 1
    $td = $fSleep - TimerDiff($fTimer)
    If $td > 0 Then
        DllCall($k32_dll, "none", "Sleep", "dword", $td)
    EndIf

    If $fps_c > $total_FPS Or _IsPressed("1B", $dll) Then
        Close()
    EndIf

Until False

Func Close()
    _GDIPlus_Shutdown()
    _CloseAvi($aAVI)
    _StopAviLibrary()
    ConsoleWrite($fps_c & " frames added" & @CRLF)
    ConsoleWrite("AVI filesize: " & Round(FileGetSize($sFile) / 1024 ^ 2, 2) & " MB" & @CRLF)
    DllClose($k32_dll)
    DllClose($dll)
    Exit MsgBox(0, "Information", "Done!", 15)
EndFunc   ;==>close

Func _GuiHole($hWnd, $i_x, $i_y, $i_sizew, $i_sizeh, $width, $height)
    Local $outer_rgn, $inner_rgn, $combined_rgn
    $outer_rgn = _WinAPI_CreateRectRgn(0, 0, $width, $height)
    $inner_rgn = _WinAPI_CreateRectRgn($i_x, $i_y, $i_x + $i_sizew, $i_y + $i_sizeh)
    $combined_rgn = _WinAPI_CreateRectRgn(0, 0, 0, 0)
    _WinAPI_CombineRgn($combined_rgn, $outer_rgn, $inner_rgn, $RGN_DIFF)
    _WinAPI_DeleteObject($outer_rgn)
    _WinAPI_DeleteObject($inner_rgn)
    _WinAPI_SetWindowRgn($hWnd, $combined_rgn)
EndFunc   ;==>_GuiHole

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By UEZ
      Here another approach to check if a script was already started using atoms and semaphores.
       
      Atom:
      #include <MsgBoxConstants.au3> Global $iSingleton = Singleton() If Not $iSingleton Then Exit MsgBox($MB_TOPMOST, "Singleton Test", "Process is already running!") EndIf MsgBox($MB_TOPMOST, "Singleton Test", "Singleton atom initialized: " & $iSingleton) Singleton_Delete($iSingleton) ; #FUNCTION# ==================================================================================================================== ; Name ..........: Singleton ; Description ...: Checks if the script has been started already. ; Syntax ........: Singleton([$sOccurrenceName = @ScriptFullPath]) ; Parameters ....: $sOccurrenceName - [optional] a string value. Default is @ScriptFullPath. ; Return values .: If the function succeeds, the return value is the newly created atom or 0 else error is set and false is returned. ; Author ........: UEZ ; Modified ......: ; Remarks .......: If Singleton finds the atom it will return 0 and the atom token will be set to extended macro. It can be used to get the atom string using _WinAPI_AtomGlobalGetName. ; Related .......: ; Link ..........: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalfindatomw ; https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globaladdatomw ; Example .......: No ; =============================================================================================================================== Func Singleton($sOccurrenceName = @ScriptFullPath) Local $iFind = _WinAPI_AtomGlobalFind($sOccurrenceName) If @error Then Return SetError(1, 0, False) If $iFind Then Return SetExtended($iFind, 0) Local $iAtom = _WinAPI_AtomGlobalAdd($sOccurrenceName) If @error Then Return SetError(2, 0, False) Return $iAtom EndFunc ;==>Singleton ; #FUNCTION# ==================================================================================================================== ; Name ..........: Singleton_Delete ; Description ...: Deletes the atom generated by the first started script. ; Syntax ........: Singleton_Delete($iAtom) ; Parameters ....: $iAtom - an integer value which was generated by Singleton ; Return values .: True if successful else false. ; Author ........: UEZ ; Modified ......: ; Remarks .......: Don't forget to call Singleton_Delete before first started script ends. ; Related .......: ; Link ..........: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globaldeleteatom ; Example .......: No ; =============================================================================================================================== Func Singleton_Delete($iAtom) _WinAPI_AtomGlobalDelete($iAtom) If @error Then Return SetError(1, 0, False) Return True EndFunc ;==>Singleton_Delete ;internal functions Func _WinAPI_AtomGlobalFind($sAtomString) Local $aReturn = DllCall("kernel32.dll", "short", "GlobalFindAtomW", "wstr", $sAtomString) If @error Then Return SetError(1, 0, -1) Return $aReturn[0] EndFunc ;==>_WinAPI_AtomGlobalFind Func _WinAPI_AtomGlobalAdd($sAtomString) Local $aReturn = DllCall("kernel32.dll", "short", "GlobalAddAtomW", "wstr", $sAtomString) If @error Then Return SetError(1, 0, -1) Return $aReturn[0] EndFunc ;==>_WinAPI_AtomGlobalAdd Func _WinAPI_AtomGlobalDelete($nAtom) Local $aReturn = DllCall("kernel32.dll", "short", "GlobalDeleteAtom", "short", $nAtom) If @error Then Return SetError(1, 0, -1) Return $aReturn[0] = 0 EndFunc ;==>_WinAPI_AtomGlobalDelete Func _WinAPI_AtomGlobalGetName($nAtom, $iBufferSize = 512) Local $tBufferAtom = DllStructCreate("wchar name[" & $iBufferSize & "]") Local $aReturn = DllCall("kernel32.dll", "uint", "GlobalGetAtomNameW", "short", $nAtom, "struct*", $tBufferAtom, "int", $iBufferSize) If @error Or Not $aReturn[0] Then Return SetError(1, 0, -1) Return $tBufferAtom.name EndFunc ;==>_WinAPI_AtomGlobalGetName  
      Semaphore:
      #include <MsgBoxConstants.au3> #include <WinAPIError.au3> Global $iSingleton = Singleton("&]8h/x87</htFV4-K*&.b.w~") If Not $iSingleton Then Exit MsgBox($MB_TOPMOST, "Singleton Test", "Process is already running!") EndIf MsgBox($MB_TOPMOST, "Singleton Test", "Singleton Semaphore initialized: " & $iSingleton) ; #FUNCTION# ==================================================================================================================== ; Name ..........: Singleton ; Description ...: Checks if the script has been started already. ; Syntax ........: Singleton($sOccurrenceName) ; Parameters ....: $sOccurrenceName - a string value which will be used to create the semaphore handle. ; Return values .: True if Singleton started the first time. False if script was already started ; Author ........: UEZ ; Modified ......: ; Remarks .......: The system closes the handle automatically when the process terminates. The semaphore object is destroyed when its last handle has been closed. ; Related .......: ; Link ..........: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsemaphorea ; Example .......: No ; =============================================================================================================================== Func Singleton($sOccurrenceName) If StringLen($sOccurrenceName) > 260 Then $sOccurrenceName = StringLeft($sOccurrenceName, 260) Local $aReturn = DllCall("kernel32.dll", "handle", "CreateSemaphoreA", "ptr", Null, "long", 0, "long", 1, "str", $sOccurrenceName) If @error Or Not $aReturn[0] Then Return SetError(1, 0, -1) Return SetExtended($aReturn[0], $aReturn[0] And _WinAPI_GetLastErrorMessage() = "The operation completed successfully.") EndFunc ;==>Singleton  
      Just start the script twice to see if it works.
      The disadvantage of using atoms is that atoms have a memory that means when your app is crashing or you forgot to delete the atom then the atom does still have the $sOccurrenceName saved and thus Singleton will not work if you use the same same value for $sOccurrenceName.
      With semaphore you don't have this issue.
       
      Thanks to jj2007 and SARG.
    • By BlueSkyMemory
      Hello guys! I'm a rookie in AutoIt lol.
      I've tried to looking up in MSDN and the UDFs, but it can only get the GUID of a usual partition and with the GUID to control it. Now I have no ways😥.
      Thanks a lot for your help!
    • By emendelson
      Using some very ingenious scripts that I found on this forum, I've put together a special-purpose folder watcher that watches a specified folder for printer output files and then either send them to a printer or converts them to a PDF and prints or opens the PDF. The page about this utility is here:
      http://www.columbia.edu/~em36/printfileprinter.html
      The one serious problem that it has is that it seems not to detect a new file if there are more than two or three files already in the watched folder. I can't figure out what I'm doing wrong, and will be grateful for any help. Here is the relevant part of the script. I've left out the functions that test whether the file is in use or not, and that send the raw data to the printer or create a PDF, etc. I hope there's enough code here to make sense of it, and will be very grateful for any help. Again, the problem is that the script doesn't detect newly-created files in watched folders with more than a very few files already in it.
      My totally ignorant guess is that the problem is in the line $iID = _WinAPI_WaitForMultipleObjects(2, $paObj, 0, 0) - but I don't know how to change it and of course I'm only guessing whether it's relevant or not. Many thanks
       
      Global $g_ahObj[2] $g_ahObj[0] = _WinAPI_FindFirstChangeNotification($watchPath, $FILE_NOTIFY_CHANGE_FILE_NAME) $g_ahObj[1] = _WinAPI_FindFirstChangeNotification($watchPath, $FILE_NOTIFY_CHANGE_DIR_NAME) If (Not $g_ahObj[0]) Or (Not $g_ahObj[1]) Then MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Error', 'Unable to create change notification.') Exit EndIf Local $tObjs = DllStructCreate('ptr;ptr') Local $paObj = DllStructGetPtr($tObjs) For $i = 0 To 1 DllStructSetData($tObjs, $i + 1, $g_ahObj[$i]) Next Local $iID While 1 Sleep(100) $select = 0 $print = 0 $format = "" Local $tempPDF $tempPDF = 0 $iID = _WinAPI_WaitForMultipleObjects(2, $paObj, 0, 0) Switch $iID Case 0 ; WAIT_OBJECT_0 ; ConsoleWrite('A file was created, renamed, or deleted in the directory.' & @CRLF) Local $hSearch = FileFindFirstFile($watchPath & "\*") Local $sFileName = "", $iResult = 0 Local $sFilePath = "" While 1 $sFileName = FileFindNextFile($hSearch) ; If there is no more file matching the search. If @error Then ExitLoop $sFilePath = $watchPath & "\" & $sFileName Local $fileUsed $fileUsed = 0 $fileUsed = _FileIsUsed($sFilePath) If $fileUsed = 1 Then While 1 Sleep(100) $fileUsed = _FileIsUsed($sFilePath) If $fileUsed = 0 Then ExitLoop WEnd EndIf If StringInStr($sFileName, "raw") Then If StringInStr($sFileName, "select") Then $select = 1 PrintRawFile($sFilePath, $select) Else If StringInStr($sFileName, "lg.") Then $pageSize = "legal" ElseIf StringInStr($sFileName, "a4.") Then $pageSize = "a4" ElseIf StringInStr($sFileName, "us.") Then $pageSize = "letter" EndIf $pdfTemp = 0 If StringLower(StringLeft($sFileName, 7) = "pdftemp") Then $pdfTemp = 1 If StringInStr($sFileName, ".pcl") Then $format = "pcl" $print = 1 If StringLeft($sFileName, 6) = "select" Then $select = 1 $print = 1 ElseIf StringLeft($sFileName, 3) = "pdf" Then $select = 0 $print = 0 EndIf MakePDF($sFilePath, $format, $print, $select, $pageSize, $pdfTemp) ElseIf StringInStr($sFileName, ".ps") Then $format = "ps" $print = 1 If StringLeft($sFileName, 6) = "select" Then $select = 1 $print = 1 ElseIf StringLeft($sFileName, 3) = "pdf" Then $select = 0 $print = 0 EndIf MakePDF($sFilePath, $format, $print, $select, $pageSize, $pdfTemp) ElseIf StringInStr($sFileName, ".esc") Then Sleep(200) $format = "epson" $print = 1 If StringLeft($sFileName, 6) = "select" Then $select = 1 $print = 1 ElseIf StringLeft($sFileName, 3) = "pdf" Then $select = 0 $print = 0 EndIf MakePDF($sFilePath, $format, $print, $select, $pageSize, $pdfTemp) ElseIf StringInStr($sFileName, ".prn") Then $format = GetFileFormat($sFilePath) $print = 1 If StringLeft($sFileName, 6) = "select" Then $select = 1 $print = 1 ElseIf StringLeft($sFileName, 3) = "pdf" Then $select = 0 $print = 0 EndIf ConsoleWrite("PRN test format: " & $format & @CRLF) MakePDF($sFilePath, $format, $print, $select, $pageSize, $pdfTemp) EndIf EndIf ; Display the file name. ; $iResult = MsgBox(BitOR($MB_SYSTEMMODAL, $MB_OKCANCEL), "", $watchPath & "\" & $sFileName) ;~ RunWait(@ComSpec & " /c notepad.exe " & $watchPath & "\" & $sFileName) ;~ FileDelete($watchPath & "\" & $sFileName) If $iResult <> $IDOK Then ExitLoop ; If the user clicks on the cancel/close button. WEnd ; Close the search handle. FileClose($hSearch) Case 1 ; WAIT_OBJECT_0 + 1 ; ConsoleWrite('A directory was created, renamed, or deleted.' & @CRLF) Case Else ContinueLoop EndSwitch If Not _WinAPI_FindNextChangeNotification($g_ahObj[$iID]) Then MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Error', 'Unexpected error.') Exit EndIf WEnd  
    • By xYuri
      This simple dllcall gives me error 5, access denied,
      Func _WinAPI_VkKeyScan($__key) _WinAPI_SetLastError(0) $res = DllCall('User32.dll', 'SHORT', 'VkKeyScan', 'CHAR', $__key) _xConsole('res: '&$res) $_LastErr = _WinAPI_GetLastError() If $_LastErr <> 0 Then _xConsole('Err: {' & $_LastErr & '}> ' & _WinAPI_GetLastErrorMessage()) Return $res EndFunc Am i doing something wrong?
      Also tried VkKeyScanA and W
      Edit:
      I want to send `:` via PostMessage() WM_KEYDOWN
    • By Sori
      Not going to dump the code down because it's essentially a keylogger.
      <snip>
×
×
  • Create New...