Opening a file using my program from windows explorer?

Just starting out? Need help? Post your questions and find answers here.
User avatar
treebolt
User
User
Posts: 50
Joined: Thu Feb 21, 2008 4:38 pm
Location: Palm Harbor, FL

Opening a file using my program from windows explorer?

Post by treebolt »

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 :D
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Opening a file using my program from windows explorer?

Post by rsts »

It would depend on how the calling program passes the data but you would retrieve it via

Code: Select all

ProgramParameter
cheers
User avatar
treebolt
User
User
Posts: 50
Joined: Thu Feb 21, 2008 4:38 pm
Location: Palm Harbor, FL

Re: Opening a file using my program from windows explorer?

Post by treebolt »

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.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Opening a file using my program from windows explorer?

Post by rsts »

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
User avatar
treebolt
User
User
Posts: 50
Joined: Thu Feb 21, 2008 4:38 pm
Location: Palm Harbor, FL

Re: Opening a file using my program from windows explorer?

Post by treebolt »

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
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Opening a file using my program from windows explorer?

Post by rsts »

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
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Opening a file using my program from windows explorer?

Post by rsts »

If the passed parameter(s) contain spaces you'll likely have to pass your string as

Code: Select all

#DQUOTE$ + string + #DQUOTE$
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Opening a file using my program from windows explorer?

Post by MachineCode »

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!
User avatar
treebolt
User
User
Posts: 50
Joined: Thu Feb 21, 2008 4:38 pm
Location: Palm Harbor, FL

Re: Opening a file using my program from windows explorer?

Post by treebolt »

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

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
Here is a full program, if it's run more than once, then the text User opened another one of me. should appear in the console of the first instance... I love Microsofts helpful information as to the usage of the last two parameters in SendMessage()...
lParam [in]

Type: LPARAM

Additional message-specific information

wParam [in]

Type: WPARAM

Additional message-specific information
gnozal
PureBasic Expert
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?

Post by gnozal »

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
You can't use SendMessage_() like this.
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).
User avatar
treebolt
User
User
Posts: 50
Joined: Thu Feb 21, 2008 4:38 pm
Location: Palm Harbor, FL

Re: Opening a file using my program from windows explorer?

Post by treebolt »

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)

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
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Opening a file using my program from windows explorer?

Post by MachineCode »

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.
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.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Post Reply