Automatic execution of a program

Just starting out? Need help? Post your questions and find answers here.
jak64
Enthusiast
Enthusiast
Posts: 625
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Automatic execution of a program

Post by jak64 »

Hello,
I wrote a program in Purebasic which manages an Sqlite database containing all the titles of my films. I put this program (.exe) on an external drive which also contains all my movies.

I would like, when I plug the external drive into a USB socket, for this program to launch automatically.

I know you can set autorun but I disabled it because I don't want autorun for other disks and other devices.

I would like autorun to only work on my external drive containing my program and movies.


Is it possible ?

Thank you for your help.
ZX80
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Dec 12, 2016 1:37 pm

Re: Automatic execution of a program

Post by ZX80 »

Hi, jak64.

I think it's possible, but you have to understand that the program that monitors usb connections must be running all the time. Watch dog in the background. Does it suit you?
The easiest way to determine that the disk that was connected is exactly the one you need is to look for some kind of label file. Or you can determine it by the characteristics of the disk.
AZJIO
Addict
Addict
Posts: 2175
Joined: Sun May 14, 2017 1:48 am

Re: Automatic execution of a program

Post by AZJIO »

Code: Select all

EnableExplicit

#Windows_0 = 0
#SysTrayIcon_0 = 0
#Menu = 0

Enumeration
	#About
	#Exit
EndEnumeration


Global ExeDir$, hWin_0, drives_avail, Letters$, MarkFileName$
Global StartDisk = 2

; Executable file (disk marker file)
MarkFileName$ = "Start.exe"

ExeDir$ = GetCurrentDirectory()

; here you need a code to prevent the program from restarting
Define *a = CreateSemaphore_(#Null, 0, 1, "USB5671230")
If *a And GetLastError_() = #ERROR_ALREADY_EXISTS
	CloseHandle_(*a)
; 	MessageRequester("","The program is already running") ; optional, but you can report that the program is already running
	End
EndIf

Procedure Exit()
	RemoveSysTrayIcon(#SysTrayIcon_0)
	CloseWindow(#Windows_0)
	End
EndProcedure


Procedure Add_drive(Mask.l)
	Protected i, CurLetter$
	For i = StartDisk To 25
		If ((Mask >> i) & 1) ; check each flag
			CurLetter$ = Chr(i + 65)
; 			Reaction to marker file
			If FileSize(CurLetter$ + ":\" + MarkFileName$) >= 0
				RunProgram(CurLetter$ + ":\" + MarkFileName$)
			EndIf
			Letters$ + CurLetter$
		EndIf
	Next
	Debug Letters$
EndProcedure

Procedure Del_drive(Mask.l)
	Protected k, Count, CurLetter$, title.s;, z
	Count = Len(Letters$)
	For k = Count To 1 Step -1
		CurLetter$ = Mid(Letters$, k, 1)
		If (Mask >> (Asc(CurLetter$) - 65)) & 1
			Letters$ = ReplaceString(Letters$, CurLetter$, "", #PB_String_NoCase, k, 1)
; 			RunProgram("cmd.exe", "/c (taskkill /im " + MarkFileName$ + ")", "")
		EndIf
	Next
	Debug Letters$
EndProcedure

Procedure WinCallback(hWnd, uMsg, wParam, lParam)
	Protected Result = #PB_ProcessPureBasicEvents, Mask, Drive.s,  *pDBHDR.DEV_BROADCAST_HDR, *pDBV.DEV_BROADCAST_VOLUME
	Protected tmp
	Select uMsg
; 		Case #WM_UNINITMENUPOPUP
; 			Debug "PopupMenu был закрыт."
	
		Case #WM_DEVICECHANGE ; Change when connecting external drives.
			Result = #True
			Select wParam
				Case #DBT_DEVICEARRIVAL, #DBT_DEVICEREMOVECOMPLETE
					*pDBHDR.DEV_BROADCAST_HDR=lParam
					If *pDBHDR\dbch_devicetype = #DBT_DEVTYP_VOLUME
						*pDBV.DEV_BROADCAST_VOLUME=lParam
						Mask = *pDBV\dbcv_unitmask

						Select wParam
							Case #DBT_DEVICEARRIVAL
								; Debug Bin(drives_avail)
								; Debug Bin(Mask)
								tmp = drives_avail
								drives_avail | Mask
								If tmp <> drives_avail
									Add_drive(Mask)
								EndIf
							Case #DBT_DEVICEREMOVECOMPLETE
								drives_avail ! (Mask & drives_avail)
								Del_drive(Mask)
						EndSelect
					EndIf
			EndSelect
	EndSelect
	ProcedureReturn Result
EndProcedure

hWin_0 = OpenWindow(#Windows_0, 0, 0, 200, 20, "", #PB_Window_Invisible)

AddSysTrayIcon(#SysTrayIcon_0, hWin_0, GetClassLongPtr_(hWin_0, #GCL_HICON))
; AddSysTrayIcon(#SysTrayIcon_0, WindowID(0), LoadImage(0, ExeDir$ + "2.ico"))
SysTrayIconToolTip(#SysTrayIcon_0, "Surveillance")

CreatePopupMenu(#Menu)
MenuItem(#Exit, "Exit")

BindMenuEvent(#Menu, #Exit, @Exit())
SetWindowCallback(@WinCallback())


Repeat
	Select WaitWindowEvent()
		Case #PB_Event_SysTray
			Select EventType()
				Case #PB_EventType_RightClick
					DisplayPopupMenu(#Menu, hWin_0)
			EndSelect
		Case #PB_Event_CloseWindow
			Exit()
	EndSelect
ForEver
Exit()

zikitrake
Addict
Addict
Posts: 869
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Re: Automatic execution of a program

Post by zikitrake »

Here are some code for this request https://www.purebasic.fr/english/viewto ... 13&t=72177
PB 6.21 beta, PureVision User
TassyJim
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 16, 2013 6:27 am
Location: Tasmania (Australia)

Re: Automatic execution of a program

Post by TassyJim »

autorun was disabled for all USB devices in Windows 7 and later.
There is autoplay but that won't help you.

I don't think there is any way of getting autorun functioning again.
You used to need a autorun.inf file in the root directory on the device.
AZJIO
Addict
Addict
Posts: 2175
Joined: Sun May 14, 2017 1:48 am

Re: Automatic execution of a program

Post by AZJIO »

jak64 wrote: Wed Feb 16, 2022 3:45 pm I would like autorun to only work on my external drive containing my program and movies.
Many virus writers would like this to work only on their disk. At first I thought that you needed it for yourself, but you jumped into another topic and wrote there that it was needed for other people's computers. This behavior is a virus. You want to control the user's computer without the user's knowledge. I understand that your friend knows about this, but the function opens a big hole in the security of other people. It's like you're asking to find a hole in the computer's security in order to perform your actions. The response of the operating system manufacturer will be negative. And also other people have no reason to trust you. You can solve the problem in the permitted way: ask a friend to open the disk and run the executable file.
jak64
Enthusiast
Enthusiast
Posts: 625
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Automatic execution of a program

Post by jak64 »

Hello,
Thank you all for all your responses. I was asking this question because these are cartoons for children and I wanted the program to open on its own and display the list of cartoons in large print on the screen. Children just had to click on the movie they wanted to watch and it would start playing right away.
AZJIO
Addict
Addict
Posts: 2175
Joined: Sun May 14, 2017 1:48 am

Re: Automatic execution of a program

Post by AZJIO »

jak64
Compile the code I gave you above and put the executable on a flash drive. Ask a friend to copy the program to his computer and add it to startup. Now children will be able to connect a flash drive with your cartoons.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Automatic execution of a program

Post by Kwai chang caine »

Nice code AZJIO, works perfectly here 8)
Even for a big baby like me :mrgreen:
Image
Thanks for sharing 8) and also at Jack64 for ask the question :wink:
ImageThe happiness is a road...
Not a destination
Post Reply