Solved

error in using sendMessage

Posted on 2002-03-19
6
289 Views
Last Modified: 2007-12-19
hi,

here is my code


Option Explicit

'this is my form and there are one rich text box
'and 2 command buttons on it.
Private Sub Command1_Click()
RichTextBox1.OLEObjects.Add , , "", "Equation.3"
RichTextBox1.SetFocus

End Sub

Private Sub Command2_Click()
    SendMessage RichTextBox1.hwnd, WM_COPY, 0&, 0&
    RichTextBox1.SetFocus
End Sub

Private Sub Form_Load()

End Sub






'here is my modul
Option Explicit


Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lparam As Any) As Long
Public Const WM_CUT = &H300
Public Const WM_COPY = &H301
Public Const WM_PASTE = &H302





when i run it.
i click command1 and input anything, then click anywhere in the rich box and click command2, then close the application.
sometimes there is an error:
The instruction at "0x77aa9b79" referenced memory at
"0x00000000". The memory could not be "read"


please help me

thanks

cc
0
Comment
Question by:cc_2000_sg
[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
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:DennisL
ID: 6880044
listening...
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6880255
Several Windows API functions will cause an error if you take the SendMessage declaration as is from the API Text viewer.  Notice that the last paramter is being passed byRef (the default).  Very often, this is a null paramter, so, it should really be passed ByVal:

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg
As Long, ByVal wParam As Long, ByVal lparam As Any) As Long
0
 

Author Comment

by:cc_2000_sg
ID: 6881540
the problem is still there, after i changed the code as what you said..



how to solve it?????
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 18

Accepted Solution

by:
mdougan earned 50 total points
ID: 6881551
OK, I think that the SendMessage doesn't have anything to do with it.  I think that it has to do with the OleObjects.Add

I think that perhaps the oleobject is open in memory, and by closing the application, you're leaving this object hanging and it will cause an error like you are getting.

You might look to see if there is some code to remove or close the oleobjects before the program shuts down.  Because of this type of thing, it is sometimes necessary to use restraint while debugging, not to hit the Stop button, but instead, to close the application through a File|Exit menu where you unload the main form, or call the End statement.
0
 

Author Comment

by:cc_2000_sg
ID: 6882004
excellent!

you are right.

if i clear the oleobjects before closing, no error will occur.


thanks mdougan.

by the way, that means i have to clear all the oleobjects before exitting my application, if those oleobjects have been referenced by other system objects like clipboard.
why should we do that?

any way i will give you the mark by tomorrow.

thanks mdougan:)


cc
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6882738
It's always a good idea to do perfect "housekeeping" anyway.  I always get rid of my objects and set them = Nothing as soon as I'm done with them.  If I create them in a function, then at the end of the function I'll close them and set them to nothing.  This frees up any memory or other resources allocated for these objects.

Where you will get gpfs is when some object is created, and it has a handle to your application, and then you close your application, then that handle is no longer valid, if the object tries to use it then there will be a crash.

As far as the clipboard thing, I can only guess that if an object is big enough, the clipboard doesn't really copy the object, it just keeps a pointer to it, and perhaps it tells the garbage collector not to remove the object from memory (in case you want to paste it to some other application).  But then if you close your app, you really want to get rid of the object, so, if you close it, and set it's variable name = Nothing, then that should do it.

I'm sure you've gotten that message before about "you've copied a lot of data to the clipboard, do you want to keep this available to other applications" when you try to shut down something like Excel, Word or Access after copying a large amount of data... that is probably what is happening.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

737 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