?
Solved

error in using sendMessage

Posted on 2002-03-19
6
Medium Priority
?
294 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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

839 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