How can i tell each new instance, once you are launched, check if a previous instance is sending the message(using api sendmessage), if yes, wait, before you send your message untill the previous instance send all it's messages (the file name), and once done and previous instance close, now you can start sending your message .... Something like making instances into a queue

Let me now explain the why

The following Code (found in the net), let us send a file name after we right click on it in window explorer, to our application.

In this code, each new instance is getting the file name from the command$, sending letter by letter the file name to the WndProc() , using the SendMessage api.

Then in WndProc we get the letters and we compose again the file name ! Wich allow us later to open it or work on it (add it to listbox), this job is made into the sub called OpenFile.

This works great when i have only 1 command sent (right click on 1 file and open it with the application) , but if i have many files selected here comes the problem :


All instances are almost using the sendmessage api on the same time, so the file that we receivne in WndPRoc() won't be correct.

For example : if we have selected 3 files C:\file1.txt C:\file2.txt C:\file3.txt
we right click and open with our application
3 instances are opened on the same time, they will all call the sendmessage api to send their file name (letter by letter) to the WndPRoc() .
WndPRoc() will receive letter from each on same time, so the result will be that when we will try to recompose the file name in WndPRoc(), we won't get C:\file1.txt then C:\file2.txt then C:\file3.txt because all were sent on the same time, so we will get something like :

CCC:::\\\fffiiillleee123...tttxxxttt (maybe not exactly in this order, but something like this)

From1 :

vb Code:
  1. Option Explicit
  2. Private Sub Form_Load()
  3.     MyMessageId = RegisterWindowMessage(PrivateMessage)
  4.     Dim FileName As String
  5.     FileName = Command$
  6.     If App.PrevInstance Then
  7.         If Len(FileName) Then
  8.             Dim N As Long
  9.             For N = 1 To Len(FileName)
  10.                 SendMessage HWND_BROADCAST, MyMessageId, Asc(Mid$(FileName, N)), ByVal 0&
  11.                 DoEvents
  12.             Next
  13.             SendMessage HWND_BROADCAST, MyMessageId, 0, ByVal 0& 'terminate the string
  14.         End If
  15.         Unload Me
  16.     Else
  17.         Show
  18.         If Len(FileName) Then OpenFile FileName
  19.         lpWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
  20.     End If
  21. End Sub


Module1 :
vb Code:
  1. Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  2. Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
  3. Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  4. 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
  5. Public Const PrivateMessage As String = "This is my application's private message" '<- Change this to a suitable message string
  6. Public MyMessageId As Long
  7. Public lpWndProc As Long
  8. Public Const HWND_BROADCAST = &HFFFFFFFF
  9. Public Const GWL_WNDPROC = (-4)
  10.  
  11. Public Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  12.     If uMsg = MyMessageId Then
  13.         Static FileName As String
  14.         If wParam Then
  15.             FileName = FileName & Chr$(wParam)
  16.         Else
  17.             OpenFile FileName
  18.             FileName = vbNullString
  19.         End If
  20.     End If
  21.     WndProc = CallWindowProc(lpWndProc, hWnd, uMsg, wParam, lParam)
  22. End Function
  23.  
  24. Public Sub OpenFile(FileName As String)
  25.     '
  26.     List1.AddItem FileName
  27.  
  28.     '
  29. End Sub



here is the link of the code am talking about