未公开的API

I

import

Unregistered / Unconfirmed
GUEST, unregistred user!
windows未公开函数揭密——之二
上一次我向大家介绍了如何利用windows未公开函数实现关闭或重新启动windows的功能。在这一期内我要向大家介绍如何使用windows未公开函数实现调用windows系统中的一些对话框的功能。其中包括如何调用系统的“运行程序“对话框、”查找文件“对话框、更改与文件相关联的图标对话框等等。
首先在vb中建立一个新的工程文件,然后在form1中加入五个commandbutton控件,不要改变它们的属性,然后在form1的代码窗口中加入以下代码:
option explicit
private type browseinfo
hwndowner as long
pidlroot as long
pszdisplayname as long
lpsztitle as long
ulflags as long
lpfncallback as long
lparam as long
iimage as long
end type
const bif_returnonlyfsdirs = 1
const max_path = 260
private declare function shobjectproperties lib "shell32" alias "#178" _
(byval hwndowner as long, _
byval uflags as long, _
byval lpstrname as string, _
byval lpstrpar as string) as long
private declare sub cotaskmemfree lib "ole32.dll" (byval hmem as long)
private declare function shbrowseforfolder lib "shell32" (lpbi _
as browseinfo) as long
private declare function shfindfiles lib "shell32" alias "#90" _
(byval pidlroot as long, _
byval pidlsavedsearch as long) as long
 
private declare function getfilenamefrombrowse lib "shell32" alias "#63" ( _
byval hwndowner as long, _
byval lpstrfile as string, _
byval nmaxfile as long, _
byval lpstrinitdir as string, _
byval lpstrdefext as string, _
byval lpstrfilter as string, _
byval lpstrtitle as string) as long
private declare sub pickicondlg lib "shell32" alias "#62" (byval hwndowner as long, _
byval lpstrfile as string, byval nmaxfile as long, lpdwiconindex as long)
private declare function shrunfiledlg lib "shell32" alias "#61" _
(byval howner as long, _
byval hicon as long, _
byval lpstrdirectory as string, _
byval sztitle as string, _
byval szprompt as string, _
byval uflags as long) as long
private sub command1_click()
shrunfiledlg form1.hwnd, form1.icon.handle, "c:", "运行程序演示", _
"在文本框中输入程序名或按浏览键查找程序", 0
end sub
private sub command2_click()
dim a as long
dim astr as string
 
astr = "c:.exe"
pickicondlg form1.hwnd, astr, 1, a
end sub
private sub command3_click()
dim astr as string * 256
dim bstr as string
 
bstr = "c:"
getfilenamefrombrowse form1.hwnd, astr, 256, bstr, "*.txt", _
"文本文件 *.txt", "open sample"
debug.print astr
end sub
private sub command4_click()
dim lpidlist as long
dim udtbi as browseinfo
'初试化udtbi结构
with udtbi
.hwndowner = form1.hwnd
.ulflags = bif_returnonlyfsdirs
end with
 
'弹出文件夹查看窗口
lpidlist = shbrowseforfolder(udtbi)
 
if lpidlist then
'查找文件
shfindfiles lpidlist, 0
call cotaskmemfree(lpidlist)
end if
end sub
private sub command5_click()
shobjectproperties form1.hwnd, 2, "c:.exe", "samples"
end sub
private sub form_load()
command1.caption = "运行程序"
command2.caption = "更改图标"
command3.caption = "打开文件"
command4.caption = "查找文件"
command5.caption = "显示文件属性"
end sub
 
运行程序,分别点击不同的按钮,就可以看到不同的按钮实现了对不同的系统对话框的调用。
windows未公开函数揭密——之三
这次介绍的是如何利用windows未公开函数实现系统文件操作监视功能。利用该功能可以对windows下的任何文件操作,包括建立文件、文件夹;删除文件;改变文件大小等操作都可以纪录在案。
首先来介绍实现上面操作的两个未公开函数:shchangenotifyregister和shchangenotifyderegister,shchangenotifyregister函数的定义如下:
declare function shchangenotifyregister lib "shell32" alias "#2" _
(byval hwnd as long, _
byval uflags as shcn_itemflags, _
byval dweventid as shcn_eventids, _
byval umsg as long, _
byval citems as long, _
lpps as pidlstruct) as long
其中参数hwnd指定接受系统通告的窗口句柄,参数umsg指定消息值,如果函数调用成功,系统就会将hwnd指定的窗口加入到系统通告链中,并且返回系统通告句柄。当有建立文件等系统操作发生时,系统会向hwnd指定的窗口发送umsg消息,关于其它参数,会在下面的程序中说明。函数shchangenotifyderegister的定义如下:
declare function shchangenotifyderegister lib "shell32" alias "#4" _
(byval hnotify as long) as boolean
其中参数hnotify指定系统通告的句柄。
下面是操作的具体的vb范例:
首先建立一个新的工程,在form1中加入一个textbox控件。在form1的代码窗口之中加入以下代码:
option explicit
private sub form_load()
if subclass(hwnd) then '改变form1的消息处理函数
if iside then
text1.text = vbcrlf & _
"一个 windows的文件目录操作即时监视程序," & vbcrlf & "可以监视在explore中的重命名、新建、删除文" & _
vbcrlf & "件或目录;改变文件关联;插入、取出cd和添加" & vbcrlf & "删除网络共享都可以被该程序记录下来。"
end if
call shnotify_register(hwnd)
else
text1 = "系统不支持操作监视程序 :)"
end if
move screen.width - width, screen.height - height
end sub
private function iside() as boolean
on error goto out
debug.print 1 / 0
out:
iside = err
end function
private sub form_unload(cancel as integer)
call shnotify_unregister
call unsubclass(hwnd)
end sub
public sub notificationreceipt(wparam as long, lparam as long)
dim sout as string
dim shns as shnotifystruct
dim sdisplayname1 as string
dim sdisplayname2 as string
 
movememory shns, byval wparam, len(shns)
 
if shns.dwitem1 then
sdisplayname1 = getdisplaynamefrompidl(shns.dwitem1)
end if
 
if shns.dwitem2 then
sdisplayname2 = getdisplaynamefrompidl(shns.dwitem2)
end if
sout = shnotify_geteventstr(sdisplayname1, sdisplayname2, lparam) & vbcrlf
text1 = text1 & sout & vbcrlf
text1.selstart = len(text1)
end sub
然后在工程中加入三个模块(bas)文件,将三个文件分别保存为mdef.bas、mshell.bas、msub.bas。在mdef.bas中加入以下代码:
'mdef.bas包含shell操作的函数和数据类型的定义
option explicit
declare sub movememory lib "kernel32" alias "rtlmovememory" (pdest as any, _
psource as any, byval dwlength as long)
declare sub cotaskmemfree lib "ole32.dll" (byval pv as long)
public const max_path = 260
public const noerror = 0
'shgetspecialfolderlocation获得某一个特殊的目录的位置,如果函数调用成功返回noerror
'或者一个ole错误
declare function shgetspecialfolderlocation lib "shell32.dll" _
(byval hwndowner as long, _
byval nfolder as shspecialfolderids, _
pidl as long) as long
public enum shspecialfolderids '列出所有windows下特殊文件夹的id
csidl_desktop = &h0
csidl_internet = &h1
csidl_programs = &h2
csidl_controls = &h3
csidl_printers = &h4
csidl_personal = &h5
csidl_favorites = &h6
csidl_startup = &h7
csidl_recent = &h8
csidl_sendto = &h9
csidl_bitbucket = &ha
csidl_startmenu = &hb
csidl_desktopdirectory = &h10
csidl_drives = &h11
csidl_network = &h12
csidl_nethood = &h13
csidl_fonts = &h14
csidl_templates = &h15
csidl_common_startmenu = &h16
csidl_common_programs = &h17
csidl_common_startup = &h18
csidl_common_desktopdirectory = &h19
csidl_appdata = &h1a
csidl_printhood = &h1b
csidl_altstartup = &h1d
csidl_common_altstartup = &h1e
csidl_common_favorites = &h1f
csidl_internet_cache = &h20
csidl_cookies = &h21
csidl_history = &h22
end enum
'shgetpathfromidlist函数将一个item转换为文件路径
declare function shgetpathfromidlist lib "shell32.dll" alias "shgetpathfromidlista" _
(byval pidl as long, _
byval pszpath as string) as long
'shgetfileinfopidl函数获得某个文件对象的信息。
declare function shgetfileinfopidl lib "shell32" alias "shgetfileinfoa" _
(byval pidl as long, _
byval dwfileattributes as long, _
psfib as shfileinfobyte, _
byval cbfileinfo as long, _
byval uflags as shgfi_flags) as long
public type shfileinfobyte
hicon as long
iicon as long
dwattributes as long
szdisplayname(1 to max_path) as byte
sztypename(1 to 80) as byte
end type
declare function shgetfileinfo lib "shell32" alias "shgetfileinfoa" _
(byval pszpath as string, _
byval dwfileattributes as long, _
psfi as shfileinfo, _
byval cbfileinfo as long, _
byval uflags as shgfi_flags) as long
public type shfileinfo
hicon as long
iicon as long
dwattributes as long
szdisplayname as string * max_path
sztypename as string * 80
end type
enum shgfi_flags
shgfi_largeicon = &h0
shgfi_smallicon = &h1
shgfi_openicon = &h2
shgfi_shelliconsize = &h4
shgfi_pidl = &h8
shgfi_usefileattributes = &h10
shgfi_icon = &h100
shgfi_displayname = &h200
shgfi_typename = &h400
shgfi_attributes = &h800
shgfi_iconlocation = &h1000
shgfi_exetype = &h2000
shgfi_sysiconindex = &h4000
shgfi_linkoverlay = &h8000
shgfi_selected = &h10000
end enum
'根据一个特定文件夹对象的id获得它的目录pidl
public function getpidlfromfolderid(howner as long, nfolder as shspecialfolderids) as long
dim pidl as long
if shgetspecialfolderlocation(howner, nfolder, pidl) = noerror then
getpidlfromfolderid = pidl
end if
end function
public function getdisplaynamefrompidl(pidl as long) as string
dim sfib as shfileinfobyte
if shgetfileinfopidl(pidl, 0, sfib, len(sfib), shgfi_pidl or shgfi_displayname) then
getdisplaynamefrompidl = getstrfrombuffera(strconv(sfib.szdisplayname, vbunicode))
end if
end function
public function getpathfrompidl(pidl as long) as string
dim spath as string * max_path
if shgetpathfromidlist(pidl, spath) then
getpathfrompidl = getstrfrombuffera(spath)
end if
end function
public function getstrfrombuffera(sz as string) as string
if instr(sz, vbnullchar) then
getstrfrombuffera = left$(sz, instr(sz, vbnullchar) - 1)
else
getstrfrombuffera = sz
end if
end function
在mshell.bas中加入以下代码:
'mshell.bas函数包含注册和反注册系统通告以及文件夹信息转换的函数
option explicit
private m_hshnotify as long '系统消息通告句柄
private m_pidldesktop as long
'定义系统通告的消息值
public const wm_shnotify = &h401
public type pidlstruct
pidl as long
bwatchsubfolders as long
end type
declare function shchangenotifyregister lib "shell32" alias "#2" _
(byval hwnd as long, _
byval uflags as shcn_itemflags, _
byval dweventid as shcn_eventids, _
byval umsg as long, _
byval citems as long, _
lpps as pidlstruct) as long
type shnotifystruct
dwitem1 as long
dwitem2 as long
end type
declare function shchangenotifyderegister lib "shell32" alias "#4" _
(byval hnotify as long) as boolean
declare sub shchangenotify lib "shell32" _
(byval weventid as shcn_eventids, _
byval uflags as shcn_itemflags, _
byval dwitem1 as long, _
byval dwitem2 as long)
public enum shcn_eventids
shcne_renameitem = &h1
shcne_create = &h2
shcne_delete = &h4
shcne_mkdir = &h8
shcne_rmdir = &h10
shcne_mediainserted = &h20
shcne_mediaremoved = &h40
shcne_driveremoved = &h80
shcne_driveadd = &h100
shcne_netshare = &h200
shcne_netunshare = &h400
shcne_attributes = &h800
shcne_updatedir = &h1000
shcne_updateitem = &h2000
shcne_serverdisconnect = &h4000
shcne_updateimage = &h8000&
shcne_driveaddgui = &h10000
shcne_renamefolder = &h20000
shcne_freespace = &h40000
shcne_assocchanged = &h8000000
shcne_diskevents = &h2381f
shcne_globalevents = &hc0581e0
shcne_allevents = &h7fffffff
shcne_interrupt = &h80000000
end enum
#if (win32_ie >= &h400) then
public const shcnee_orderchanged = &h2
#end if
public enum shcn_itemflags
shcnf_idlist = &h0
shcnf_patha = &h1
shcnf_printera = &h2
shcnf_dword = &h3
shcnf_pathw = &h5
shcnf_printerw = &h6
shcnf_type = &hff
shcnf_flush = &h1000
shcnf_flushnowait = &h2000
#if unicode then
shcnf_path = shcnf_pathw
shcnf_printer = shcnf_printerw
#else
shcnf_path = shcnf_patha
shcnf_printer = shcnf_printera
#end if
end enum
public function shnotify_register(hwnd as long) as boolean
dim ps as pidlstruct
 
if (m_hshnotify = 0) then
 
m_pidldesktop = getpidlfromfolderid(0, csidl_desktop)
if m_pidldesktop then
 
ps.pidl = m_pidldesktop
ps.bwatchsubfolders = true
 
'注册windows监视,将获得的句柄保存到m_hshnotify中
m_hshnotify = shchangenotifyregister(hwnd, shcnf_type or shcnf_idlist, _
shcne_allevents or shcne_interrupt, _
wm_shnotify, 1, ps)
shnotify_register = cbool(m_hshnotify)
 
else
call cotaskmemfree(m_pidldesktop)
end if
end if
end function
public function shnotify_unregister() as boolean
if m_hshnotify then
if shchangenotifyderegister(m_hshnotify) then
m_hshnotify = 0
call cotaskmemfree(m_pidldesktop)
m_pidldesktop = 0
shnotify_unregister = true
end if
end if
end function
public function shnotify_geteventstr(strpath1, strpath2 as string, dweventid as long) as string
dim sevent as string
 
select case dweventid
case shcne_renameitem: sevent = "重命名文件" + strpath1 + "为" + strpath2
case shcne_create: sevent = "建立文件 文件名:" + strpath1
case shcne_delete: sevent = "删除文件 文件名:" + strpath1
case shcne_mkdir: sevent = "新建目录 目录名:" + strpath1
case shcne_rmdir: sevent = "删除目录 目录名:" + strpath1
case shcne_mediainserted: sevent = strpath1 + "中插入可移动存储介质"
case shcne_mediaremoved: sevent = strpath1 + "中移去可移动存储介质"
case shcne_driveremoved: sevent = "移去驱动器" + strpath1
case shcne_driveadd: sevent = "添加驱动器" + strpath1
case shcne_netshare: sevent = "改变目录" + strpath1 + "的共享属性"
case shcne_updatedir: sevent = "更新目录" + strpath1
case shcne_updateitem: sevent = "更新文件 文件名:" + strpath1
case shcne_serverdisconnect: sevent = "断开与服务器的连" + strpath1 + " " + strpath2
case shcne_updateimage: sevent = "shcne_updateimage"
case shcne_driveaddgui: sevent = "shcne_driveaddgui"
case shcne_renamefolder: sevent = "重命名文件夹" + strpath1 + "为" + strpath2
case shcne_freespace: sevent = "磁盘空间大小改变"
 
case shcne_assocchanged: sevent = "改变文件关联"
end select
 
shnotify_geteventstr = sevent
end function
在msub.bas中加入以下代码:
'msub函数包括窗口的消息处理函数
option explicit
private const wm_ncdestroy = &h82
private const gwl_wndproc = (-4)
private const oldwndproc = "oldwndproc"
private declare function getprop lib "user32" alias "getpropa" (byval _
hwnd as long, byval lpstring as string) as long
private declare function setprop lib "user32" alias "setpropa" (byval _
hwnd as long, byval lpstring as string, byval hdata as long) as long
private declare function removeprop lib "user32" alias "removepropa" (byval _
hwnd as long, byval lpstring as string) as long
private declare function setwindowlong lib "user32" alias "setwindowlonga" _
(byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long
private declare function callwindowproc lib "user32" alias "callwindowproca" _
(byval lpprevwndfunc as long, byval hwnd as long, byval umsg as long, _
byval wparam as long, byval lparam as long) as long
public function subclass(hwnd as long) as boolean
dim lpfnold as long
dim fsuccess as boolean
 
if (getprop(hwnd, oldwndproc) = 0) then
lpfnold = setwindowlong(hwnd, gwl_wndproc, addressof wndproc)
if lpfnold then
fsuccess = setprop(hwnd, oldwndproc, lpfnold)
end if
end if
 
if fsuccess then
subclass = true
else
if lpfnold then call unsubclass(hwnd)
msgbox "unable to successfully subclass &h" & hex(hwnd), vbcritical
end if
end function
public function unsubclass(hwnd as long) as boolean
dim lpfnold as long
 
lpfnold = getprop(hwnd, oldwndproc)
if lpfnold then
if removeprop(hwnd, oldwndproc) then
unsubclass = setwindowlong(hwnd, gwl_wndproc, lpfnold)
end if
end if
end function
public function wndproc(byval hwnd as long, byval umsg as long, byval wparam as _
long, byval lparam as long) as long
select case umsg
case wm_shnotify '处理系统消息通告函数
call form1.notificationreceipt(wparam, lparam)
case wm_ncdestroy
call unsubclass(hwnd)
msgbox "unubclassed &h" & hex(hwnd), vbcritical, "wndproc error"
end select
 
wndproc = callwindowproc(getprop(hwnd, oldwndproc), hwnd, umsg, wparam, lparam)
end function
保存文件,然后运行程序,然后你可以在explore中试着建立或者删除一个文件或者文件夹,在form中可以看到你所做的操作已经被纪录并且显示到textbox中了。
现在分析以下上面的程序,上面的程序首先调用shchangenotifyregister函数将form添加到系统消息通告链中,并利用setwindowlong函数改变form的缺省的消息处理函数,当接受到系统通告消息后,根据传递的参数获得系统通告的内容并且显示在文本窗口中。退出程序时调用shchangenotifyderegister函数注销系统消息通告。
 

Similar threads

顶部