Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

Sys(3097), Sys(3098)

Starting with VFP 7, these functions increment and decrement the reference count for COM objects.

Usage

nRefCount = SYS( 3097, oComObject )
nRefCount = SYS( 3098, oComObject )

Parameter

Value

Meaning

oComObject

Object

The COM object for which you want to increment or decrement the reference count.

nRefCount

Numeric

The current reference count.

This one’s for experienced developers. There may be occasions where you want to increment or decrement the current reference count for a COM object. SYS(3097) increases the number of reference counts by one, while SYS(3098) decreases the count. Why would you want to do this? Say the developer of a COM object you’re using has a glitch in the code, so that the reference count is always incremented twice when you create the object (an easy mistake to make). With this function, you can tweak the count back down again.

Warning: Increasing the reference count without decreasing the same amount can prevent the object from being released. Likewise, decreasing the count without having increased it can crash your program. If you must prove it to yourself, try issuing ? SYS(3098, _VFP) until the count reaches 0—boom! VFP crashes with a C00000005 error (or even worse, sometimes it just disappears). If you think you should be decreasing the count, but you’re not sure what it is, try incrementing first, verifying that it is now at least two, and decrementing it twice.

Example

* Here's an example of how this works:
oExcel = CREATEOBJECT("Excel.Application")
oExcel.Visible = .T.
? SYS(3097, oExcel)  && Increment counter, shows 2
oExcel.Quit()        && Excel disappears

* Open the Task Manager, and see that the process EXCEL.EXE
* is still running.

? SYS(3098, oExcel)  && Decrement counter, shows 1, but
                     && still in Task Manager Process List.
? SYS(3098, oExcel)  && Decrement counter again, shows 0, and it
                     && is gone from the Task Manager.
* THIS IS NOT THE WAY TO CODE YOUR APPLICATION. THIS CODE
* DEMONSTRATES THE EFFECTS OF THIS FUNCTION IN A SIMPLE MANNER
* AND IS FOR DEMONSTRATION PURPOSES ONLY.

See Also

Sys(), Sys(3095), Sys(3096)