?
Solved

error in using sendMessage

Posted on 2002-03-19
6
Medium Priority
?
292 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 18

Accepted Solution

by:
mdougan earned 200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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 Month12 days, 20 hours left to enroll

777 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