• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

error in using sendMessage

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
cc_2000_sg
Asked:
cc_2000_sg
  • 3
  • 2
1 Solution
 
DennisLCommented:
listening...
0
 
mdouganCommented:
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
 
cc_2000_sgAuthor Commented:
the problem is still there, after i changed the code as what you said..



how to solve it?????
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
mdouganCommented:
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
 
cc_2000_sgAuthor Commented:
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
 
mdouganCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now