Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Double-Click->open file in already open application 1

Status
Not open for further replies.

Trevoke

Programmer
Jun 6, 2002
1,142
US
Visual Studio 6.
I have an application (text editor) that I am using as the default editor for text and rtf files ('cause I can).

When I double-click on a txt of rtf file, and an instance of my software is already open, I would like that instance to open the txt file in one of its windows (the main application has a subwindow for every open file), as opposed to opening a new instance of my software like it does now.

Does anyone know a quick and clean way to do it? Or .. a clean way? Or even a dirty way?

Catapultam habeo. Nisi pecuniam omnem mihi dabis, ad caput tuum saxum immane mittam.
 
You need to somehow communicate to the previous instance of your application, if it is running, and pass the rtf or txt filename to that previous instance so that the file is loaded in that instance in a new subwindow (which I think is a MDI child window).

The following example demonstrates this using SendMessage API. It registers a unique window message which is used to communicate between two instances of the application and to pass the string data between them.

Start a new project and insert the following code in Form1.
___

Option Explicit
Private Sub Form_Load()
MyMessageId = RegisterWindowMessage(PrivateMessage)
Dim FileName As String
FileName = Command$
If App.PrevInstance Then
If Len(FileName) Then
Dim N As Long
For N = 1 To Len(FileName)
SendMessage HWND_BROADCAST, MyMessageId, Asc(Mid$(FileName, N)), ByVal 0&
DoEvents
Next
SendMessage HWND_BROADCAST, MyMessageId, 0, ByVal 0& 'terminate the string
End If
Unload Me
Else
Show
If Len(FileName) Then OpenFile FileName
lpWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
End If
End Sub
___

Add a standard module to your project and insert the following code in it.
___

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const PrivateMessage As String = &quot;This is my application's private message&quot; '<- Change this to a suitable message string
Public MyMessageId As Long
Public lpWndProc As Long
Public Const HWND_BROADCAST = &HFFFFFFFF
Public Const GWL_WNDPROC = (-4)

Public Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = MyMessageId Then
Static FileName As String
If wParam Then
FileName = FileName & Chr$(wParam)
Else
OpenFile FileName
FileName = vbNullString
End If
End If
WndProc = CallWindowProc(lpWndProc, hWnd, uMsg, wParam, lParam)
End Function

Public Sub OpenFile(FileName As String)
'
MsgBox &quot;Put your code here for opening files.&quot; _
& vbLf & FileName, , &quot;Sub OpenFile&quot;
'
End Sub
___

Compile your program to make exe file.

Now create a new file type and associate it with this application using Windows Explorer using the standard procedure.

Create several files based on the above extensions and try to open them in Windows Explorer. You will notice that new instances of the application are not created and only the first instance of the application responds to the all the documents which are opened using Windows Explorer.

You can implement the method used in this example in your application.
I have tested it on Windows XP and it is working fine.
 
Thanks a lot for the information; however, a friend of mine, helping me develop the application, said he would rather have something that would be of lower-level, maybe by catching the call through Windows when it is made, I'm not sure exactly..
Is that exhausting your knowledge, are we entering Assembly yet? :)

Catapultam habeo. Nisi pecuniam omnem mihi dabis, ad caput tuum saxum immane mittam.
 
If your (expert) friend wants to work at a lower level than API calls, then you may be posting in the wrong forum! Have a look for a 'C' forum or an assembly language forum.

But I seriously doubt you need it...

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Er, sorry, but he wanted a higher level solution than the Windows message queue. Maybe something using DDE? (Neither of us know anything about DDE except what it stands for and what it can do).

Catapultam habeo. Nisi pecuniam omnem mihi dabis, ad caput tuum saxum immane mittam.
 
Well, one way would be to change the association for text and rtf files on application startup, and to restore them to their original values on application shutdown
 
One way will be to check the commandline argument at application startup, and also check for the previous instance of the application and if found DDE.

Will be like this

Exe Sub Main gets a call
Check the commandline argument (If Command$ <> &quot;%1&quot; And Command$ <> &quot;&quot; Then)
Check for application previous instance
If found, send the contents of the file (Inside Command$) to that exe via any method you want.

I hope your friend will be happy to try ..
 
Hi, Hypetia. I have a problem in which its solution is similar to the one you post here. I noticed that you convert the string to ASCII format before sending it using SendMessage.

May I know is it possible to send a string directly to another window?

Thanks and hope to hear from you soon.
 
I send the string character by character, each character being sent with a SendMessage call followed by a terminating null character signaling the end of the string.

>May I know is it possible to send a string directly to another window?

Yes, you can pass string data between two applications using WM_COPYDATA message.

Microsoft knowledge base article 176058 explains how to pass string data between two applications.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top