Leaderboard
Popular Content
Showing content with the highest reputation on 03/30/2022 in all areas
-
Jon's text-encoding-detect
Musashi and one other reacted to pixelsearch for a topic
Hi everybody A few years ago, @Jon made public his C# and C++ code to detect the encoding of a text file, in this link. It's the detection method used by AutoIt (thanks to @TheDcoder for providing the link a few days ago) As I'm a C++ newbie, I focused on the interesting detection functions, trying to understand them, then compiled the C++ code (to an exe file) and tested it on a few files. Everything worked fine and the files I tested had their content correctly detected, but (if i'm not wrong) the code works, by design, for 1 file only : int wmain(int argc, wchar_t* argv[]) { if (argc != 2) Return 1 ... So I thought : why not trying to compile the code as a dll, allowing us to check all files from a chosen folder, by running an AutoIt script that calls the dll ? Jon's C++ code is mainly composed of 3 parts, named : 1) text_encoding_detect.h 2) text_encoding_detect.cpp 3) main.cpp Below is main.cpp, reworked for 2 reasons : * it has to be compiled as a dll (and no more as an exe) * the string messages indicating the detection result have all been replaced by integer values, to avoid string returns from the dll (as discussed yesterday in another thread). No worry, the string messages will all be displayed in AutoIt. The code may look a bit messy because I kept the (now) unused original lines, commented out, so everybody can see what has been altered in main.cpp : // // Copyright 2015-2016 Jonathan Bennett <jon@autoitscript.com> // // https://www.autoitscript.com // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #include <stdio.h> #include <tchar.h> #include "text_encoding_detect.h" using namespace AutoIt::Common; // int wmain(int argc, wchar_t* argv[]) extern "C" __declspec(dllexport) int text_encoding_detect (wchar_t* sfilename) { /* if (argc != 2) { wprintf(L"\nUsage: %s filename.", argv[0]); // comment out all wprintf lines return 1; } */ // Open file in binary mode // FILE *file = _wfopen(argv[1], L"rb"); FILE *file = _wfopen(sfilename, L"rb"); if (file == NULL) { // wprintf(L"\nCould not open file.\n"); return 1; } // Get file size fseek(file, 0, SEEK_END); long fsize = ftell(file); fseek(file, 0, SEEK_SET); // Read it all in unsigned char *buffer = new unsigned char[fsize]; fread(buffer, fsize, 1, file); fclose(file); // Detect the encoding TextEncodingDetect textDetect; TextEncodingDetect::Encoding encoding = textDetect.DetectEncoding(buffer, fsize); int iret = 0; // added line. Also added all following iret lines // wprintf(L"\nEncoding: "); if (encoding == TextEncodingDetect::None) // wprintf(L"Binary"); iret = 10; else if (encoding == TextEncodingDetect::ASCII) // wprintf(L"ASCII (chars in the 0-127 range)"); iret = 11; else if (encoding == TextEncodingDetect::ANSI) // wprintf(L"ANSI (chars in the range 0-255 range)"); iret = 12; else if (encoding == TextEncodingDetect::UTF8_BOM || encoding == TextEncodingDetect::UTF8_NOBOM) // wprintf(L"UTF-8"); iret = 13; else if (encoding == TextEncodingDetect::UTF16_LE_BOM || encoding == TextEncodingDetect::UTF16_LE_NOBOM) // wprintf(L"UTF-16 Little Endian"); iret = 14; else if (encoding == TextEncodingDetect::UTF16_BE_BOM || encoding == TextEncodingDetect::UTF16_BE_NOBOM) // wprintf(L"UTF-16 Big Endian"); iret = 15; // Free up delete[] buffer; // return 0; return iret; } AutoIt code for using the dll : #include <Array.au3> #include <File.au3> #include <MsgBoxConstants.au3> Opt("MustDeclareVars", 1) Local $hDLL = DllOpen(@ScriptDir & "\text_encoding_detect.dll") If $hDLL = - 1 Then Exit Msgbox($MB_TOPMOST, "DllOpen", "error occured") Global $bAbort = False ; user can press Esc to abort while detecting (wrong folder ?) Local $sPath = FileSelectFolder("Text encoding detect : choose folder", "", $FSF_NEWDIALOG) If @error Then ; MsgBox(BitOr($MB_TOPMOST, $MB_ICONWARNING), "Error", _ ; "No folder selected ") ; Cancel, Red X or Esc key in FileSelectFolder() DllClose($hDLL) Exit Else If StringRight($sPath, 1) <> "\" Then $sPath &= "\" ; "C:\" => "C:\" "C:\Temp" => "C:\Temp\" EndIf EndIf Local $aFileList = _FileListToArray($sPath, Default, $FLTA_FILES) ; return files only Local $iKeep_error = @error, $sMsg = "" Switch $iKeep_error Case 0 ; no error (and at least 1 file found because of non-error 4) Case 1 $sMsg = "Invalid Folder" Case 2 $sMsg = "Invalid Filter" Case 3 $sMsg = "Invalid Flag" Case 4 $sMsg = "No File(s) Found" Case Else $sMsg = "Not documented" EndSwitch If $iKeep_error Then MsgBox($MB_TOPMOST, "_FileListToArray", _ "Path : " & $sPath & @crlf & _ "error #" & $iKeep_error & " : " & $sMsg) DllClose($hDLL) Exit EndIf ; _ArrayDisplay($aFileList, "$aFileList") Local $sFile, $aRet, $iTotFiles = $aFileList[0], $aResult[$iTotFiles][2] HotKeySet("{ESC}", "_Abort") For $iInc = 1 To $iTotFiles _SplashOn("File : " & $iInc & " / " & $iTotFiles) $sFile = $sPath & $aFileList[$iInc] $aRet = DllCall($hDLL, "int:cdecl", "text_encoding_detect", "wstr", $sFile) If @error Then $iKeep_error = @error HotKeySet("{ESC}") SplashOff() Msgbox($MB_TOPMOST, "DllCall", "error " & $iKeep_error & @crlf & $sFile) DllClose($hDLL) Exit EndIf Switch $aRet[0] ; Jon's string messages from C++ main.ccp Case 1 $sMsg = "Could not open file" Case 10 $sMsg = "Binary" Case 11 $sMsg = "ASCII (chars in the 0-127 range)" Case 12 $sMsg = "ANSI (chars in the range 0-255 range)" Case 13 $sMsg = "UTF-8" Case 14 $sMsg = "UTF-16 Little Endian" Case 15 $sMsg = "UTF-16 Big Endian" Case Else $sMsg = "Not documented" EndSwitch $aResult[$iInc - 1][0] = $aFileList[$iInc] $aResult[$iInc - 1][1] = $sMsg If $bAbort Then ; user keyed Esc Redim $aResult[$iInc][2] ExitLoop EndIf Next HotKeySet("{ESC}") SplashOff() _ArrayDisplay($aResult, ($bAbort ? "PARTIAL - " : "") & $sPath, _ Default, Default, Default, "File name|Encoding") DllClose($hDLL) ;========================================================== Func _Abort() HotKeySet("{ESC}") $bAbort = True EndFunc ; ==>_Abort ;========================================================== Func _SplashOn($sFirstLine, $sSecondLine = "please wait...") SplashTextOn("", $sFirstLine & @CRLF & $sSecondLine, _ 250, 50, -1, -1, $DLG_NOTITLE + $DLG_TEXTVCENTER) EndFunc ;==>_SplashOn 2nd file name is a non-ANSI name correctly passed to the dll : "b" & ChrW(1034) A few ideas I'll probably work on : * add a file extension column in the final ArrayDisplay (it will allow to sort by extension) * add another column in ArrayDisplay to indicate the position (offset) in the file where the detection has been made. The goal is not to upload a dll here, but if Jon or an AutoIt developer think it may interest users, then the decision is theirs to compile and upload it as a dll, especially their compilers are certainly better than the free one I use. Meanwhile, any interested user having the tools to compile could be interested too. If you guys think something should be corrected in the script, please indicate it in this thread. Thanks2 points -
AutoIt v3.3.16.0 has been released. Thanks to @jpm and the MVPs who were responsible for the majority of code in this version. Download it here. Complete list of changes: History2 points
-
Moved to the appropriate forum. This is now the fourth one we had to move for you, so please simply post in the Support forum when you have an Autoit3 question... can't really be that hard now can it? Moderation Team1 point
-
Info Chrome automation - (Moved)
MarcoMonte reacted to Nine for a topic
Yes this is the best UDF to automate Chrome. When you download the UDF, there is a Help File. But more importantly, you will get the demo.au3 file to learn all the basics. There are many useful examples in the demo that can serve to accelerate the learning. Look also at the AutoIt Wiki for the how-to. In case of an issue, post code here, someone will help you.1 point -
Set Volume for another Program
Floooooo24 reacted to ad777 for a topic
@Floooooo24 this should help ya:1 point -
I am very glad to say that I found the reason: a debug DLL is not supposed to run without Visual Studio. So here I attach the release builds of both DLLs, they are tested with a new machine, and the size is much smaller. 😄 NewHook.zip1 point
-
Understanding / Learning - C#
Skeletor reacted to Noviceatthis for a topic
Sorry, I'm a little late here, I agree with Skeletor, and if you want a specific resource I found to be quite useful, it would be this guy: https://www.udemy.com/user/moshfeghhamedani/ (although you have to pay for that, also prices on Udemy are weird and fluctuate wildly, one day a course can be £10, and the next it can be £100, so don't get ripped off). Generally, when I'm trying to learn a new language, I'll set myself a challenge. One I quite like doing is consuming some sort of API, and displaying the information in some way, for example use this: https://www.tvmaze.com/api to fetch some information (based on some sort of input, can be from the console, or from some sort of UI if you're feeling fancy) about a TV show and display it in some way.1 point -
Control *Application* Sound Volume?
Floooooo24 reacted to Aelc for a topic
Here you go - tested with WIN7 and AIMP special thanks goes to DanyfireX for the sounddetect code #include-once ;https://www.autoitscript.com/forum/topic/174460-is-there-a-way-to-detect-any-sound-file-played/ ;Special Thanks to Danyfirex 02/08/2015 #include <WinAPIProc.au3> Global Const $sCLSID_MMDeviceEnumerator = "{BCDE0395-E52F-467C-8E3D-C4579291692E}" Global Const $sIID_IMMDeviceEnumerator = "{A95664D2-9614-4F35-A746-DE8DB63617E6}" Global Const $sTagIMMDeviceEnumerator = _ "EnumAudioEndpoints hresult(int;dword;ptr*);" & _ "GetDefaultAudioEndpoint hresult(int;int;ptr*);" & _ "GetDevice hresult(wstr;ptr*);" & _ "RegisterEndpointNotificationCallback hresult(ptr);" & _ "UnregisterEndpointNotificationCallback hresult(ptr)" Global Const $sIID_IMMDevice = "{D666063F-1587-4E43-81F1-B948E807363F}" Global Const $sTagIMMDevice = _ "Activate hresult(struct*;dword;ptr;ptr*);" & _ "OpenPropertyStore hresult(dword;ptr*);" & _ "GetId hresult(wstr*);" & _ "GetState hresult(dword*)" Global Const $sIID_IAudioSessionManager2 = "{77aa99a0-1bd6-484f-8bc7-2c654c9a9b6f}" Global Const $sTagIAudioSessionManager = "GetAudioSessionControl hresult(ptr;dword;ptr*);" & _ "GetSimpleAudioVolume hresult(ptr;dword;ptr*);" Global Const $sTagIAudioSessionManager2 = $sTagIAudioSessionManager & "GetSessionEnumerator hresult(ptr*);" & _ "RegisterSessionNotification hresult(ptr);" & _ "UnregisterSessionNotification hresult(ptr);" & _ "RegisterDuckNotification hresult(wstr;ptr);" & _ "UnregisterDuckNotification hresult(ptr)" Global Const $sIID_IAudioSessionEnumerator = "{e2f5bb11-0570-40ca-acdd-3aa01277dee8}" Global Const $sTagIAudioSessionEnumerator = "GetCount hresult(int*);GetSession hresult(int;ptr*)" Global Const $sIID_IAudioSessionControl = "{f4b1a599-7266-4319-a8ca-e70acb11e8cd}" Global Const $sTagIAudioSessionControl = "GetState hresult(int*);GetDisplayName hresult(ptr);" & _ "SetDisplayName hresult(wstr);GetIconPath hresult(ptr);" & _ "SetIconPath hresult(wstr;ptr);GetGroupingParam hresult(ptr*);" & _ "SetGroupingParam hresult(ptr;ptr);RegisterAudioSessionNotification hresult(ptr);" & _ "UnregisterAudioSessionNotification hresult(ptr);" Global Const $sIID_IAudioSessionControl2 = "{bfb7ff88-7239-4fc9-8fa2-07c950be9c6d}" Global Const $sTagIAudioSessionControl2 = $sTagIAudioSessionControl & "GetSessionIdentifier hresult(ptr)" & _ "GetSessionInstanceIdentifier hresult(ptr);" & _ "GetProcessId hresult(dword*);IsSystemSoundsSession hresult();" & _ "SetDuckingPreferences hresult(bool);" ; http://answers.awesomium.com/questions/3398/controlling-the-sound-using-pinvoke-the-volume-mix.html Global Const $sIID_ISimpleAudioVolume = "{87CE5498-68D6-44E5-9215-6DA47EF883D8}" Global Const $sTagISimpleAudioVolume = _ "SetMasterVolume hresult(float;ptr);" & _ "GetMasterVolume hresult(float*);" & _ "SetMute hresult(int;ptr);" & _ "GetMute hresult(int*)" Global $__oIAudioSessionManager2 $SoundAPP = "AIMP.exe" $newValue = Random ( 1,100,1 ) MsgBox ( 64,"","new random value: " & $newValue, 1 ) Local $CurrentVolume = Audio_GetMasterVolume(_GetProcessID_by_name($SoundAPP)) * 100 If $CurrentVolume <> $newValue And $CurrentVolume > $newValue Then For $i = $CurrentVolume To $newValue Step -1 Audio_SetMasterVolume(_GetProcessID_by_name($SoundAPP), $i / 100) Sleep(30) Next Else For $i = $CurrentVolume To $newValue Step +1 Audio_SetMasterVolume(_GetProcessID_by_name($SoundAPP), $i / 100) Sleep(30) Next EndIf Func _GetProcessID_by_name($process_name) Local $Process_list = ProcessList() Local $ProcessID_Func = 0 For $i = 1 To UBound($Process_list) - 1 If $Process_list[$i][0] = $process_name Then $ProcessID_Func = $Process_list[$i][1] ExitLoop EndIf Next Return $ProcessID_Func EndFunc ;==>_SoundAPP_setVolume Func Audio_SetMasterVolume($pid, $value) If Not IsObj($__oIAudioSessionManager2) Then $__oIAudioSessionManager2 = Audio_GetIAudioSessionManager2() EndIf If Not IsObj($__oIAudioSessionManager2) Then Return Local $pIAudioSessionEnumerator, $oIAudioSessionEnumerator If $__oIAudioSessionManager2.GetSessionEnumerator($pIAudioSessionEnumerator) < 0 Then Return $oIAudioSessionEnumerator = ObjCreateInterface($pIAudioSessionEnumerator, $sIID_IAudioSessionEnumerator, $sTagIAudioSessionEnumerator) If Not IsObj($oIAudioSessionEnumerator) Then Return SetError(1) Local $i, $nSessions, $pIAudioSessionControl2, $oIAudioSessionControl2 Local $ProcessID, $oISimpleAudioVolume Local $bMute = 0, $error = 1 If $oIAudioSessionEnumerator.GetCount($nSessions) >= 0 Then For $i = 0 To $nSessions - 1 If $oIAudioSessionEnumerator.GetSession($i, $pIAudioSessionControl2) >= 0 Then $oIAudioSessionControl2 = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioSessionControl2, $sTagIAudioSessionControl2) If @error Then ContinueLoop $oIAudioSessionControl2.GetProcessId($ProcessID) If $ProcessID = $pid Then $oISimpleAudioVolume = ObjCreateInterface($pIAudioSessionControl2, $sIID_ISimpleAudioVolume, $sTagISimpleAudioVolume) If @error Then ContinueLoop $oIAudioSessionControl2.AddRef() ;stabilize If $oISimpleAudioVolume.SetMasterVolume($value, 0) >= 0 Then $error = 0 ExitLoop EndIf EndIf EndIf Next EndIf $oISimpleAudioVolume = 0 $oIAudioSessionControl2 = 0 $oIAudioSessionEnumerator = 0 ;MsgBox(0, $error, "App muted: " & $bMute) Return SetError($error, 0, $bMute) EndFunc ;==>Audio_SetMasterVolume Func Audio_GetMasterVolume($pid) If Not IsObj($__oIAudioSessionManager2) Then $__oIAudioSessionManager2 = Audio_GetIAudioSessionManager2() EndIf If Not IsObj($__oIAudioSessionManager2) Then Return Local $pIAudioSessionEnumerator, $oIAudioSessionEnumerator If $__oIAudioSessionManager2.GetSessionEnumerator($pIAudioSessionEnumerator) < 0 Then Return $oIAudioSessionEnumerator = ObjCreateInterface($pIAudioSessionEnumerator, $sIID_IAudioSessionEnumerator, $sTagIAudioSessionEnumerator) If Not IsObj($oIAudioSessionEnumerator) Then Return SetError(1) Local $i, $nSessions, $pIAudioSessionControl2, $oIAudioSessionControl2 Local $ProcessID, $oISimpleAudioVolume Local $bMute = 0, $error = 1 If $oIAudioSessionEnumerator.GetCount($nSessions) >= 0 Then For $i = 0 To $nSessions - 1 If $oIAudioSessionEnumerator.GetSession($i, $pIAudioSessionControl2) >= 0 Then $oIAudioSessionControl2 = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioSessionControl2, $sTagIAudioSessionControl2) If @error Then ContinueLoop $oIAudioSessionControl2.GetProcessId($ProcessID) If $ProcessID = $pid Then $oISimpleAudioVolume = ObjCreateInterface($pIAudioSessionControl2, $sIID_ISimpleAudioVolume, $sTagISimpleAudioVolume) If @error Then ContinueLoop $oIAudioSessionControl2.AddRef() ;stabilize If $oISimpleAudioVolume.GetMasterVolume($bMute) >= 0 Then $error = 0 ExitLoop EndIf EndIf EndIf Next EndIf $oISimpleAudioVolume = 0 $oIAudioSessionControl2 = 0 $oIAudioSessionEnumerator = 0 ;MsgBox(0, $error, "App muted: " & $bMute) Return SetError($error, 0, $bMute) EndFunc ;==>Audio_GetMasterVolume Func Audio_GetIAudioSessionManager2() Local $oIAudioSessionManager2 = 0 Local Const $eMultimedia = 1, $CLSCTX_INPROC_SERVER = 0x01 Local $pIMMDevice, $oMMDevice, $pIAudioSessionManager2 Local $oMMDeviceEnumerator = ObjCreateInterface($sCLSID_MMDeviceEnumerator, $sIID_IMMDeviceEnumerator, $sTagIMMDeviceEnumerator) If IsObj($oMMDeviceEnumerator) Then If $oMMDeviceEnumerator.GetDefaultAudioEndpoint(0, $eMultimedia, $pIMMDevice) >= 0 Then $oMMDevice = ObjCreateInterface($pIMMDevice, $sIID_IMMDevice, $sTagIMMDevice) If IsObj($oMMDevice) Then If $oMMDevice.Activate(__uuidof($sIID_IAudioSessionManager2), $CLSCTX_INPROC_SERVER, 0, $pIAudioSessionManager2) >= 0 Then $oIAudioSessionManager2 = ObjCreateInterface($pIAudioSessionManager2, $sIID_IAudioSessionManager2, $sTagIAudioSessionManager2) EndIf $oMMDevice = 0 EndIf EndIf $oMMDeviceEnumerator = 0 EndIf If IsObj($oIAudioSessionManager2) Then Return $oIAudioSessionManager2 EndIf EndFunc ;==>Audio_GetIAudioSessionManager2 Func __uuidof($sGUID) Local $tGUID = DllStructCreate("ulong Data1;ushort Data2;ushort Data3;byte Data4[8]") DllCall("ole32.dll", "long", "CLSIDFromString", "wstr", $sGUID, "struct*", $tGUID) If @error Then Return SetError(@error, @extended, 0) Return $tGUID EndFunc ;==>__uuidof Func CLSIDFromString($sGUID) Local $tGUID = DllStructCreate("ulong Data1;ushort Data2;ushort Data3;byte Data4[8]") DllCall("ole32.dll", "long", "CLSIDFromString", "wstr", $sGUID, "struct*", $tGUID) Return $tGUID EndFunc ;==>CLSIDFromString Had fun on it so I inserted a fading and it's actually a random value I don't know why but actually SoundSetWaveVolume() doesn't work... So i have to set the Master Volume up to 100 because the device volume compare to it :/ If you want it like the SoundSetWaveVolume() just make a func of it with $soundAPP and $newValue as Parameter and delete that like the msgbox.1 point -
Firstly don't necro old threads, secondly search the forum for WinAPIEx and the function _WinAPI_EmptyWorkingSet. Case closed.1 point
-
It's unlikely that more and more memory will get used. I have a script which loops continually. It performs various jobs in response to hotkeys during the day and every night it makes backups of various files. I rarely turn my computer off and it runs continually for months at a time with no hint of any memory problems.1 point