加载中…
个人资料
悦妃云
悦妃云
  • 博客等级:
  • 博客积分:0
  • 博客访问:142,750
  • 关注人气:27
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

VB实现双进程保护程序实例

(2013-06-14 09:03:14)
标签:

双进程

保护程序

vb

it

分类: 开发源码
'窗体代码

Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Type PROCESSENTRY32
    dwsize As Long
    cntUsage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwFlags As Long
    szExeFile As String * 260
End Type
Private Const PROCESS_ALL_ACCESS As Long = &H1F0FFF
Dim ProcessID As Long
Function PanDuanPIDTure(ByVal ProcessID As Long) As Boolean
 On Error Resume Next
  Dim Proc As PROCESSENTRY32
   Dim snap As Long
    Dim theloop As Long
     snap = CreateToolhelpSnapshot(15, 0)
      Proc.dwsize = Len(Proc)
       theloop = ProcessFirst(snap, Proc)
        Do While theloop <> 0
          If Proc.th32ProcessID = ProcessID Then
               PanDuanPIDTure = True
               Exit Do
             Else
               PanDuanPIDTure = False
          End If
           theloop = ProcessNext(snap, Proc)
        Loop
       CloseHandle snap
End Function
Public Function TerminateProcess1(ByVal ProcessID As Long) As Boolean
 Dim hProcess As Long
  hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessID)
   If TerminateProcess(hProcess, 0) = 0 Then
     TerminateProcess1 = False
    Else
     TerminateProcess1 = True
   End If
  CloseHandle hProcess
End Function
Private Sub Command1_Click()
 Timer1.Enabled = False
 TerminateProcess1 ProcessID
 End
End Sub
Private Sub Form_Load()
If Command <> "" Then
  ProcessID = Command
 Else
  ProcessID = -1
End If
End Sub
Private Sub Timer1_Timer()
 If Not PanDuanPIDTure(ProcessID) Then
   Me.Hide
   ProcessID = Shell(App.Path & "" & App.EXEName & ".exe " & GetCurrentProcessId, 1)
 End If
End Sub



一个时钟控件,一个命令按钮。

0

阅读 评论 收藏 禁止转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有