Solved

Passing a string from VB6 to VB.NET

Posted on 2004-08-04
12
803 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now