WINDOWS未開函式揭密(1) (轉)

gugu99發表於2008-04-15
WINDOWS未開函式揭密(1) (轉)[@more@] 

未開揭密


  什麼是未公開函式?
   對於Windows的函式相信大家已經不會陌生了。API函式呼叫是透過在VB中定義和呼叫Windows動態連線庫中的函式從而擴充套件VB的功能。API函式的定義和呼叫方法在的開發工具中都有詳細的說明。
   那麼什麼是“未公開”函式呢?微軟為了某種目的。對於一些封裝在中的函式沒有在任何開發文件提供任何函式說明和定義。而這些函式有很多都是很有用的。所幸的是一些有心之人對Windows系統動態連線庫做了十分詳細的分析,從而將這些未公開函式公開以供廣大開發人員共享(我對他們的感激之情就象滔滔江水,綿綿不絕……)
  好了,廢話少說,言歸正傳,這裡首先向大家介紹的是Windows下未公開的關閉Windows的函式。在Windows API函式中雖然提供了ExitWindowsEx這樣的API函式,但是在有一些程式中我們需要象一些程式完畢那樣提示重新啟動或者不重新啟動計算機。或者要在程式中彈出關閉系統對話方塊。要實現這些功能就要使用Windows未公開函式了。下面來看程式。
  首先建立一個新的工程,在工程檔案中加入一個Module檔案。在Module檔案中加入以下程式碼:
  Option Explicit
  Public bIsWinNT As Boolean
  '下面就是未公開的函式定義,注意在Alias之後沒有使用函式的真正名字
  '而是用了函式編號,這是因為微軟沒有公開函式名
  Declare Function SHRestartSystemMB Lib“32"-
  Alias “#59"-
  (ByVal hOwner As Long, —
  ByVal sExtraPrompt As String,-
  ByVal uFlags As Long) As Long
  Declare Function SHShutDownDialog Lib “shell32" -
  Alias “#60" -
  (ByVal YourGuess As Long) As Long
  Declare Function GetVersionEx Lib “kernel32" -
  Alias“GetVersionExA" -
  (lpVersionInformation As OSVERSIONINFO) As Long
  Type OSVERSIONINFO
  dwOSVersionInfoSize As Long
  dwMajorVersion As Long
  dwMinorVersion As Long
  dwBuildNumber As Long
  dwPlatfod As Long
  szCSDVersion As String * 128
  End Type
  Public Const EWX-LOGOFF = 0
  Public Const EWX-SHUTDOWN = 1
  Public Const EWX-REBOOT = 2
  Public Const EWX-FORCE = 4
  Public Const EWX-POWEROFF = 8
  Public Const shrsExitNoDefPrompt = 1
  Public Const shrsRebootSystem = 2
  Const VER-PLATFORM-s = 0
  Const VER-PLATFORM-WIN32-WINDOWS = 1
  Const VER-PLATFORM-WIN32-NT = 2
  Declare Sub CopyMemory Lib “kernel32"
  Alias “RtlMoveMemory" -
  (pDest As Any, -
  p As Any, -
  ByVal ByteLen As Long)
  Declare Function IsTextUnicode Lib “advapi32"-
  (lpBuffer As Any,-
  ByVal cb As Long, -
  lpi As Long) As Long
  Public Const IS-TEXT-UNICODE-ASCII16 = &H1
  Public Const IS-TEXT-UNICODE-REVERSE-ASCII16 = &H10
  Public Const IS-TEXT-UNICODE-STATISTICS = &H2
  Public Const IS-TEXT-UNICODE-REVERSE-STATISTICS = &H20
  Public Const IS-TEXT-UNICODE-CONTROLS = &H4
  Public Const IS-TEXT-UNICODE-REVERSE-CONTROLS = &H40
  Public Const IS-TEXT-UNICODE-SIGNATURE = &H8
  Public Const IS-TEXT-UNICODE-REVERSE-SIGNATURE = &H80
  Public Const IS-TEXT-UNICODE-ILLEGAL-CHARS = &H100
  Public Const IS-TEXT-UNICODE-ODD-LENGTH = &H200
  Public Const IS-TEXT-UNICODE-CS_LEADBYTE = &H400
  Public Const IS-TEXT-UNICODE-NULL-BYTES = &H1000
  Public Const IS-TEXT-UNICODE-UNICODE-MASK = &HF
  Public Const IS-TEXT-UNICODE-REVERSE-MASK = &HF0
  Public Const IS-TEXT-UNICODE-NOT-UNICODE-MASK = &HF00
  Public Const IS-TEXT-UNICODE-NOT-ASCII_MASK = &HF000
  Public Function IsWinNT() As Boolean
  Dim osvi As OSVERSIONINFO
  osvi.dwOSVersionInfoSize = Len(osvi)
  GetVersionEx osvi
  IsWinNT = (osvi.dwPlatformId = VER-PLATFORM-WIN32-NT)
  End Function
  Public Function CheckString(msg As String) As String
  If bIsWinNT Then
  CheckString = StrConv(msg, vbUnicode)
  Else: CheckString = msg
  End If
  End Function
  Public Function GetStrFromPtr(lpszStr As Long, nBytes As Integer) As String
  ReDim ab(nBytes) As Byte
  CopyMemory ab(0), ByVal lpszStr, nBytes
  GetStrFromPtr = GetStrFromBuffer(StrConv(ab(), vbUnicode))
  End Function
  Public Function GetStrFromBuffer(szStr As String) As String
  If IsUnicodeStr(szStr) Then szStr = StrConv(szStr, vbFromUnicode)
  If InStr(szStr, vbNullChar) Then
  GetStrFromBuffer = Left$(szStr, InStr(szStr, vbNullChar) - 1)
  Else: GetStrFromBuffer = szStr
  End If
  End Function
  Public Function IsUnicodeStr(sBuffer As String) As Boolean
  Dim dwRtnFlags As Long
  dwRtnFlags = IS-TEXT-UNICODE-UNICODE-MASK
  IsUnicodeStr = IsTextUnicode(ByVal sBuffer, Len(sBuffer), dwRtnFlags)
  End Function
  然後在Form1中加入一個ComboBox、兩個CommandButton控制元件,然後在Form1的程式碼視窗中加入以下程式碼:
  Private Sub Command1_Click()
  Call SHShutDownDialog(0)
  End Sub
  Private Sub Command2_Click()
  Dim sPrompt As String
  Dim uFlag As Long
   Case Combo1.ListIndex
  Case -1: uFlag = Val(Combo1.Text)
  Case 0: uFlag = shrsExitNoDefPrompt
  Case 1: uFlag = shrsRebootSystem
  End Select
  If SHRestartSystemMB(hWnd, sPrompt, uFlag) = vbYes Then
  End If
  End Sub
  Private Sub Form_Load()
  bIsWinNT = IsWinNT()
  If bIsWinNT Then 'WinNT
  With Combo1
  .AddItem “0 - 關閉程式並以其它使用者身份登陸"
  .AddItem “1 - 關閉計算機"
  .AddItem “2 - 重新啟動計算機"
  .Text = “"
  End With
  Else 'Win95/98作業系統
  With Combo1
  .AddItem“1 - 關閉計算機"
  .AddItem “2 - 重新啟動計算機"
  .Text = “"
  End With
  End If
  Command1.Caption = “關閉系統對話方塊"
  Command2.Caption = “關閉或重新啟動計算機"
  End Sub
  執行程式,點選“關閉系統對話方塊”按鈕就可以彈出關閉系統對話方塊。在Combo1中選擇關閉系統、重新啟動或者關閉程式並以其它使用者身份登陸項再點選“關閉或重新啟動計算機”按鈕,系統就會彈出提示對話方塊提示是否相應的操作,點選“是”就可以執行了。
  Windows下未公開的API函式還有很多,以後我將會一一向大家介紹。 以上程式在,Windows98,4,下執行透過。(長沙 陳銳)



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-1002565/,如需轉載,請註明出處,否則將追究法律責任。

相關文章