1

I have a server application in VB.NET which is trying to send a text message to a textbox in a window in a client application, written in classic VB (I have no control over the client code). I'm using IPC, specifically the "SendMessageTimeout" PInvoke.

I want to send "Hello World" to the textbox in the client. When I run the app, it sends "H" (but doesn't send "ello World"). If I change the message to "Test 1-2-3", it sends "T" (but not "est 1-2-3").

How can I get the whole message sent?

Code below ("lParam" is the string I want to send):

   Private Function fSendString(ByVal hwnd As IntPtr, ByVal uMsg As IntPtr, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr

      Try
         Dim lResult As IntPtr
         Dim GCH As GCHandle = GCHandle.Alloc(lParam, GCHandleType.Pinned)
         Dim pS As IntPtr = GCH.AddrOfPinnedObject
         If SendMessageTimeout(hwnd, uMsg, wParam, pS, 0, 25, lResult) Then
            'If this function returns true, the result holds the return value.
            fSendString = lResult
         End If
         GCH.Free()
      Catch ex As Exception
         MsgBox("fSendString(): " & ex.ToString)
         fSendString = Nothing
      End Try

      Return fSendString

   End Function

As far as the app is concerned, everything is running fine - no errors at all. But obviously it's not working. :)

2
  • Is it possible that the client app only process one letter at a time? Commented Oct 24, 2014 at 18:21
  • No; it processes the whole string at once. I have the code for it, and eventually we'll be migrating it to .NET, but for right now it's stuck in classic. Also, while I'm testing it I'm having it pass to a client app of my creation, which is just a window with a textbox with absolutely no codebehind on the textbox. Commented Oct 24, 2014 at 18:40

1 Answer 1

1

I found the answer. Apparently pinning it via GCHandle.Alloc and then getting the IntPtr of the GCHandle was the cause of the problem. I solved it by replacing the following lines:

     Dim GCH As GCHandle = GCHandle.Alloc(lParam, GCHandleType.Pinned)
     Dim pS As IntPtr = GCH.AddrOfPinnedObject

With:

     Dim pS As IntPtr = Marshal.StringToHGlobalAnsi(lParam)

After doing this, the string passed completely into the client window's textbox.

Sign up to request clarification or add additional context in comments.

3 Comments

This will fail for international text. Would you like to know how to solve that and send UTF-16.
Sure, David. We only needed it so we can send commands to a running process (in this instance, to send an ID from a database table to a process which is divorced from our database) but it would be nice to have more robust code that handles international text as well.
Never mind. You've already accepted so I guess that means you are done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.