Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Anyone who knows anything about "Sub-classing" and "Hooking"

Status
Not open for further replies.

Muzzery

Programmer
Jan 11, 2002
72
0
0
GB
Can any one give me any information on "Sub-classing" and "Hooking," and how to use them at all?

Any help will be appreciated.

Thanks

muzz
 
Subclassing is a technique to change or amend the normal behavior of certain aspect. Hooking generally refers to intercepting keystrokes to again either change or otherwise amend the standard Windows message handling.

They can be quite powerful is used correctly, and quite devastating if not, and I wouldn't recomment going down that path unless you have a quite specific need to do so.

What is it that you're trying to do?

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I have a Word Document on a website that is opened up through IE, that is to say that you have all the standard functions of Word, but within IE.

The user can then modify the document, but they need to save it back up to the FTP site that it came from. We don't want to give out the FTP User Name and Pass, so we needed a way to upload the document, automatically.

I've created a button that shows at all times in Word, the button is specifically used for the documents that are opened as said above.

The button will do the following:
1. Upload the Document back up to the FTP site it came from.
2. Find the Window Handle of the IE Window with the Word Document embeded into it, using API commands.
3. Close the IE Window.

The trouble is that if the user does modify the document, then, as expected, Word realises this and asks the user if they want to save the changes. The thing is i don't want this to show, as i have already uploaded the document, and thus saving it.

I need to get rid of this message, and from what i understand, and have read on the Internet, Sub-Classing or Hooking seem to be the way to stop this.

I've tried countless other ways, including the usual Word VBA commands such as:

ActiveDocument.Saved = True
Application.Quit SaveChanges:=False

but they don't work because the document is Embeded in IE, it's not an original Word Document.

What i'm hoping is that i can get into IE's message queue, and intercept the "Do you want to save?" question, and abandon it.

Do you think this is possible? If so, could you, or anyone, point me in the right direction as to how to achieve it?

Thanks

muzz

P.S. Sorry about the long post, but i need to explain it all for you to understand.
 
Hmm. SaveChanges:=False should have done the trick. I'm trying to figure out why it doesn't seem to work for you.
 
It sounds like the Word "Save Changes" prompt is being called as a "trickle-down" effect of the IE window being closed. The event order when you close the whole IE window may be different than when you are in Word directly.

Just out of curiosity, what if you used Word VBA to close the document BEFORE you close the window? That might allow SaveChanges:=False to work correctly. . .

It's a stab in the dark, but you never know!

VBAjedi [swords]
 
Thanks for the idea, but unfortunatly it doesn;t work. I get this error:

Run-Time Error: '4605':

This method or property is not available becuase this docuemnt is in another application


That is the same error i get for the Application.Quit SaveChanges:=False (But not the ActiveDocument.Saved = True funily enough.)

Is "Sub-Classing" or "Hooking" the correct way to go about doing this, or does anybody else have any ideas?

Thanks

muzz :(
 
OK, i now think that i need to use a "System Wide Hook" which seems more complicated, does anybody know how to impliment this?

thanks

muzz
 
Can anybody help at all? So far i have the following code, but i need to create a DLL file to get the reference for the System WIde Hook.

Code:
Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" _
                    (ByVal lpLibFileName As String) As Long
                    
Declare Function GetProcAddress Lib "kernel32" _
                    (ByVal hModule As Long, ByVal lpProcName As String) As Long
                    
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
                    (ByVal idHook As Long, ByVal lpfn As Long, _
                    ByVal hmod As Long, ByVal dwThreadId As Long) As Long

Public Const WH_MSGFILTER = (-1)

Public Sub HookTest()
Dim HookDLL As String
Dim HookPointer As String
Dim HookHandle As Long

    'Load the DLL Library
    HookDLL = LoadLibrary("C:\Temp\Hook.dll")
    'Get the Address in the DLL to hook into the system
    HookPointer = GetProcAddress(HookDLL, "Hook")
    'Hook into the system
    HookHandle = SetWindowsHookEx(WH_MSGFILTER, HookPointer, HookDLL, 0)
    
End Sub
thanks

muzz
 
This is just an idea, but have you tried using 'ActiveDocument.Close' SaveChanges:=False' rather than 'Application.Quit SaveChanges:=False'?

N.
 
Yeah, i get this message again:

Run-Time Error: '4605':

This method or property is not available becuase this docuemnt is in another application


Can anybody tell me how i would create a DLL file to Hook into Windows?

thanks

muzz
 
I don't know if this will help or not but here goes:

'From wdSaveOptions enumeration
Public Const wdDoNotSaveChanges = 0
Public Const wdPromptToSaveChanges = -2 '(&HFFFFFFFE)
Public Const wdSaveChanges = -1 '(&HFFFFFFFF)

On Error Resume Next 'Handle errors here
objApp.Quit wdDoNotSaveChanges
On Error GoTo 0 ' Reset error processing

Hopefully the quit no save will stop your prompt window and the error code will let the code run. Or maybe ActiveDocument.Close

Good Luck!

PS. You might try posting this on the VB Windows API forum and see if they can help you.

Have a great day!

j2consulting@yahoo.com
 
Here's a link to a thread about killing a process using API calls. I don't know if it will be helpful to you or not.

thread711-663482

Good LucK!

Have a great day!

j2consulting@yahoo.com
 
Thanks for the reply, the first bit of code doesn't work i'm afraid, but i looked at the link in your second post, and i used this code:

Code:
Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, lpdwProcessId As Long) As Long

Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Const PROCESS_ALL_ACCESS = &H1F0FFF
'_______________________________

Public Sub sKillProcess(Byval hWnd As Long)

  '--- Kills a running application

  '--- Parameter
  '    hWnd: the application's window handle

  Dim lngRtn As Long
  Dim lngProc As Long
  DimlngProcID As Long

  lngRtn = GetWindowThreadProcessId(hWnd, lngProcID)
  lngProc = OpenProcess(PROCESS_ALL_ACCESS, Clng(0), lngProcID)
  lngRtn = TerminateProcess(lngProc, Clng(0))

End Sub
Which does work! (Yes!) BUT the trouble is that it closes 2 IE windows.

As i said above, the Word document is opened in IE. The way the user opens it is by clicking on a link from a website, which in turn, opens the Word docuemnt in a seperate IE window.

When i terminate the process, it close the document, without asking for saving, but it also closes the original IE window! Do you know how i can get round this? Or even why it's doing it? (I think it's becuase it's a child window, but i'm not sure.)

thanks for your help.

muzz.
 
Since you tried the KillThread code and it didn't do exactly what you wanted, I would suggest posting your code and the problem you are having as a response to the linked thread above. Perhaps the fellow that posted it will be able to help you. I'm sorry that I cannot, I just remembered seeing it and linked it in case it would help.

Good LucK!

Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top