用VB枚举系统进程PID及路径
四、VBA获取目录、文件路径简明代码(VB语句、FSO两种方式)
四、VBA获取目录、文件路径简明代码(VB语句、FSO两种方式)(一)VB语句方式''''程序入口↓''''获取所有文件路径Sub GetFileList()Call GetFolderList ''''调用GetFolderList()过程获取所有文件夹路径Columns(2).ClearDim fileName, folderPath As StringDim rowIndexA, rowIndexB, maxRow, lastRowA As Integer maxRow = Rows.CountlastRowA = Cells(maxRow,1).End(xlUp).RowFor rowIndexA =1To lastRowAfolderPath = Cells(rowIndexA,1).ValuefileName = Dir(folderPath)rowIndexB = Cells(maxRow,2).End(xlUp).Row +1Do While fileName <>""Cells(rowIndexB,2).Value = folderPath & fileNamerowIndexB = rowIndexB +1fileName = DirLoopNext rowIndexAEnd Sub''''获取GetMainDirectory拾取文件夹路径下的所有文件夹,放到A列Sub GetFolderList()Dim folderName As StringDim i, k As IntegerColumns(1).ClearCells(1,1).Value = GetMainDirectory(msoFileDialogFolderPicker)&"\"i =1k =1Do While i <= kfolderName = Dir(Cells(i,1).Value, vbDirectory)DoIf InStr(folderName,".")=0And _(GetAttr(Cells(i,1).Value & folderName)And vbDirectory)= vbDirectory Thenk = k +1Cells(k,1).Value = Cells(i,1).Value & folderName &"\"End IffolderName = DirLoop Until folderName =""i = i +1LoopEnd Sub''''函数,拾取一个文件夹路径,返回路径字符串Function GetMainDirectory(ByVal DialogType As MsoFileDialogType)As StringWith Application.FileDialog(DialogType)If.Show =True ThenGetMainDirectory =.SelectedItems(1)End IfEnd WithEnd Function(二)FSO方式''''##############################''''工具——引用类库文件"Microsoft Scripting Runtime"''''##############################''''程序入口↓''''获取文件列表Sub FsoGetFileList()Dim folderPath As StringDim maxRow, lastRow, maxRowB, LastRowB As IntegerDim i As IntegerDim folder, allFiles As ObjectDim fso As New FileSystemObjectCall FsoGetFolderList ''''调用FsoGetFolderList方法获取目录列表Columns(2).ClearmaxRow = Rows.CountlastRow = Cells(maxRow,1).End(xlUp).RowFor i =1To lastRowfolderPath = Cells(i,1).ValueSet folder = fso.GetFolder(folderPath)Set allFiles = folder.FilesmaxRowB = Rows.CountLastRowB = Cells(maxRowB,2).End(xlUp).Row +1For Each File In allFilesCells(LastRowB,2).Value = File.PathLastRowB = LastRowB +1NextNext iEnd Sub''''获取文件夹列表Sub FsoGetFolderList()Dim rowIndex As IntegerDim folderPath As String''''调用函数获取主文件夹目录folderPath = GetMainDirectory(msoFileDialogFolderPicker) rowIndex =1Columns(1).ClearDoIf rowIndex =1ThenGetFolderPath (folderPath)Cells(rowIndex,1).Value = folderPathElseGetFolderPath (Cells(rowIndex,1).Value)End IfrowIndex = rowIndex +1Loop Until Cells(rowIndex,1).Value =""End Sub''''定义函数,作用是获取给定文件夹路径(mainFolderPath)的子文件夹Function GetFolderPath(mainFolderPath)Dim mainFolder, childFolders As ObjectDim index As Integer''''创建FileSystemObject对象fsoDim fso As New FileSystemObject''''从路径获得folder对象mainFolderSet mainFolder = fso.GetFolder(mainFolderPath)''''获得mainFolder的子目录集合childFoldersSet childFolders = mainFolder.SubFolders''''行号初始值设定为A列最后一个非空行的+1行,第一次执行的时候index=2index = Cells(Rows.Count,1).End(xlUp).Row +1''''for each ……in 遍历集合取每一个子目录childFolder的路径pathFor Each childfolder In childFoldersCells(index,1).Value = childfolder.Path ''''路径index = index +1NextEnd Function''''函数,拾取一个文件夹路径,返回路径字符串Function GetMainDirectory(ByVal DialogType As MsoFileDialogType)As StringWith Application.FileDialog(DialogType)If.Show =True ThenGetMainDirectory =.SelectedItems(1)End IfEnd WithEnd Function。
利用VB控件操作目录和文件夹
利用VB控件操作目录和文件夹 (一)察看和显示目录下的文件和文件夹 对于这个实现,其实很简单。
首先想到的就是VB中给我们提供的现成的控件。
主要有这么两类: 一类是驱动器列表框(DriveListBox)、目录列表框(DirListBox)和文件列表框(FileListBox)三个控件组合而成的自定义对话框; 另一类是windows提供的标准对话框。
他们在工具箱中的位置和图标如下所示: 1、驱动器列表框是一个下拉式的列表框,他和一般下拉式的列表框的不同仅在于功能上的不同,它提供了一个驱动器的列表。
当单击右边的箭头时,则弹出计算机中的所有驱动器的下拉列表。
默认状态下,在驱动器列表中显示的是当前驱动器,我们可以输入或从下拉列表中选择有效的驱动器标示符。
下面是它的主要属性,事件和方法. 属性Drive本属性用于返回或设置运行时选择的驱动器.默认值为当前驱动器 改变Drive属性会触发Change事件. 示例: Drive1.Drive = “c:” 设置C盘为当前驱动器. 事件Change当选择一个新驱动器或通过代码改变了Drive属性时触发该事件 下面是示例代码: Private Sub Drive1_Change() Dir1.Path = Drive1.Drive '当选择一个新驱动器时,将驱动器列表中选中的当前驱动器,赋给目录列表的路径. End Sub 2、目录列表框 目录列表框用于显示当前驱动器的目录结构,目录列表框从最高层目录开始,显示当前驱动器的目录结构,并按层次关系缩进跟目录下的所有子目录。
下面是它的主要属性,方法和事件: 属性Path本属性用于返回或设置运行时选择的路径,默认路径为当前路径.改变Dri属性会触发Change事件. 示例: Dri1.Path = Drive1.Drive 设置在驱动器列表框中选中的驱动器盘符为目录列表的当前路径. ListIndex本属性用来返回或设置控件中当前被选择的项目索引号.目录列表框中的每一个目录都可以通过ListIndex属性来标识.由Path属性所设置的当前目录的ListIndex属性值总是-1,而它上面的目录的ListIndex属性值为-2,再上面的为-3,以此类推;而它所包含的子目录恰恰相反,紧挨着的第一个子目录的ListIndex属性值为0,往下一次加一. ListCount本属性返回当前目录下的所有子目录书.ListCount的值比最大的ListIndex的值大1. 事件Change当选择一个新目录或通过代码改变了Path属性时触发该事件 下面是示例代码: Private Sub Dir1_Change() '将文件列表框的路径值,设置为目录列表框所选中的路径值 File1.Path = Dir1.Path End Sub 3、文件列表框 文件列表框用来显示当前目录中的部分或者全部文件.文件列表框的大部分属性和一般的列表框相同,都具有大小,位置,字体,颜色等以及List,ListCount,ListIndex等属性.下面是主要的属性: 属性Path本属性用于返回或设置运行时选择的路径以显示其下的文件,默认路径为当前路径.改变Dri属性会触发PathChange事件. 示例: File1.Path= Dri1.Path 设置在目录列表框中选中的路径为文件列表的当前路径. Pattern本属性用来确定程序运行时,列表框中显示那些类型的文件.除了使用”*” ”?”等通配符外,在参数中还可以使用分号”;”来分割多种文件类型.例如:”*.ext;*.bat” FileName本属性返回或设置所选文件的路径和文件名.可以从本属性值中返回当前列表中选择的文件名.路径可用Path属性单独检索.在功能上,本属性值与ListIndex等价.如果没有文件被选中,FileName属性将返回0长度的字符串.改变甭属性值可能会产生一个或多个如下事件:PathChange(如果改变路径),PatternChange(如果改变模式),DblClick(如果指定存在的文件) 事件Click当选择一个新的文件时触发该事件 下面是示例代码: Private Sub File1_Click() Picture1.Picture = LoadPicture(Dir1.Path & " " & File1.FileName) '在图片框中显示选定的图形文件. End Sub 4、标准对话框 CommonDialog控件提供了一组标准的操作对话框,进行诸如打开,和保存文件,设置打印选项,以及选择颜色和字体等操作.通过运行windows帮助引擎还能显示帮助. CommonDialog控件在visual basic和Microsoft Windows动态链接库commdlg.dll的例程之间提供了一个接口.为了用这个控件创建一个对话框,commdlg.dll必须存在于microsoft Windows的system目录下.然后再visual basic中选择工程/部件,并在显示的部件对话框中选中Microsoft common Dialog Control 6.0,确定后,在工具栏里就显示了出来.如下图所示: 在应用程序中要使用CommonDialog控件,可将其添加到窗体中并设置其属性.控件所显示的对话框有控件的方法确定.在运行时,当相应的方法被调用时,将显示一个对话框或是执行帮助引擎;在设计时,CommonDialog 控件是以图标的形式显示在窗体中的.该图标的大小不能改变. 使用指定的方法,CommonDialog控件能够显示下列对话框: 方法所显示的对话框 ShowOpen显示[打开]对话框 ShowSave显示[另存为]对话框 ShowColor显示[颜色]对话框 ShowFont显示[字体]对话框 ShowPrinter显示[打印]或[打印选项]对话框 Showhelp显示windows帮助引擎 下面是它的主要属性,方法: 属性Filer该属性应用于CommonDialog控件中的[打开][另存为]对话框.本属性用来返回或设置在对话框[类型]列表框中显示的过滤器.过滤的作用是确定对话框中文件列表框中显示的文件类型.例如:设置为*.txt时,将显示文本文件.要显示多种类型的文件,可以用管道(|)符号(ASCII124)将他们分开.管道符号前后不能加空格.如:*.rm|*.rmvb Action该属性返回或设置一个表示所显示对话框类型的整数.具体如下. 设置数值说明 0没有操作 1显示[打开]对话框 2显示[另存为]对话框 3显示[颜色]对话框 4显示[字体]对话框 5显示[打印]或[打印选项]对话框 6运行WINHLP32.EXE FileName本属性应用于CommonDialog控件的[打开][另存为]对话框. 本属性返回或设置所选文件的路径和文件名.如果在运行时被创建,FileName属性将返回0长度的字符串,表示当前没有选择文件.在CommonDialog控件里,可以在打开对话框之前设置FileName属性来设定初始文件名. 可以从本属性值中返回当前列表中选择的文件名.路径可用Path属性单独检索.在功能上,本属性值与ListIndex等价.如果没有文件被选中,FileName属性将返回0长度的字符串. 改变甭属性值可能会产生一个或多个如下事件:PathChange(如果改变路径),PatternChange(如果改变模式),DblClick(如果指定存在的文件) 事件Click当选择一个新的文件时触发该事件 下面是一个例子: 我们在这里要做一个VCD的播放器,下面是界面. 下表是其中所用到的控件及其属性设置: 对象特性设置值 窗体名称Frmvcd BorderStyle1 CaptionVCD播放器 菜单标题文件 名称Mnufile 标题打开 名称Mnuopen 标题播放 名称Mnuplay 标题退出 名称Mnuexit 标题选项 名称Mnuoption 标题连续播放 名称Mnurepeat 标题静音 名称Mnuslient 多媒体控件名称Mmcontrol Picture控件名称Picture1 通用对话框名称Commondialog1 下面是主要程序代码代码: Private Sub mnuopen_Click() ’当点击菜单中的打开时执行 '在未选择文件时,文件名为空字符,播放菜单不可用 mnuplay.Enabled =False CommonDialog1.FileName = "" '下面语句设置文件过滤方式,可显示扩展名为avi,dat,wav和mid文件 CommonDialog1.Filter = "(*.avi)|*.avi|(*.wave)|*.wav|(vcd *.dat)|*.dat|(midi *.mid)|*.mid" '初始化文件过滤方式为*.avi CommonDialog1.FilterIndex = 1 '建立打开方式的通用对话框,也可使用commondialog1.showopen CommonDialog1.Action = 1 '打开一个文件前先关闭前一次被打开的多媒体设备 mand = "close" Select CommonDialog1.FilterIndex Case 1 '选择*.avi '设置多媒体设备类型为avividio MMControl1.DeviceType = "avividio" '设置时间格式为帧 MMControl1.TimeFormat = 3 '设置播放的文件为通用对话框中选择的文件 MMControl1.FileName = CommonDialog1.FileName '打开文件 mand = "open " Case 2 '选择*.wav '设置多媒体设备类型为waveaudio MMControl1.DeviceType = "waveaudio" '设置时间格式为帧 MMControl1.TimeFormat = 3 '设置播放的文件为通用对话框中选择的文件 MMControl1.FileName = CommonDialog1.FileName '打开文件 mand = "open " Case 3 '选择*.dat '设置多媒体设备类型为Mpegvidio MMControl1.DeviceType = "Mpegvidio" '设置时间格式为帧 MMControl1.TimeFormat = 3 '设置播放的文件为通用对话框中选择的文件 MMControl1.FileName = CommonDialog1.FileName '打开文件 mand = "open " Case 4 '选择*.mid '设置多媒体设备类型为waveaudio MMControl1.DeviceType = "waveaudio" '设置时间格式为帧 MMControl1.TimeFormat = 3 '设置播放的文件为通用对话框中选择的文件 MMControl1.FileName = CommonDialog1.FileName '打开文件 mand = "open " End Select '设置hwnddisplay的值,使媒体文件能够在picture控件中播放 MMControl1.hWndDisplay = Picture1.hWnd End Sub (二)新建、修改、删除目录 以上控件除了通用对话框(CommonDialog)之外一般只能显示当前的目录结构,对于在磁盘上新建、修改、删除目录却基本无能为力。
vb 取得进程地址
Dim lngNumElements As Long
Dim lngProcessIDs() As Long
Public Const PROCESS_QUERY_INFORMATION = 1024
Public Const PROCESS_VM_READ = 16
Public Const MAX_PATH = 260
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
dwAvailPhys As Long
dwTotalPageFile As Long
dwAvailPageFile As Long
dwTotalVirtual As Long
dwAvailVirtual As Long
dwFlags As Long
szExeFile As String * 260 ' MAX_PATH
End Type
Public Type OSVERSIONINFO
dwOSVersionInfoSize As Long
Dim booResult As Boolean
Dim lngLength As Long
Dim lngProcessID As Long
Dim strProcessName As String
cntThreads As Long
th32ParentProcessID As Long ' This process's parent process
VB获取系统各类文件夹路径
VB获取系统各类文件夹路径Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As LongPrivate Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As LongPrivate Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As LongPrivate Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As LongPrivate Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As LongConst MAX_LEN = 200 '字符串最大长度Const DESKTOP = &H0& '桌面Const PROGRAMS = &H2& '程序集Const MYDOCUMENTS = &H5& '我的文档Const MYFAVORITES = &H6& '收藏夹Const STARTUP = &H7& '启动Const RECENT = &H8& '最近打开的文件Const SENDTO = &H9& '发送Const STARTMENU = &HB& '开始菜单Const NETHOOD = &H13& '网上邻居Const FONTS = &H14& '字体Const SHELLNEW = &H15& 'ShellNewConst APPDATA = &H1A& 'Application DataConst PRINTHOOD = &H1B& 'PrintHoodConst PAGETMP = &H20& '网页临时文件Const COOKIES = &H21& 'Cookies目录Const HISTORY = &H22& '历史Private Sub Form_Load()Dim sTmp As String * MAX_LEN '存放结果的固定长度的字符串Dim nLength As Long '字符串的实际长度Dim pidl As Long '某特殊目录在特殊目录列表中的位置'*************************获得Windows目录**********************************Length = GetWindowsDirectory(sTmp, MAX_LEN)txtWin.Text = Left(sTmp, Length)'*************************获得System目录***********************************Length = GetSystemDirectory(sTmp, MAX_LEN)txtSystem.T ext = Left(sTmp, Length)'*************************获得Temp目录***********************************Length = GetTempPath(MAX_LEN, sTmp)txtTemp.Text = Left(sTmp, Length)'*************************获得DeskT op目录**********************************SHGetSpecialFolderLocation 0, DESKTOP, pidlSHGetPathFromIDList pidl, sTmptxtDesktop.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'*************************获得发送到目录**********************************SHGetSpecialFolderLocation 0, SENDTO, pidlSHGetPathFromIDList pidl, sTmptxtSendT o.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'*************************获得我的文档目录*********************************SHGetSpecialFolderLocation 0, MYDOCUMENTS, pidlSHGetPathFromIDList pidl, sTmptxtDocument.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'*************************获得程序集目录***********************************SHGetSpecialFolderLocation 0, PROGRAMS, pidlSHGetPathFromIDList pidl, sTmptxtProgram.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'*************************获得启动目录*************************************SHGetSpecialFolderLocation 0, STARTUP, pidlSHGetPathFromIDList pidl, sTmptxtStart.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'*************************获得开始菜单目录*********************************SHGetSpecialFolderLocation 0, STARTMENU, pidlSHGetPathFromIDList pidl, sTmptxtStartMenu.T ext = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'*************************获得收藏夹目录***********************************SHGetSpecialFolderLocation 0, MYFAVORITES, pidlSHGetPathFromIDList pidl, sTmptxtFavorites.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'**********************获得最后打开的文件目录*******************************SHGetSpecialFolderLocation 0, RECENT, pidlSHGetPathFromIDList pidl, sTmptxtRecent.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'*************************获得网上邻居目录*********************************SHGetSpecialFolderLocation 0, NETHOOD, pidlSHGetPathFromIDList pidl, sTmptxtNetHood.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'*************************获得字体目录**********************************SHGetSpecialFolderLocation 0, FONTS, pidlSHGetPathFromIDList pidl, sTmptxtFonts.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'*************************获得Cookies目录**********************************SHGetSpecialFolderLocation 0, COOKIES, pidlSHGetPathFromIDList pidl, sTmptxtCookies.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'*************************获得历史目录**********************************SHGetSpecialFolderLocation 0, HISTORY, pidlSHGetPathFromIDList pidl, sTmptxtHistory.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'***********************获得网页临时文件目录*******************************SHGetSpecialFolderLocation 0, PAGETMP, pidlSHGetPathFromIDList pidl, sTmptxtPageTmp.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'*************************获得ShellNew目录*********************************SHGetSpecialFolderLocation 0, SHELLNEW, pidlSHGetPathFromIDList pidl, sTmptxtShellNew.T ext = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'***********************获得Application Data目录*****************************SHGetSpecialFolderLocation 0, APPDATA, pidlSHGetPathFromIDList pidl, sTmptxtAppData.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)'*************************获得PrintHood目录*********************************SHGetSpecialFolderLocation 0, PRINTHOOD, pidlSHGetPathFromIDList pidl, sTmptxtPrintHood.T ext = Left(sTmp, InStr(sTmp, Chr(0)) - 1)End Sub。
VBA 中的文件夹创建与目录枚举技巧
VBA 中的文件夹创建与目录枚举技巧VBA(Visual Basic for Applications)是一种宏语言,常用于Microsoft Office软件中,尤其是Excel。
文件处理是编程中常见的任务之一,而在文件处理中,文件夹的创建和目录的枚举是两个常用的操作。
本文将介绍在VBA中如何实现文件夹的创建与目录的枚举,并提供一些实用的技巧。
一、文件夹的创建文件夹的创建是指在指定的路径下创建一个新的文件夹。
在VBA中,我们可以使用FileSystemObject对象来实现文件夹的创建。
以下是一个简单的示例代码:```vbaSub CreateFolder()Dim fso As ObjectDim folderPath As String'指定文件夹路径folderPath = "C:\MyFolder"'创建FileSystemObject对象Set fso = CreateObject("Scripting.FileSystemObject")'判断文件夹是否存在,如果不存在则创建If Not fso.FolderExists(folderPath) Thenfso.CreateFolder folderPathMsgBox "文件夹创建成功!"ElseMsgBox "文件夹已存在!"End If'释放对象Set fso = NothingEnd Sub```在上述代码中,我们声明了一个FileSystemObject对象,然后指定了要创建的文件夹的路径。
接下来,通过判断文件夹是否存在来决定是否创建文件夹。
如果文件夹不存在,则调用CreateFolder方法创建文件夹。
最后,通过释放对象来清理内存。
二、目录的枚举目录的枚举是指列出指定路径下的所有文件和文件夹。
同样地,在VBA中,我们可以使用FileSystemObject对象进行目录的枚举。
VB代码VB小程序:通过枚举进程显示所有进程、隐藏进程、进程路径
VB代码VB小程序:通过枚举进程显示所有进程、隐藏进程、进程路径本小程序采用枚举进程的方法,显示所有进程,也能显示隐藏进程。
同时,能显示进程的完整路径。
有意思的是,一些已经结束的进程,同样可以显示。
以下是程序运行截图:''''以下是 VB6 代码,在 WinXP 调试通过'需在窗体放置以下 5 个控件,不必设置任何属性,全部采用默认设置:' Command1、List1、Check1、Timer1、Label1'本人原创,转载请注明来源:/100bd/blog/item/c4199fedda35ba0763d09f6b.htmlPrivate Type tyProcpID As Long: pName As String: pPath As String: pHide As StringEnd TypeDim ctP() As tyProc, ctPs As LongPrivate Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As LongPrivate Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPrivate Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef lpcbNeeded As Long) As LongPrivate Declare Function GetModuleFileNameEx Lib "psapi.dll" Alias"GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As String, ByVal nSize As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As LongPrivate Declare Function GetProcessImageFileName Lib "psapi.dll" Alias"GetProcessImageFileNameA" (ByVal hProcess As Long, ByVal lpImageFileName As String, ByVal nSize As Long) As LongPrivate Const PROCESS_ALL_ACCESS = &H1F0FFFPrivate Const PROCESS_QUERY_INFORMATION = &H400Private Const PROCESS_VM_READ = &H10Private Const PROCESS_CREATE_PROCESS = &H80Private Const PROCESS_CREATE_THREAD = &H2Private Const PROCESS_DUP_HANDLE = &H40'Private Const PROCESS_QUERY_INFORMATION = &H400Private Const PROCESS_QUERY_LIMITED_INFORMATION = &H1000Private Const PROCESS_SET_QUOTA = &H100Private Const PROCESS_SET_INFORMATION = &H200Private Const PROCESS_SUSPEND_RESUME = &H800Private Const PROCESS_TERMINATE = &H1Private Const PROCESS_VM_OPERATION = &H8'Private Const PROCESS_VM_READ = &H10Private Const PROCESS_VM_WRITE = &H20'以下是在 NT 系统中提升当前进程权限的代码================================'系统级权限,可以:PROCESS_ALL_ACCESS OpenProcessToken、LookupPrivilegevalue、AdjustTokenPrivilegesPrivate Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As LongPrivate Declare Function GetCurrentProcess Lib "kernel32" () As LongPrivate Declare Function LookupPrivilegeValue Lib "advapi32" Alias"LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As LongPrivate Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_Privileges, ByVal BufferLength As Long, PreviousState As TOKEN_Privileges, ReturnLength As Long) As LongPrivate Type LUIDUsedPart As LongIgnoredForNowHigh32BitPart As LongEnd TypePrivate Type TOKEN_PrivilegesPrivilegeCount As LongTheLuid As LUIDAttributes As LongEnd TypePublic Sub AdjustPrivilege()'NT系统:提升权限Dim dl As Long, CurP As Long, nToKen As Long, nLuid As LUIDDim OldTKP As TOKEN_Privileges, NewTKP As TOKEN_PrivilegesDim pName As StringConst TOKEN_Adjust_Privileges = &H20Const TOKEN_Query = &H8Const SE_Privilege_Enabled_BY_DEFAULT = &H1 '默认权限Const SE_Privilege_Enabled = &H2 '开启权限Const SE_Privilege_USED_FOR_ACCESS = &H80000000 '所有访问权限'获取当前进程的一个句柄CurP = GetCurrentProcess()'打开进程令牌:用 nToKen 获得进程访问令牌的句柄dl = OpenProcessToken(CurP, (TOKEN_Adjust_Privileges Or TOKEN_Query), nToKen)'用 nLuid 返回指定权限的 LUID 结构'权限名称:SeDebugPrivilege、SeShutdownPrivilege、SeRestorePrivilege、SeBackupPrivilege、SeUnsolicitedInputPrivilegepName = "SeDebugPrivilege"dl = LookupPrivilegeValue("", pName, nLuid)NewTKP.PrivilegeCount = 1NewTKP.TheLuid = nLuidNewTKP.Attributes = SE_Privilege_Enabled'调整令牌权限dl = AdjustTokenPrivileges(nToKen, False, NewTKP, Len(NewTKP), OldTKP, 0&) End Sub'===================Private Sub Form_Load() = "宋体": Me.Caption = "枚举进程"Command1.Caption = "刷新" = Call AdjustPrivilege '提升本进程权限Timer1.Interval = 10Check1.Caption = "自动刷新": Check1.Value = 1End SubPrivate Sub Check1_Click()Timer1.Enabled = Check1.Value = 1End SubPrivate Sub Timer1_Timer()Static S As Long, S1 As LongDim nTai As StringS1 = S1 + 1If S1 > 2 ThenS1 = 0nTai = "↖↑↗→↘↓↙←"S = S + 1If S > 8 Then S = 1Label1.Caption = Mid(nTai, S, 1) '动画显示End IfCall ShowProcEnd SubPrivate Sub Command1_Click()List1.Clear: List1.RefreshCall ShowProcEnd SubPrivate Sub Form_Resize()Dim H1 As Single, T As SingleOn Error Resume NextH1 = Me.TextHeight("A")Command1.Move H1, H1, H1 * 4, H1 * 2Label1.Move H1 * 6, H1 * 1.5, H1, H1Check1.Move H1 * 8, H1, H1 * 8, H1 * 2T = Command1.Top + Command1.Height + H1 * 0.5List1.Move 0, T, Me.ScaleWidth, Me.ScaleHeight - TEnd Sub'本人原创,转载请注明来源:/100bd/blog/item/c4199fedda35ba0763d09f6b.html Private Sub ShowProc()Dim pID(1023) As Long, Ps As Long, dwDesiredAccess As LongDim cbNeeded As Long, P As Long, hModule As LongDim hProcess As Long, nStr As String, I As LongDim IsChange As Boolean, P2() As tyProc, Ps2 As LongOn Error Resume NextdwDesiredAccess = PROCESS_QUERY_INFORMATION Or PROCESS_VM_READPs2 = ctPs: P2 = ctPctPs = 1: ReDim ctP(0 To 1)ctP(1).pName = "[System Process]"nStr = String(1024, 0)' 进程ID的数组,数组的大小,返回实际进程数组的大小If EnumProcesses(pID(0), 4& * 1024, cbNeeded) <> 0 ThenPs = cbNeeded \ 4 '进程总数For P = 0 To &HFFFF& Step 4hProcess = OpenProcess(dwDesiredAccess, 0, P) '返回指定进程的句柄If hProcess <> 0 ThenctPs = ctPs + 1: ReDim Preserve ctP(0 To ctPs)ctP(ctPs).pHide = "隐藏"For I = 0 To Ps - 1If P = pID(I) Then ctP(ctPs).pHide = "": Exit ForNext I'nStr 返回主模块全名:每个进程的第一模块即为进程主模块If EnumProcessModules(hProcess, hModule, 4&, 0&) <> 0 Then GetModuleFileNameEx hProcess, hModule, nStr, 1024Else '型如:\Device\HarddiskVolumeGetProcessImageFileName hProcess, nStr, 1024End IfCloseHandle hProcess '关闭进程的句柄With ctP(ctPs).pID = P '进程 ID.pPath = CutStr(nStr, vbNullChar) '进程路径If Left(.pPath, 4) = "\??\" Then .pPath = Mid(.pPath, 5) '去掉“\??\”.pName = CutStr(.pPath, "\", True) '进程名If P = 4 And .pName = "" Then .pName = "System"End WithEnd IfNextEnd If'List1.ClearFor P = 1 To ctPsnStr = AddSpace(P, 4) & ProcStr(ctP(P)) '合成显示条目If P > List1.ListCount ThenList1.AddItem nStr' List1.ListIndex = List1.NewIndexElseIf nStr <> List1.List(P - 1) Then List1.List(P - 1) = nStrEnd IfNext'删除多余条目For P = List1.ListCount - 1 To ctPs Step -1List1.RemoveItem PNextEnd SubPrivate Function ProcStr(P As tyProc) As StringProcStr = AddSpace(P.pID) & AddSpace(P.pHide, 6) & AddSpace(P.pName, 20) & AddSpace(P.pPath)End FunctionPrivate Function AddSpace(ByVal nStr As String, Optional ByVal S As Long) As StringIf S < 1 Then S = 6S = S - LenB(StrConv(nStr, vbFromUnicode))If S < 1 Then S = 1AddSpace = nStr & String(S, " ")End FunctionPrivate Function CutStr(nStr As String, Fu As String, Optional GetRight As Boolean) As String'GetRight=T 从右到左查找Dim S As LongIf GetRight Then ' 从右到左查找S = InStrRev(nStr, Fu)If S > 0 Then CutStr = Mid(nStr, S + 1) Else CutStr = nStrElseS = InStr(nStr, Fu)If S > 0 Then CutStr = Left(nStr, S - 1) Else CutStr = nStrEnd IfEnd Function'本人原创,转载请注明来源:/100bd/blog/item/c4199fedda35ba0763d09f6b.html查看文档来源:/100bd/item/fbd87c3004c6f5342e0f8140。
用VB枚举系统进程PID及路径
用VB枚举系统进程PID及路径代码如下:‘在声明中添加以下代码Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As LongPrivate Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As LongPrivate Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As LongPrivate Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As LongPrivate Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long‘上为CloseHandle(),OpenProcess(),EnumProcesses(),GetModuleFileNameExA()EnumProcessModules()函数的声明,在此程序中EnumProcessModules()为非必要,枚举进程路径,并不需要去枚举进程的模块。
用VB检测到局域网中的所有电脑名称和IP并显示在列表框中源代码
用VB检测到局域网中的所有电脑名称和IP并显示在列表框中源代码Public Type WKSTA_USER_INFO_1 wkui1_username As Long wkui1_logon_domain As Longwkui1_oth_domains As Longwkui1_logon_server As LongEnd TypePublic Type Server_InfoPlatform_id As LongSvrName As StringVersion_major As LongVersion_minor As LongnType As LongComment As StringEnd TypePublic Type G_Grp_InfoGrpName As StringGrpComment As StringEnd TypePublic Type OS_VER_INFO dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As StringEnd TypePublic Type USER_INFO strWkstaName As String '工作站名称strLanGrp As String '所属的工作组名称strUsrName As String '用户名称strDomainName As String '网域名称strSvrName As String '网域PDC strUsrFullName As String '用户全名strPassword As String '保留End TypePublic tUsrInfo As USER_INFOSub Main().....tUsrInfo = GetNUsrInfo ......End SubPublic Function GetNUsrInfo() As USER_INFO......Dim tdfRetInfo As WKSTA_USER_INFO_1, nPtrRetInfo As Long .............NetWkstaUserGetInfo ByVal 0&, 1, nPtrRetInfoRtlMoveMemory tdfRetInfo, ByVal nPtrRetInfo, Len(tdfRetInfo)'将nPtrRetInfo指针指向的内存块复制到tdfRetInfo中lstrcpy bBufferA(0), tdfRetInfo.wkui1_logon_domain'将wkui1_logon_domain指针指向的内存块复制到一个字节数组中'***********************************************'这段代码将NT的NULL TERMINA TE的UNICODE字符串转化为VB的字符串strDomainName = bBufferAstrDomainName = Left(strDomainName, InStr(strDomainName, Chr(0)) - 1)*this is the USER_INFO.strDomainName........END SUBUse netServerEnum.Sample:Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As LongPublic Declare Function PtrT oStr Lib "kernel32" Alias"lstrcpyW" (retVal As Byte, ByVal Ptr As Long) As Long Public Declare Function StrToPtr Lib "kernel32" Alias "lstrcpyW" (ByVal Ptr As Long, Source As Byte) As Long Public Declare Function PtrToInt Lib "kernel32" Alias "lstrcpynW" (retVal As Any, ByVal Ptr As Long, ByVal nCharCount As Long) As LongPublic Declare Function StrLen Lib "kernel32" Alias "lstrlenW" (ByVal Ptr As Long) As Long Public OK As BooleanPrivate Sub Form_Load()Dim sBuffer As StringDim lSize As LongCall FillCboSvrsEnd SubSub FillCboSvrs()cboSvrs.ClearDim tSvrInfo As Server_InfoDim strDomainName As StringDim result As Long, bufptr As Long, entriesread As Long, totalentries As Long, resumehandle As Long, BufLen As Long, _ DNArray() As Byte, SNArray(99) As Byte, UNArray() As Byte, _ SName As String, i As Integer, UNPtr As Long, nLevel As Long, nSvrType As Long, _TempPtr As MungeLong, TempStr As MungeIntDim nPlatform_id As Long, strSvrName As String, nVersion_major As Long, nVersion_minor As Long, nType As Long, strComment As StringDim SvrInfoArray() As Server_InfonSvrType = SV_TYPE_ALL' UNArray = UName & vbNullChar ' Move to Byte arrayBufLen = -1 ' Buffer sizeresumehandle = 0 ' Start with the first entrynLevel = 101strDomainName = tUsrInfo.strDomainNameDNArray = strDomainName & vbNullChar ' Move to byte arrayDoIf Len(Trim(strDomainName)) > 0 Thenresult = NetServerEnum(ByVal 0, nLevel, bufptr, BufLen, entriesread, totalentries, nSvrType, DNArray(0), resumehandle) Elseresult = NetServerEnum(ByVal 0, nLevel, bufptr, BufLen, entriesread, totalentries, nSvrType, ByVal 0, resumehandle) End IfIf result <> ERROR_SUCCESS And result <> ERROR_MORE_DATA Then' requiredMsgBox ("Error " & result & " enumerating group " & entriesread & " of " & totalentries)Exit SubEnd IfDim j As LongFor i = 1 To entriesread' Get pointer to string from beginning of buffer' Copy 4 byte block of memory each timej = (i - 1) * 6result = PtrToInt(T empPtr.X, bufptr + j * 4, 4)nPlatform_id = TempPtr.Xresult = PtrToInt(T empPtr.X, bufptr + (j + 1) * 4, 4)' Copy string to array and convert to a stringresult = PtrToStr(SNArray(0), TempPtr.X)strSvrName = Left(SNArray, StrLen(TempPtr.X))result = PtrT oInt(TempPtr.X, bufptr + (j + 2) * 4, 4) nVersion_major = TempPtr.Xresult = PtrT oInt(TempPtr.X, bufptr + (j + 3) * 4, 4) nVersion_minor = TempPtr.Xresult = PtrToInt(T empPtr.X, bufptr + (j + 4) * 4, 4)nType = TempPtr.Xresult = PtrToInt(T empPtr.X, bufptr + (j + 5) * 4, 4)result = PtrToStr(SNArray(0), TempPtr.X)strComment = Left(SNArray, StrLen(TempPtr.X))'Add A Record Into Return RecordsetcboSvrs.AddItem strSvrNameNext iresult = NetAPIBufferFree(bufptr)Loop Until entriesread = totalentries' The above condition only valid for reading accounts on NT ' but not OK for OS/2 or LanMan' result = NetAPIBufferFree(bufptr) ' Don't leak memory End Sub。
VB程式如何取得 硬碟 及 主机板 序号
如何取得硬碟及主機板序號VB使用WMI ( Windows Management Instrumentation ) 來取得<< >> 寫法1Imports System.Runtime.InteropServicesPublic Class Form1Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load MessageBox.Show("硬碟序號: " & Get_PscDrv_SNo(0) & _ControlChars.CrLf & ControlChars.CrLf & _"主機板序號: " & Get_MB_SNo())End Sub' 取得硬碟序號( 實體磁碟)' 當有多顆硬碟時, 可調傳入參數為1 , 2 .. 依此類推Private Function Get_PscDrv_SNo(ByVal DrvIdx As Byte) As StringDim WMI As Object = GetObject("winmgmts:") ' 取得WMI 物件' GetObject : 傳回COM 元件所提供物件的參考。
Dim strCls As String = "Win32_PhysicalMedia"' WMI 類別Dim strKey As String = strCls & ".Tag=""\\\\.\\PHYSICALDRIVE" & DrvIdx & """"Return WMI.InstancesOf(strCls)(strKey).SerialNumber.ToString.Trim ' 取得SerialNumber 屬性Marshal.ReleaseComObject(WMI) ' 釋放Com 物件所使用的資源End Function' 取得主機板序號Private Function Get_MB_SNo() As StringDim WMI As Object = GetObject("winmgmts:") ' 取得WMI 物件' GetObject : 傳回COM 元件所提供物件的參考。
VB使用NtQuerySystemInformation枚举进程和线程
LsBox.AddItem "页面错误:" & .PageFaultCount
LsBox.AddItem "内存峰值:" & .PeakWorkingSetSize / 1024 & "K"
LsBox.AddItem "非页面缓冲池峰值:" & .QuotaPeakNonPagedPoolUsage / 1024 & "K"
LsBox.AddItem "非页面缓冲池:" & .QuotaNonPagedPoolUsage / 1024 & "K"
Reserved2(1) As Long
VmCounters As VM_COUNTERS
IoCounters As IO_COUNTERS
End Type
Public Sub EnumProcessAndThread(ByVal LsBox As ListBox)
Dim dwLen As Long
PeakPageFileUsage As Long
PrivatePageCount As Long
End Type
Private Type IO_COUNTERS
ReadOperationCount As LARGE_INTEGER
WriteOperationCount As LARGE_INTEGER
Private Type VM_COUNTERS
VBA获取系统环境变量及特殊文件夹的各种代码与方法(转载)
VBA获取系统环境变量及特殊⽂件夹的各种代码与⽅法(转载)这⼏天帮客户做个系统,需要获到系统环境变量及⼀些特殊⽂件夹。
收集和研究了各种代码。
记录⼀下,以免⾃⼰忘记,同时也分享⼀下给⼤家,避免⼤家遇到同样问题时,再去花费⼤量时间。
⼀、我个⼈整理出来的⼀些⼼得和经验:1.Environ("AllUsersProfile") 在XP下获取的是 C:\Documents and Settings\All Users在win7 win8下获取的是C:\ProgramData有所不同,这个差异请注意2.XP 与win7 ⼤多的变量是可以对应的,但也有些是对应不到的环境变量(environment variables)⼀般是指在操作系统中⽤来指定操作系统运⾏环境的⼀些参数,如:临时⽂件夹位置和系统⽂件夹位置等。
环境变量是在操作系统中⼀个具有特定名字的对象,它包含了⼀个或者多个应⽤程序所将使⽤到的信息。
例如Windows和DOS操作系统中的path环境变量,当要求系统运⾏⼀个程序⽽没有告诉它程序所在的完整路径时,系统除了在当前⽬录下⾯寻找此程序外,还应到path中指定的路径去找。
⽤户通过设置环境变量,来更好的运⾏进程。
⼆、常⽤windows系统环境变量清单%USERPROFILE% 表⽰ C://Documents and Settings//当前⽤户名%ALLUSERSPROFILE% 表⽰ C://Documents and Settings//All Users%APPDATA% 表⽰ C://Documents and Settings//当前⽤户名//Application Data%ALLAPPDATA% 表⽰ C://Documents and Settings//All Users//Application Data%SYSTEMDRIVE% 表⽰ C:%HOMEDRIVE% 表⽰ C://%SYSTEMROOT% 表⽰ C://WINDOWS%WINDIR% 表⽰ C://WINDOWS%TEMP% 和 %TMP% 表⽰ C://Documents and Settings//当前⽤户名//Local Settings//Temp%ProgramFiles% 表⽰ C://Program Files%CommonProgramFiles% 表⽰ C://Program Files//Common Files设置环境变量有两种⽅式:第⼀种是在命令提⽰符运⾏窗⼝中设置;第⼆种是通过单击“我的电脑→属性→⾼级”标签的“环境变量”按钮设置。
VB代码VB小程序:通过枚举进程显示所有进程、隐藏进程、进程路径
VB代码VB小程序:通过枚举进程显示所有进程、隐藏进程、进程路径本小程序采用枚举进程的方法,显示所有进程,也能显示隐藏进程。
同时,能显示进程的完整路径。
有意思的是,一些已经结束的进程,同样可以显示。
以下是程序运行截图:''''以下是 VB6 代码,在 WinXP 调试通过'需在窗体放置以下 5 个控件,不必设置任何属性,全部采用默认设置:' Command1、List1、Check1、Timer1、Label1'本人原创,转载请注明来源:/100bd/blog/item/c4199fedda35ba0763d09f6b.htmlPrivate Type tyProcpID As Long: pName As String: pPath As String: pHide As StringEnd TypeDim ctP() As tyProc, ctPs As LongPrivate Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As LongPrivate Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPrivate Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef lpcbNeeded As Long) As LongPrivate Declare Function GetModuleFileNameEx Lib "psapi.dll" Alias"GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As String, ByVal nSize As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As LongPrivate Declare Function GetProcessImageFileName Lib "psapi.dll" Alias"GetProcessImageFileNameA" (ByVal hProcess As Long, ByVal lpImageFileName As String, ByVal nSize As Long) As LongPrivate Const PROCESS_ALL_ACCESS = &H1F0FFFPrivate Const PROCESS_QUERY_INFORMATION = &H400Private Const PROCESS_VM_READ = &H10Private Const PROCESS_CREATE_PROCESS = &H80Private Const PROCESS_CREATE_THREAD = &H2Private Const PROCESS_DUP_HANDLE = &H40'Private Const PROCESS_QUERY_INFORMATION = &H400Private Const PROCESS_QUERY_LIMITED_INFORMATION = &H1000Private Const PROCESS_SET_QUOTA = &H100Private Const PROCESS_SET_INFORMATION = &H200Private Const PROCESS_SUSPEND_RESUME = &H800Private Const PROCESS_TERMINATE = &H1Private Const PROCESS_VM_OPERATION = &H8'Private Const PROCESS_VM_READ = &H10Private Const PROCESS_VM_WRITE = &H20'以下是在 NT 系统中提升当前进程权限的代码================================'系统级权限,可以:PROCESS_ALL_ACCESS OpenProcessToken、LookupPrivilegevalue、AdjustTokenPrivilegesPrivate Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As LongPrivate Declare Function GetCurrentProcess Lib "kernel32" () As LongPrivate Declare Function LookupPrivilegeValue Lib "advapi32" Alias"LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As LongPrivate Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_Privileges, ByVal BufferLength As Long, PreviousState As TOKEN_Privileges, ReturnLength As Long) As LongPrivate Type LUIDUsedPart As LongIgnoredForNowHigh32BitPart As LongEnd TypePrivate Type TOKEN_PrivilegesPrivilegeCount As LongTheLuid As LUIDAttributes As LongEnd TypePublic Sub AdjustPrivilege()'NT系统:提升权限Dim dl As Long, CurP As Long, nToKen As Long, nLuid As LUIDDim OldTKP As TOKEN_Privileges, NewTKP As TOKEN_PrivilegesDim pName As StringConst TOKEN_Adjust_Privileges = &H20Const TOKEN_Query = &H8Const SE_Privilege_Enabled_BY_DEFAULT = &H1 '默认权限Const SE_Privilege_Enabled = &H2 '开启权限Const SE_Privilege_USED_FOR_ACCESS = &H80000000 '所有访问权限'获取当前进程的一个句柄CurP = GetCurrentProcess()'打开进程令牌:用 nToKen 获得进程访问令牌的句柄dl = OpenProcessToken(CurP, (TOKEN_Adjust_Privileges Or TOKEN_Query), nToKen)'用 nLuid 返回指定权限的 LUID 结构'权限名称:SeDebugPrivilege、SeShutdownPrivilege、SeRestorePrivilege、SeBackupPrivilege、SeUnsolicitedInputPrivilegepName = "SeDebugPrivilege"dl = LookupPrivilegeValue("", pName, nLuid)NewTKP.PrivilegeCount = 1NewTKP.TheLuid = nLuidNewTKP.Attributes = SE_Privilege_Enabled'调整令牌权限dl = AdjustTokenPrivileges(nToKen, False, NewTKP, Len(NewTKP), OldTKP, 0&) End Sub'===================Private Sub Form_Load() = "宋体": Me.Caption = "枚举进程"Command1.Caption = "刷新" = Call AdjustPrivilege '提升本进程权限Timer1.Interval = 10Check1.Caption = "自动刷新": Check1.Value = 1End SubPrivate Sub Check1_Click()Timer1.Enabled = Check1.Value = 1End SubPrivate Sub Timer1_Timer()Static S As Long, S1 As LongDim nTai As StringS1 = S1 + 1If S1 > 2 ThenS1 = 0nTai = "↖↑↗→↘↓↙←"S = S + 1If S > 8 Then S = 1Label1.Caption = Mid(nTai, S, 1) '动画显示End IfCall ShowProcEnd SubPrivate Sub Command1_Click()List1.Clear: List1.RefreshCall ShowProcEnd SubPrivate Sub Form_Resize()Dim H1 As Single, T As SingleOn Error Resume NextH1 = Me.TextHeight("A")Command1.Move H1, H1, H1 * 4, H1 * 2Label1.Move H1 * 6, H1 * 1.5, H1, H1Check1.Move H1 * 8, H1, H1 * 8, H1 * 2T = Command1.Top + Command1.Height + H1 * 0.5List1.Move 0, T, Me.ScaleWidth, Me.ScaleHeight - TEnd Sub'本人原创,转载请注明来源:/100bd/blog/item/c4199fedda35ba0763d09f6b.html Private Sub ShowProc()Dim pID(1023) As Long, Ps As Long, dwDesiredAccess As LongDim cbNeeded As Long, P As Long, hModule As LongDim hProcess As Long, nStr As String, I As LongDim IsChange As Boolean, P2() As tyProc, Ps2 As LongOn Error Resume NextdwDesiredAccess = PROCESS_QUERY_INFORMATION Or PROCESS_VM_READPs2 = ctPs: P2 = ctPctPs = 1: ReDim ctP(0 To 1)ctP(1).pName = "[System Process]"nStr = String(1024, 0)' 进程ID的数组,数组的大小,返回实际进程数组的大小If EnumProcesses(pID(0), 4& * 1024, cbNeeded) <> 0 ThenPs = cbNeeded \ 4 '进程总数For P = 0 To &HFFFF& Step 4hProcess = OpenProcess(dwDesiredAccess, 0, P) '返回指定进程的句柄If hProcess <> 0 ThenctPs = ctPs + 1: ReDim Preserve ctP(0 To ctPs)ctP(ctPs).pHide = "隐藏"For I = 0 To Ps - 1If P = pID(I) Then ctP(ctPs).pHide = "": Exit ForNext I'nStr 返回主模块全名:每个进程的第一模块即为进程主模块If EnumProcessModules(hProcess, hModule, 4&, 0&) <> 0 Then GetModuleFileNameEx hProcess, hModule, nStr, 1024Else '型如:\Device\HarddiskVolumeGetProcessImageFileName hProcess, nStr, 1024End IfCloseHandle hProcess '关闭进程的句柄With ctP(ctPs).pID = P '进程 ID.pPath = CutStr(nStr, vbNullChar) '进程路径If Left(.pPath, 4) = "\??\" Then .pPath = Mid(.pPath, 5) '去掉“\??\”.pName = CutStr(.pPath, "\", True) '进程名If P = 4 And .pName = "" Then .pName = "System"End WithEnd IfNextEnd If'List1.ClearFor P = 1 To ctPsnStr = AddSpace(P, 4) & ProcStr(ctP(P)) '合成显示条目If P > List1.ListCount ThenList1.AddItem nStr' List1.ListIndex = List1.NewIndexElseIf nStr <> List1.List(P - 1) Then List1.List(P - 1) = nStrEnd IfNext'删除多余条目For P = List1.ListCount - 1 To ctPs Step -1List1.RemoveItem PNextEnd SubPrivate Function ProcStr(P As tyProc) As StringProcStr = AddSpace(P.pID) & AddSpace(P.pHide, 6) & AddSpace(P.pName, 20) & AddSpace(P.pPath)End FunctionPrivate Function AddSpace(ByVal nStr As String, Optional ByVal S As Long) As StringIf S < 1 Then S = 6S = S - LenB(StrConv(nStr, vbFromUnicode))If S < 1 Then S = 1AddSpace = nStr & String(S, " ")End FunctionPrivate Function CutStr(nStr As String, Fu As String, Optional GetRight As Boolean) As String'GetRight=T 从右到左查找Dim S As LongIf GetRight Then ' 从右到左查找S = InStrRev(nStr, Fu)If S > 0 Then CutStr = Mid(nStr, S + 1) Else CutStr = nStrElseS = InStr(nStr, Fu)If S > 0 Then CutStr = Left(nStr, S - 1) Else CutStr = nStrEnd IfEnd Function'本人原创,转载请注明来源:/100bd/blog/item/c4199fedda35ba0763d09f6b.html查看文档来源:/100bd/item/fbd87c3004c6f5342e0f8140。
vb 调用路径 -回复
vb 调用路径-回复标题:VB调用路径的深度解析一、引言Visual Basic(简称VB)是由Microsoft公司开发的一种基于Basic语言的可视化编程工具。
它以其简单易学、操作方便、功能强大的特点,深受广大程序设计人员的喜爱。
在使用VB进行编程时,我们需要调用各种外部文件或模块,这就涉及到路径的问题。
本文将详细介绍如何在VB 中调用路径。
二、基本概念1. 路径:在计算机科学中,路径是指从一个节点到另一个节点的一系列边的集合。
在文件系统中,路径是文件或目录的位置标识。
2. VB中的路径:在VB中,我们可以通过设置路径来指定要访问的文件或目录的位置。
三、VB中的路径类型在VB中,有三种类型的路径:1. 绝对路径:这是完整的路径,包括驱动器名、根目录和文件名。
例如"C:\Users\Documents\MyFile.txt"。
2. 相对路径:这是相对于当前工作目录的路径。
例如"MyFolder\MyFile.txt"。
3. 网络路径:用于访问网络上的共享资源。
例如"\\ServerName\FolderName\File.txt"。
四、VB中如何设置路径1. 使用绝对路径:直接在代码中写出完整的路径即可。
vbOpen "C:\Users\Documents\MyFile.txt" For Input As #12. 使用相对路径:首先需要确定当前的工作目录,然后在此基础上添加相对路径。
vbChDir "C:\Users\Documents"Open "MyFile.txt" For Input As #13. 使用网络路径:与绝对路径类似,只是路径开头是网络服务器的名称。
vbOpen "\\ServerName\FolderName\MyFile.txt" For Input As #1五、VB中的路径函数VB提供了一些内置的函数来处理路径:1. CurDir():返回当前工作目录。
VBA中的文件路径与目录操作技巧
VBA中的文件路径与目录操作技巧VBA(Visual Basic for Applications)是一种编程语言,用于在Microsoft Office中自定义宏和程序。
在进行文件处理时,了解和熟练掌握VBA中的文件路径和目录操作技巧是非常重要的。
本文将介绍一些在VBA中处理文件路径和目录的常用技巧和方法。
1. 获取当前工作目录使用VBA的CurDir函数可以获取当前工作目录的路径。
该函数返回一个字符串,表示当前工作目录的完整路径。
下面是一个示例:```vbaSub GetCurrentDirectory()Dim currentPath As StringcurrentPath = CurDirMsgBox currentPathEnd Sub```2. 获取文件路径和文件名使用VBA的FileOpen函数可以打开一个文件,获取其路径和文件名。
通过获取文件路径和文件名,我们可以进行进一步的处理。
下面是一个示例:```vbaSub GetFilePathAndFileName()Dim filePath As StringDim fileName As StringfilePath = Application.GetOpenFilename()fileName = Mid(filePath, InStrRev(filePath, "\") + 1,Len(filePath) - InStrRev(filePath, "\"))MsgBox "文件路径:" & filePath & vbCrLf & "文件名:" & fileNameEnd Sub```3. 检查目录是否存在在VBA中,可以使用Dir函数来检查指定目录是否存在。
Dir函数返回一个非空字符串,表示目录存在;返回一个空字符串,表示目录不存在。
用VB进行PID数字仿真及整定
”) {七 三 (’ [七一( 1 ( = e) 七 (+ 。 7 e)e 一) D+ ( 七 】
23
:. e+ ( 1×, l 误差比 大于09) 1 率 . 5
维普资讯
《 电技术》20 机 0 8年 第 2期
性 较好 的 曲线 。若 该变 参数 的算 法 使川 一 次 函数 f e = X[ () r + ,则式 ( )转变为 : () a e k / ] b 1
( = 蟊一 ) ( × [ 一e 一1 ( 1+ × {e ( ( ) 】 + ×( + 。 x ) 2 ( ) ( 2】 [ 一 一I+。 一 )l
d ( :一 K ) [ et e T -eKT-T c ) ( ]
其 中函数 f( )是进行控制曲线变化率的重 e
e
阶差分 近似 微
一
分 方程 ) ,当 tK :T时 ,得 山一般 P D控制 算式 : I
1 ★ 乃
( 误差值 与设 定值 的 比值 ) 然 后根据误 差 比率 不 ,
这 就 是增量 式 P D 制算 式 ,其 中参数 K 、 I控 P
K - P T 、K = P D为别为 比例 、积 分 、微 分系 I K / I D K XT 数 ,不 同的参数对 P D系 统 的微分 、积分 、比例 I 调节 作 的强度 和效 果 是完全 不 同 。利 该控 制 算 式进 行计 算机 编程 仿 真 的时候 ,需要考 虑仿 真
法之一 是采川变参数 的控制算 法 , 这样才 能有快 速
1 d e ( =K [ t+ j ( t — 】 , ) Pe ) i oa + ( e
其 中 K 为 比例 系数 ;T P I积 分 时间常数 ;T D 为微 分 时 间常 数 ,e( )为偏 差 。若采 用数 值逼 t
基于VB内核调用MATLAB组件创建模糊PID的程序运用
在 当代 工业 控制 领 域 , 多数 人对 P D 调节 器都 大 I 十分熟 悉 , 而且 P D 调 节器 具 有 较好 的控 制 效 果 和适 I 应性, 至今仍 为控 制过 程所 广泛 采用 。 但是 在实 际运 用 当 中 ,I P D参 数 的 调整 不 仅 需 要熟 练 的技 巧 和实 际 经
第 2 3卷
第 3期
电 脑 开 发 与 应 用
文 章 编 号 :0 3 5 5 2 1 ) 3 0 0 - 3 1 0 - 8 0( 0 0 0 - 0 9 0
基 于 VB 内核 调 用 MAT AB 组 件 创 建 模 糊 P D 的 程序 运 用 L I
App i a i n o e tng Fu z D o r m g a f o VB— a e r b s d Co e
a d mfa ’ p t, , ’’ ’[ 9 , 1 ] ; =a d ( ,n u ’1 ’ , mf,~ 0 一 O ) i NB z
a d v ra ’ p t, c,~ 6 ,O ) P rmee c =a da ( ,n u ’’ ’[ 0 6 ] ; i e aa tre a d mfa ’ p t, , ’’ ’[ 6 , 2 3 ; =a d ( ,n u’2 ’ ,z ,~ o 一 o ) i NB mf
中 图 分 类 号 :TP 9 . 31 9 文 献 标 识 码 :A
ABS TRACT VB r s n to l re d y i h n e f c O d v l p n ,b ta s e ag o p l a in i o n u t ilc n r l Co e i o n y f in l t ei t ra e t e e o me t u lo b o d a p i to s mei d sra o to n c n
Vb.net取程序运行的绝对路径
imports system.diagnostics.fileversioninfo
getExecutingassembly.location
━━━━━━━━━━━━━━━━━━━━━━━━━━
string curPath;
curPath = space(256);
而curdir是当前运行路径但如果期间进行了打开文件或其它改变路径的操作后curdir返回的可能就不是程序运行的路径了所以不推荐使用
取程序运行的绝对路径
取程序运行的绝对路径
━━━━━━━━━━━━━━━━━━━━━━━━━━
取程序运行的绝对路径用 application.startuppath()
GetCurrentDirectory(256, curPath)
if right(curPath,1)<>'\' then curPath +="\"
return curPath;
━━━━━━━━━━━━━━━━━━━━━━━━━━
而CurDir是当前运行路径,但如果期间进行了打开文件或其它改变路径的操作后,CurDir返回的可能就不是程序运行的路径了,所以不推荐使用。
━━━━━━━━━━━━━━━━━━━━━━━━━━
Application.StartupPath 得到可执行文件的路径(bin文件夹下的)。
━━━
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
用VB枚举系统进程PID及路径
代码如下:
‘在声明中添加以下代码
Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
‘上为CloseHandle(),OpenProcess(),EnumProcesses(),GetModuleFileNameExA()EnumProcessModules()函数的声明,在此程序中EnumProcessModules()为非必要,枚举进程路径,并不需要去枚举进程的模块。
Dim cbNeeded As Long, cbNeeded2 As Long, ProcessIDs(1 To 255) As Long
Dim lRet As Long, ModuleName As String, hProcess As Long
Const PROCESS_QUERY_INFORMA TION = &H400
Const PROCESS_VM_READ = &H10
在Private Sub Command1_Click()添加黑色代码
Private Sub Command1_Click()
List1.Clear
Call EnumProcesses(ProcessIDs(1), 256, cbNeeded) '返回进程PID数组和进程数组大小ProcessCount = cbNeeded / 4 '计算出进程数
For i = 1 To ProcessCount
hProcess = OpenProcess(&H400 Or &H10, False, ProcessIDs(i))
If hProcess <> 0 Then
ModuleName = Space(255) '为模块名分配空间,这很重要
'模块句柄指针为零默认获取进程名,无需枚举进程模块
lRet = GetModuleFileNameExA(hProcess, 0, ModuleName, 255)
List1.AddItem ProcessIDs(i) & " " & ModuleName '将进程PID和路径添加到列表框End If
lRet = CloseHandle(hProcess) '关掉进程句柄
Next
End Sub
说明:1.所用控件:Command,List
2.效果如下:。