Welcome Guest!
 VB Helper
 Previous Message All Messages Next Message 
VB Helper Newsletter  Rod Stephens
 Oct 09, 2009 09:47 PDT 

These days I often have examples that work in .NET but not in VB 6, bit
it's not too often that I post an example that works in VB 6 but not in
.NET.

This week I've posted an example that gets an object's reference count
(the number of variables pointing to it). Because VBN .NET uses garbage
collection instead of reference counting (a mistake IMHO), you can't do
this in VB .NET.

Have a great week and thanks for subscribing!
----------
Rod
RodSte-@vb-helper.com

Books To Keep: http://www.BooksToKeep.com
----------
*** Now Available ***

Beginning Database Design Solutions
http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/

Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
==========

    VB6 Contents:
1. New HowTo: Get an object's reference count in Visual Basic 6
2. New HowTo: Send a window to the top or bottom in Visual Basic 6

    VB.NET Contents:
3. New HowTo: Get a module's fully qualified name in Visual Basic .NET
4. New HowTo: Send a window to the top or bottom in Visual Basic .NET

    Both Contents:
5. New Links
==========
++++++++++
   <VB6>
++++++++++
==========
1. New HowTo: Get an object's reference count in Visual Basic 6
http://www.vb-helper.com/howto_get_reference_count.html
http://www.vb-helper.com/HowTo/howto_get_reference_count.zip

The GetRefCount function looks at the memory address 4 bytes after an
object reference's address and copies the long integer value there. It
subtracts 3 to adjust for extra references added by the call to the
function.

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
(dest As Any, src As Any, ByVal nbytes As Long)

' Return the number of references to the object.
' We subtract 3 to adjust for references made by parameters.
Private Function GetRefCount(ByRef obj As IUnknown) As Long
    If obj Is Nothing Then Exit Function
    CopyMemory GetRefCount, ByVal (ObjPtr(obj)) + 4, 4
    GetRefCount = GetRefCount - 3
End Function

The program makes a collection containing a reference to a form. The Add
Reference button adds a new reference to that same object. The Remove
Reference button removes a reference.

As the program adds and removes references, it displays the number of
references to that object and the number of items in the collection
(they should match).

Private References As Collection

' Make a Form1 instance to have references to.
Private Sub Form_Load()
    Set References = New Collection
    References.Add New Form1

    lblResults.Caption = GetRefCount(References(1)) & " references"
    lblNumItems.Caption = References.count & " items"
End Sub

' Add a new reference.
Private Sub cmdAddReference_Click()
    References.Add References(1)
    lblResults.Caption = GetRefCount(References(1)) & " references"
    lblNumItems.Caption = References.count & " items"
    cmdRemoveReference.Enabled = True
End Sub

' Delete a reference.
Private Sub cmdRemoveReference_Click()
    References.Remove References.count
    lblResults.Caption = GetRefCount(References(1)) & " references"
    lblNumItems.Caption = References.count & " items"
    cmdRemoveReference.Enabled = (References.count > 1)
End Sub
==========
2. New HowTo: Send a window to the top or bottom in Visual Basic 6
http://www.vb-helper.com/howto_send_to_top_bottom.html
http://www.vb-helper.com/HowTo/howto_send_to_top_bottom.zip

The program uses the SetWindowPos API function to move the window to the
top or bottom.

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long,
ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal
cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const HWND_BOTTOM = 1
Private Const HWND_TOP = 0

Private Sub cmdSendToBack_Click()
Dim flags As Long

    flags = SWP_NOSIZE Or SWP_NOMOVE
    SetWindowPos Me.hwnd, HWND_BOTTOM, 0, 0, 0, 0, flags
End Sub

Private Sub cmdBringToFront_Click()
Dim flags As Long

    flags = SWP_NOSIZE Or SWP_NOMOVE
    SetWindowPos Me.hwnd, HWND_TOP, 0, 0, 0, 0, flags
End Sub
==========
++++++++++
<VB.NET>
++++++++++
==========
3. New HowTo: Get a module's fully qualified name in Visual Basic .NET
http://www.vb-helper.com/howto_net_get_module_name.html
http://www.vb-helper.com/HowTo/howto_net_get_module_name.zip

The GetModuleName function returns the module's fully qualified name. It
gets the Type for the Person class and uses that object's Module method
to get the module. It then simply returns the module's
FullyQualifiedName property.

Imports System.Reflection

Public Class Person
    ' Return the module's fully qualified name.
    Public Shared Function GetModuleName() As String
        Dim my_module As [Module] = GetType(Person).Module
        Return my_module.FullyQualifiedName
    End Function

    ...
End Class
==========
4. New HowTo: Send a window to the top or bottom in Visual Basic .NET
http://www.vb-helper.com/howto_net_send_to_top_bottom.html
http://www.vb-helper.com/HowTo/howto_net_send_to_top_bottom.zip

The program uses the SetWindowPos API function to move the window to the
top or bottom.

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal
hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal
cx As Integer, ByVal cy As Integer, ByVal uFlags As UInt32) As Boolean
End Function

ReadOnly HWND_BOTTOM As New IntPtr(1)
ReadOnly HWND_TOP As New IntPtr(0)

Private Const SWP_NOSIZE As UInt32 = &H1
Private Const SWP_NOMOVE As UInt32 = &H2

Private Sub btnSendToBottom_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnSendToBottom.Click
    Dim flags As UInt32 = SWP_NOMOVE Or SWP_NOSIZE
    SetWindowPos(Me.Handle, HWND_BOTTOM, 0, 0, 0, 0, flags)
End Sub

Private Sub btnBringToTop_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnBringToTop.Click
    Dim flags As UInt32 = SWP_NOMOVE Or SWP_NOSIZE
    SetWindowPos(Me.Handle, HWND_TOP, 0, 0, 0, 0, flags)
End Sub
==========
++++++++++
<Both>
++++++++++
==========
5. New Links
http://www.vb-helper.com/links.html

Enable Technology Training
http://www.enableit.co.uk
Training in VBA, ASP, ASP.NET, SQL Server, etc. in West Midlands and
Birmingham.

Advanced Outlook Repair
http://www.repair-outlook.com/
Outlook recovery tool that scans damages folders to recover messages,
folders, etc.

calnet Technology Group
http://calnettech.com/
Computer support and IT Services in the Los Angeles/Irvine/Long Beach
area.
==========
Archives:
    http://www.topica.com/lists/VBHelper
    http://www.topica.com/lists/VB6Helper
    http://www.topica.com/lists/VBNetHelper

Post questions at:
    http://www.topica.com/lists/VBHelperQA
	
 Previous Message All Messages Next Message 
  Check It Out!

  Topica Channels
 Best of Topica
 Art & Design
 Books, Movies & TV
 Developers
 Food & Drink
 Health & Fitness
 Internet
 Music
 News & Information
 Personal Finance
 Personal Technology
 Small Business
 Software
 Sports
 Travel & Leisure
 Women & Family

  Start Your Own List!
Email lists are great for debating issues or publishing your views.
Start a List Today!

© 2001 Topica Inc. TFMB
Concerned about privacy? Topica is TrustE certified.
See our Privacy Policy.