Solved

Passing a string from VB6 to VB.NET

Posted on 2004-08-04
12
825 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
Industry Leaders: 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 - Compare and highlight cell not the same 3 63
Added a column screws up code 5 78
MsgBox 4 76
Excel 2007 Random Generate List Based on Defined Source Cells 3 32
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…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

752 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