Solved

error in using sendMessage

Posted on 2002-03-19
6
281 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now