Opening a file using my program from windows explorer?
Opening a file using my program from windows explorer?
If I make a program and want to use it as the default program (like an image viewer for image files), how can I retrieve information for the program to use such as the path and file name for the file to use? I tried searching around but maybe I'm using bad keywords.. Thanks
Re: Opening a file using my program from windows explorer?
It would depend on how the calling program passes the data but you would retrieve it via
cheers
Code: Select all
ProgramParameter
Re: Opening a file using my program from windows explorer?
Very cool that's exactly what I needed.
Now I have an issue where if I open multiple files, it will open multiple instances of the program instead of passing the parameter to the original program? How would I solve this?
I'm opening from windows explorer so the only parameter that's passed it seems is the full path with the name of the file that was clicked.
Now I have an issue where if I open multiple files, it will open multiple instances of the program instead of passing the parameter to the original program? How would I solve this?
I'm opening from windows explorer so the only parameter that's passed it seems is the full path with the name of the file that was clicked.
Re: Opening a file using my program from windows explorer?
You can set a mutex and see if your program is already running and if so, use the one already running.
see http://www.purebasic.fr/english/viewtopic.php?t=27372
see http://www.purebasic.fr/english/viewtopic.php?t=27372
Re: Opening a file using my program from windows explorer?
This code seems to work except the part where it passes the parameters to the original window, I'm probably using sendMessage_() incorrectly. It does find another instance and close itself correctly though.
Code: Select all
String.s = ProgramParameter()
FirstInstance = FindWindow_(0,"test program")
If FirstInstance
SendMessage_(FirstInstance,String,0,Len(String)) ;-Something is wrong here
SetForegroundWindow_(FirstInstance)
End
EndIf
Re: Opening a file using my program from windows explorer?
Try
Code: Select all
String.s = ProgramParameter()
FirstInstance = FindWindow_(0,"test program")
If FirstInstance
SendMessage_(FirstInstance,@String,0,Len(String)) ;-Something is wrong here
SetForegroundWindow_(FirstInstance)
End
EndIf
Re: Opening a file using my program from windows explorer?
If the passed parameter(s) contain spaces you'll likely have to pass your string as
Code: Select all
#DQUOTE$ + string + #DQUOTE$
-
- Addict
- Posts: 1482
- Joined: Tue Feb 22, 2011 1:16 pm
Re: Opening a file using my program from windows explorer?
FindWindow is not the way to do it. Search for the forum for why, but basically it's because the window title can change without you wanting it too.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
PureBasic: Born in 1998 and still going strong to this very day!
Re: Opening a file using my program from windows explorer?
This code did not work for me unfortunately My program finds the window OK because the second instance terminates if it's run more than once. But the first instance doesn't receive the message. even SetForegroundWindow_(FirstInstance) worked because the window comes to front if i run the code twice. I did make sure I used the because there were spaces in the string to send.rsts wrote:Try
Code: Select all
String.s = ProgramParameter() FirstInstance = FindWindow_(0,"test program") If FirstInstance SendMessage_(FirstInstance,@String,0,Len(String)) ;-Something is wrong here SetForegroundWindow_(FirstInstance) End EndIf
I guess it would work if I'm using it only for my own program right? I just have to make sure I don't change the title of the window.MachineCode wrote:FindWindow is not the way to do it. Search for the forum for why, but basically it's because the window title can change without you wanting it too.
Code: Select all
OpenConsole()
String.s = #DQUOTE$ + "User opened another one of me." + #DQUOTE$
FirstInstance = FindWindow_(0,"testProgram")
If FirstInstance
SetForegroundWindow_(FirstInstance)
SendMessage_(FirstInstance,@String,0,Len(String)) ;-Something is wrong here
End
EndIf
If OpenWindow(0, 220, 0, 600, 300, "testProgram", #PB_Window_SystemMenu | #PB_Window_TitleBar )
If CreateGadgetList(WindowID(0))
Repeat
Delay(1)
Select WindowEvent()
Case #PB_Event_CloseWindow : End
EndSelect
PP.s = ProgramParameter()
If PP <> "":PrintN(PP):EndIf
ForEver
EndIf
EndIf
lParam [in]
Type: LPARAM
Additional message-specific information
wParam [in]
Type: WPARAM
Additional message-specific information
-
- PureBasic Expert
- Posts: 4229
- Joined: Sat Apr 26, 2003 8:27 am
- Location: Strasbourg / France
- Contact:
Re: Opening a file using my program from windows explorer?
You can't use SendMessage_() like this.treebolt wrote:Code: Select all
String.s = ProgramParameter() FirstInstance = FindWindow_(0,"test program") If FirstInstance SendMessage_(FirstInstance,@String,0,Len(String)) ;-Something is wrong here SetForegroundWindow_(FirstInstance) End EndIf
SendMessage_(hWnd, Msg, wParam, lParam) sends the specified message (Msg) to a window or windows. But Msg is an integer, not a string or a pointer to a string. It's a system-defined message or an application-defined message that may be registered using RegisterWindowMessage_(), like in the post mentioned earlier in this thread.
The two last parameters depend on the message ; for example, if Msg = #WM_SETTEXT, lParam is a pointer to a null-terminated string that is the window text and wParam is not used.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Re: Opening a file using my program from windows explorer?
I see thanks for clearing that up. I found an example code for using mutex from the above link rsts posted that works. Thanks to all, u guys rock.
(Taken from http://www.purebasic.fr/english/viewtopic.php?t=27372)
(Taken from http://www.purebasic.fr/english/viewtopic.php?t=27372)
Code: Select all
Global WM_ACTIVATEOLDINST
WM_ACTIVATEOLDINST = RegisterWindowMessage_("WM_ACTIVATEOLDINST_PB")
MutexName$ = "mylittletestapp"
Procedure MyWindowCallback(WindowID, Message, wParam, lParam)
Result = #PB_ProcessPureBasicEvents
If message = WM_ACTIVATEOLDINST
CloseHandle_(hMutex)
End
EndIf
ProcedureReturn Result
EndProcedure
hMutex = CreateMutex_(0, 0, @MutexName$)
If GetLastError_() = #ERROR_ALREADY_EXISTS
SendMessage_(#HWND_BROADCAST, WM_ACTIVATEOLDINST, 0, 0)
Delay(100)
EndIf
If OpenWindow(0,100,150,450,200,"MyLittleTestApp " + Str(Random(500)), #PB_Window_SystemMenu)
SetWindowCallback(@MyWindowCallback())
Repeat
ev=WaitWindowEvent()
Until ev=#PB_Event_CloseWindow
EndIf
If hMutex <> 0
CloseHandle_(hMutex)
EndIf
End
-
- Addict
- Posts: 1482
- Joined: Tue Feb 22, 2011 1:16 pm
Re: Opening a file using my program from windows explorer?
For your own app for your own private use, yes; it's probably fine. But not for public use, as you never know what their window titles will be named, even if you set the name. They may be running apps that change the window title, for example, like Sandboxie does. Or Windows itself may add "(Not Responding)" to it. And so on.treebolt wrote:I guess it would work if I'm using it only for my own program right? I just have to make sure I don't change the title of the window.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
PureBasic: Born in 1998 and still going strong to this very day!