Solved

Passing a string from VB6 to VB.NET

Posted on 2004-08-04
12
812 Views
Last Modified: 2010-08-05
I have a program written in vb6 that needs to send a string to a program written in VB.NET.  I'm tring to use window messages to do this.  So in the VB6 program I'd have:

SendMessage hwnd, WM_COMMAND, MessageType, "The Message"

and in vb.net I override the WndProc to catch WM_COMMAND messages and then do something with the string:

ProcessMessage(m.lParam)

Of course, that doesn't work.  LParam is an IntPtr, not a string.  So I tried this:

ProcessMessage(System.Runtime.InteropServices.Marshal.PtrToStringAnsi(m.LParam))

which returned junk (I tried PtrToStringUni and PtrToStringAuto with the same result).  I also tried changing the SendMessage declaration.  I origionally used
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

and I've tried changing the lParam declaration to be 'ByRef lParam as String'.  I've also tried making the lParam declaration 'ByVal lParam as Long', and passing the string as 'StrPtr("The Message")'.

Incidentally, I am receiving the MessageType number correctly, and can switch on its value, and I can see that m.lParam.toInt32() is changing each time I get a message.

How do I get the string to go through correcly?
0
Comment
Question by:LastToKnow0
12 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 11719907
What is the purpose of this?  Maybe we can offer alternatives when we understand the reason for doing this.
0
 
LVL 1

Author Comment

by:LastToKnow0
ID: 11720006
The VB6 program is connected to hardware that collects caller ID information from incoming phone calls.  The VB.NET uses the phone number (or other information, its not always numerical, so I can't just pass it as a long) that was collected and displays information associated with that number.

If there is a better method for getting the information to the vb.net program I'd be interested in that, but I'd really like to know how to get the SendMessage method to work, since I use is in other programs.
0
 
LVL 15

Expert Comment

by:SRigney
ID: 11720081
It sounds like you could make your VB6 program a .dll that raises an event, then catch that event in .Net.

Another method (although probably a poor work-around), is to write the information to a file and make the .net application use a file watcher.  Any time the file changes it will get an event and can read the file to see what changed.

I'm not the one to answer the SendMessage question though, so good luck there.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 1

Author Comment

by:LastToKnow0
ID: 11720174
Unfortunately, I'm not able to make changes to the vb6 program on the scale of making it into a dll, and although the file solution would work, its pretty kludgy.
0
 
LVL 15

Expert Comment

by:SRigney
ID: 11720764
0
 
LVL 15

Expert Comment

by:SRigney
ID: 11720792
0
 
LVL 1

Author Comment

by:LastToKnow0
ID: 11720969
I'm afraid not.  I'm not getting an exception as in the first question (and I have tried both of the declarations for SendMessage that are listed there.  And the second is more geared towards sending a string out from .NET, rather than receiving one.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11722592
How is the SendMessage function defined in VB6?

Bob
0
 
LVL 1

Author Comment

by:LastToKnow0
ID: 11722820
Not sure what I can tell you that isn't in the question.  To reiterate:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
0
 
LVL 3

Accepted Solution

by:
Stimphy earned 125 total points
ID: 11723416
Hello,

   Maybe you could just open a TCP Socket and send the data through it.  Granted this isnt the best solution, but it will work.

Regaurds,
Dave
0
 
LVL 1

Author Comment

by:LastToKnow0
ID: 11729494
I like the idea of using a TCP socket.  If no one comes up with a way to send the string through a SendMessage call, I'll give you the points.
0
 
LVL 1

Author Comment

by:LastToKnow0
ID: 11768097
Well, I went ahead and used the TCP socket.  Thanks for the idea.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question