Hi all,

I have a bit of a problem with the following scenario:

I'm trying to get my MDI application to open documents from Windows Explorer.
This isn't actually any problems but when ever I double click on a document a new instance of the application is started.

So what I'm trying to do is to let the old instance (if any) open the new document.
This is how I'm trying to do this:
The start-up object is set to Sub Main().
The first thing the Main sub does is to check if App.PrevInstance is True.
If not it loads the frmMDI form that saves its hWnd to the registry (and other startup stuff in the Load event).
If there is a previous instance loaded Sub Main reads the hWnd value from the registry.
Then it calls the Command function to get the file name (if any).
The file name is saved to a string called sFile.
After that it sends a user defined message (WM_USER + &HFC) to the previous instance with wParam set to the length of the string (sFile) and lParam to the actual string value.

I've subclassed the frmMDI form and it gets the message allright.
But when I try to retrieve the original string it fails.
I use CopyMemory in my WinProc to copy wParam back into a string but I only get garbage.

Here's the code I use:
Code:
'In module1:
Private Declare Function SendMessageString _
 Lib "user32" Alias "SendMessageA" ( _
 ByVal hwnd As Long, _
 ByVal wMsg As Long, _
 ByVal wParam As Long, _
 ByVal lParam As String) As Long

Public Const = UM_OPENFILE = &H4FC&

Public Sub Main()
    Dim sFile As String
    Dim hWnd As Long

    If App.PrevInstance Then
        sFile = Command
        If Len(sFile) Then
            hWnd = CLng(GetSetting(App.EXEName, "Instance", "hWnd", "0")
            If hWnd Then
                SendMessageString hWnd, UM_OPENFILE, LenB(sFile), sFile
                End
            End If
        End If
    End If
    frmMDI.Show
End Sub

'**********************************
'in frmMDI
Private Sub MDIForm_Load()
    SaveSetting App.EXEName, "Instance", "hWnd", Me.hwnd
    HookForm Me.hwnd 'set up the sub-classing
    'other init stuff goes here
End Sub

'**********************************
'in Module2 (sub-classing module)
Private Declare Sub CopyMemory _
 Lib "kernel32" Alias "RtlMoveMemory" ( _
 Destination As Any, _
 Source As Any, _
 ByVal Length As Long)

Private Declare Function SetWindowLong _
 Lib "user32" Alias "SetWindowLongA" ( _
 ByVal hwnd As Long, _
 ByVal nIndex As Long, _
 ByVal dwNewLong As Long) As Long

Private 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

Private Const GWL_WNDPROC = (-4)
Private Const WM_DESTROY = &H2&

Private lngHwnd As Long
Private lngPrevWndProc As Long

Public Sub HookForm(frmHandle As Long)
    lngHwnd = frmHandle
    lngPrevWndProc = SetWindowLong(lngHwnd, GWL_WNDPROC, AddressOf WinProc)
End Sub

Public Sub UnhookForm()
    SetWindowLong lngHwnd, GWL_WNDPROC, lngPrevWndProc
End Sub

Public Function WinProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim sFile As String
    
    Select Case uMsg
        Case UM_OPENFILE
            If wParam > 0 Then
                sFile = String$(wParam, vbNullChar)
                CopyMemory ByVal sFile, ByVal lParam, wParam
            End If
        Case WM_DESTROY
            'the window has been closed
            UnhookForm
    End Select
    WinProc = CallWindowProc(lngPrevWndProc, hw, uMsg, wParam, lParam)
    If Len(sFile) Then
         MsgBox sFile 'here I actually want to call my open file procedure
    End If
End Function
Why doesn't the above work??????

The message is sent and retrieved by my WinProc above but I just can't get the string out correctly.

Best regards