Results 1 to 7 of 7

Thread: HELP on MAKING an ACTIVE-X Control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    philippines
    Posts
    81

    HELP on MAKING an ACTIVE-X Control

    Ive been searching for days finding some answers to my question: pls. help.

    Im making an active-x control, in my usercontrol i have a command button, i want that command button to have a property named 'CommandFunction' with that property there is a user selectable dropdown menu which have 'Restart_PC', 'Log_Off_PC' and 'Shutdown_PC'. As we know these list, the button must behave to Restart or Log_off or Shutdown the PC, can anyone help me with this?

    im trying to study abt it and all i know i have to create an enum for this

    Public Enum cmdAPIfunction
    Restart_PC = 1
    Shutdown_PC = 2
    Log_Off_PC = 3
    End Enum

    and please i really dont understand propert let and get procedure. pls help. thanks!!
    DSN

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    philippines
    Posts
    81

    Unhappy

    helppppppppppp!!!
    DSN

  3. #3
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    425
    I am confused too when it comes to the get and let property... The trick is to know where to look from. In this case, you should look al the control from your application. The application wants to get information, so the Public Property Get ... is for the usercontrol to give information to the application that is calling.

    An example:
    VB Code:
    1. Dim lTestProperty As Long
    2.  
    3. Public Property Get TestProp() As Long
    4.     TestProp = lTestProperty 'letting the aaplication know the value of the variable in the usercontrol
    5. End Property
    6.  
    7. Public Property Let TestProp(NewValue As Long)
    8.     lTestProperty = NewValue 'assigning a new value to the variable
    9. End Property

    The example above placed in a usercontrol will give the aplication the functionality to set the long value in the usercontrol and retreiving it afterwards.

    If you want to set some attributes of this usercontrol in designtime and you want the application to access these properties, you have to use the propertybag to save these values.

    VB Code:
    1. Dim SubFormCaption As String
    2.  
    3. Public Property Let Caption(sCaption As String)
    4.     SubFormCaption = sCaption
    5.     lblCaption.Caption = SubFormCaption
    6.     PropertyChanged "pbSubFormCaption" 'This will call the propertybag sub, very important...
    7. End Property
    8.  
    9. Public Property Get Caption() As String
    10.     Caption = SubFormCaption
    11. End Property
    12.  
    13. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    14.     SubFormCaption = PropBag.ReadProperty("pbSubFormCaption", "")
    15.     lblCaption.Caption = SubFormCaption
    16. End Sub
    17.  
    18. Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    19.     Call PropBag.WriteProperty("pbSubFormCaption", SubFormCaption, "")
    20. End Sub


    I hope you get more inside when reading this... I have the information from the www.vbworld.com. Or what it used to be. It had a very good tutorial... Try to find it for more information and explanations.

    Good luck

    [edit: had a little typo]
    Last edited by BShadow; Mar 11th, 2003 at 04:43 PM.
    "Experience is something you don't get until just after you need it."

  4. #4
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    425

    Re: HELP on MAKING an ACTIVE-X Control

    Originally posted by dexter2003
    Ive been searching for days finding some answers to my question: pls. help.

    Im making an active-x control, in my usercontrol i have a command button, i want that command button to have a property named 'CommandFunction' with that property there is a user selectable dropdown menu which have 'Restart_PC', 'Log_Off_PC' and 'Shutdown_PC'. As we know these list, the button must behave to Restart or Log_off or Shutdown the PC, can anyone help me with this?

    im trying to study abt it and all i know i have to create an enum for this

    Public Enum cmdAPIfunction
    Restart_PC = 1
    Shutdown_PC = 2
    Log_Off_PC = 3
    End Enum

    and please i really dont understand propert let and get procedure. pls help. thanks!!
    By the way, if you want to let the computer reboot etc. You can use the command "shutdown -r -t0" the -r is reboot and -t0 is now, this timeout can be set to another value in seconds..

    There are API calls to do this though... Just search this forum on "Shutdown API" and you'll probably find them... Or you can look at www.allapi.net or use your API viewer in your Visual Basic Addins...

    Again, good luck
    "Experience is something you don't get until just after you need it."

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    philippines
    Posts
    81
    i have done the code already but i have 1 more problem.

    when i click the Button_function on my control and selected 1 of the 3 options which is restart, shutdown or logoff, suppose i selected shutdown the shutdown dialog shows up, even if im not running the form, is there a code that can stop it to show during the selection process?
    DSN

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    philippines
    Posts
    81
    by the way heres my code:

    Public Enum cmdchoose
    [Restart PC]
    [Log Off PC]
    [Shutdown PC]
    End Enum


    Private Declare Function SHShutDownDialog Lib "shell32" Alias "#60" (ByVal YourGuess As Long) As Long
    Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long

    Public Property Get Button_Function() As cmdchoose
    Button_Function = selectfunc
    End Property

    Public Property Let Button_Function(ByVal NewValue As cmdchoose)
    If NewValue = [Restart PC] Or NewValue = [Shutdown PC] Or NewValue = [Log Off PC] Then
    selectfunc = NewValue
    If selectfunc = [Restart PC] Then MsgBox "Restart PC", vbOKOnly, "Restart!"
    If selectfunc = [Log Off PC] Then MsgBox "PC LOGGED OFF", vbOKOnly, "Log-OFF"
    If selectfunc = [Shutdown PC] Then SHShutDownDialog 0
    End If
    End Property
    DSN

  7. #7
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    425
    Okay you use the API calls...

    If selectfunc = [Shutdown PC] Then SHShutDownDialog 0

    This will probably show the ShutDown dialog...
    Try looking for another APPI...

    VB Code:
    1. Option Explicit
    2. Private Const EWX_SHUTDOWN As Long = 1
    3. Private Const EWX_REBOOT As Long = 2
    4. Private Const EWX_FORCE As Long = 4
    5. Private Const EWX_POWEROFF As Long = 8
    6. Private Const EWX_LOGOFF as Long = 0
    7. 'The ExitWindowsEx function either logs off, shuts down, or shuts
    8. 'down and restarts the system.
    9.  
    10. Private Declare Function ExitWindowsEx Lib "user32" _
    11.     (ByVal dwOptions As Long, _
    12.     ByVal dwReserved As Long) As Long
    13. 'The GetLastError function returns the calling thread's last-error
    14. 'code value. The last-error code is maintained on a per-thread basis.
    15. 'Multiple threads do not overwrite each other's last-error code.
    16. Private Declare Function GetLastError Lib "kernel32" () As Long
    17. Private Const mlngWindows95 = 0
    18. Private Const mlngWindowsNT = 1
    19. Public glngWhichWindows32 As Long
    20. 'The GetVersion function returns the operating system in use.
    21. Private Declare Function GetVersion Lib "kernel32" () As Long
    22. Private Type LUID
    23.     UsedPart As Long
    24.     IgnoredForNowHigh32BitPart As Long
    25. End Type
    26. Private Type LUID_AND_ATTRIBUTES
    27.     TheLuid As LUID
    28.     Attributes As Long
    29. End Type
    30. Private Type TOKEN_PRIVILEGES
    31.     PrivilegeCount As Long
    32.     TheLuid As LUID
    33.     Attributes As Long
    34. End Type
    35. 'The GetCurrentProcess function returns a pseudohandle for the
    36. 'current process.
    37. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    38. 'The OpenProcessToken function opens the access token associated with
    39. 'a process.
    40. Private Declare Function OpenProcessToken Lib "advapi32" _
    41.     (ByVal ProcessHandle As Long, _
    42.     ByVal DesiredAccess As Long, _
    43.     TokenHandle As Long) As Long
    44. 'The LookupPrivilegeValue function retrieves the locally unique
    45. 'identifier (LUID) used on a specified system to locally represent
    46. 'the specified privilege name.
    47. Private Declare Function LookupPrivilegeValue Lib "advapi32" _
    48.     Alias "LookupPrivilegeValueA" _
    49.     (ByVal lpSystemName As String, _
    50.     ByVal lpName As String, _
    51.     lpLuid As LUID) As Long
    52. 'The AdjustTokenPrivileges function enables or disables privileges
    53. 'in the specified access token. Enabling or disabling privileges
    54. 'in an access token requires TOKEN_ADJUST_PRIVILEGES access.
    55. Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
    56.     (ByVal TokenHandle As Long, _
    57.     ByVal DisableAllPrivileges As Long, _
    58.     NewState As TOKEN_PRIVILEGES, _
    59.     ByVal BufferLength As Long, _
    60.     PreviousState As TOKEN_PRIVILEGES, _
    61.     ReturnLength As Long) As Long
    62. Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
    63.  
    64. Private Sub AdjustToken()
    65. '********************************************************************
    66. '* This procedure sets the proper privileges to allow a log off or a
    67. '* shut down to occur under Windows NT.
    68. '********************************************************************
    69. Const TOKEN_ADJUST_PRIVILEGES = &H20
    70. Const TOKEN_QUERY = &H8
    71. Const SE_PRIVILEGE_ENABLED = &H2
    72. Dim hdlProcessHandle As Long
    73. Dim hdlTokenHandle As Long
    74. Dim tmpLuid As LUID
    75. Dim tkp As TOKEN_PRIVILEGES
    76. Dim tkpNewButIgnored As TOKEN_PRIVILEGES
    77. Dim lBufferNeeded As Long
    78. 'Set the error code of the last thread to zero using the
    79. 'SetLast Error function. Do this so that the GetLastError
    80. 'function does not return a value other than zero for no
    81. 'apparent reason.
    82.     SetLastError 0
    83. 'Use the GetCurrentProcess function to set the hdlProcessHandle
    84. 'variable.
    85.     hdlProcessHandle = GetCurrentProcess()
    86. '    If GetLastError <> 0 Then
    87. '        systemMessage.AddItem "GetCurrentProcess Error==" & GetLastError
    88. '    End If
    89.     OpenProcessToken hdlProcessHandle, _
    90.         (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
    91. '    If GetLastError <> 0 Then
    92. '        systemMessage.AddItem "OpenProcessToken Error==" & GetLastError
    93. '    End If
    94. 'Get the LUID for shutdown privilege
    95.     LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
    96. '    If GetLastError <> 0 Then
    97. '        systemMessage.AddItem "LookupPrivilegeValue Error==" & GetLastError
    98. '    End If
    99.     tkp.PrivilegeCount = 1    ' One privilege to set
    100.     tkp.TheLuid = tmpLuid
    101.     tkp.Attributes = SE_PRIVILEGE_ENABLED
    102. 'Enable the shutdown privilege in the access token of this process
    103.     AdjustTokenPrivileges hdlTokenHandle, _
    104.     False, _
    105.     tkp, _
    106.     Len(tkpNewButIgnored), _
    107.     tkpNewButIgnored, _
    108.     lBufferNeeded
    109. '    If GetLastError <> 0 Then
    110. '        systemMessage.AddItem "AdjustTokenPrivileges Error==" & GetLastError
    111. '    End If
    112. End Sub
    113.  
    114. Private Sub cmdShutdown_Click()
    115.     If glngWhichWindows32 = mlngWindowsNT Then
    116.         AdjustToken
    117. '        systemMessage.AddItem "Post-AdjustToken GetLastError " & GetLastError
    118.     End If
    119.     ExitWindowsEx (EWX_LOGOFF), &HFFFF
    120. '    systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
    121. End Sub
    122.  
    123. Private Sub cmdForceShutdown_Click()
    124.     If glngWhichWindows32 = mlngWindowsNT Then
    125.         AdjustToken
    126. '        systemMessage.AddItem "Post-AdjustToken GetLastError " & GetLastError
    127.     End If
    128.     ExitWindowsEx (EWX_LOGOFF Or EWX_FORCE), &HFFFF
    129. '    systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
    130. End Sub
    131.  
    132. Public Sub Main()
    133. '********************************************************************
    134. '* When the project starts, check the operating system used by
    135. '* calling the GetVersion function.
    136. '********************************************************************
    137. Dim lngVersion As Long
    138.     lngVersion = GetVersion()
    139.     If ((lngVersion And &H80000000) = 0) Then
    140.         glngWhichWindows32 = mlngWindowsNT
    141. '        systemType.Text = "Windows NT"
    142.     Else
    143.         glngWhichWindows32 = mlngWindows95
    144. '        systemType.Text = "Windows 95"
    145.     End If
    146. Call cmdForceShutdown_Click
    147. End
    148. End Sub

    This is from http://www.vbforums.com/showthread.p...t=Shutdown+API

    VB Code:
    1. Const EWX_LOGOFF = 0
    2. Const EWX_SHUTDOWN = 1
    3. Const EWX_REBOOT = 2
    4. Const EWX_FORCE = 4
    5. Private Declare Function ExitWindowsEx Lib "user32" (ByVal  _
    6. uFlags As Long, ByVal dwReserved As Long) As Long
    7.  
    8. Private Sub Form_Load()
    9.     msg = MsgBox("This program is going to reboot your computer. Press OK to continue or Cancel to stop.", vbCritical + vbOKCancel + 256, App.Title)
    10.     If msg = vbCancel Then End
    11.     'reboot the computer
    12.     ret& = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
    13. End Sub

    From http://www.vbforums.com/showthread.p...t=Shutdown+API
    "Experience is something you don't get until just after you need it."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width