Solved

Passing a string from VB6 to VB.NET

Posted on 2004-08-04
12
834 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

628 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