Ⅰ 怎样在vb中自动检测到可用串口号的具体程序代码
vb.net的话
For
Each
sp
As
String
In
My.Computer.Ports.SerialPortNames
cbxport.Items.Add(sp)
Next
vb6.0的话要调用API查看串口相关信息存在的注册表。
HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
通过读注册表的方法获得串口数量,当然也可以获得串口号了。
Option
Explicit
Private
Declare
Function
RegOpenKey
Lib
"advapi32.dll
"
Alias
"RegOpenKeyA
"
(ByVal
hKey
As
Long,
ByVal
lpSubKey
As
String,
phkResult
As
Long)
As
Long
Private
Declare
Function
RegQueryInfoKey
Lib
"advapi32.dll
"
Alias
"RegQueryInfoKeyA
"
(ByVal
hKey
As
Long,
ByVal
lpClass
As
String,
lpcbClass
As
Long,
ByVal
lpReserved
As
Long,
lpcSubKeys
As
Long,
lpcbMaxSubKeyLen
As
Long,
lpcbMaxClassLen
As
Long,
lpcValues
As
Long,
lpcbMaxValueNameLen
As
Long,
lpcbMaxValueLen
As
Long,
lpcbSecurityDescriptor
As
Long,
lpftLastWriteTime
As
Long)
As
Long
Private
Const
HKEY_LOCAL_MACHINE
=
&H80000002
'
获得当前系统的
COM
口的数量
Function
GetCOMCount()
As
Integer
Dim
ret
As
Long,
cntCOM
As
Long
RegOpenKey
HKEY_LOCAL_MACHINE,
"HARDWARE\DEVICEMAP\SERIALCOMM
",
ret
RegQueryInfoKey
ret,
"
",
0,
0,
0,
0,
0,
cntCOM,
0,
0,
0,
0
GetCOMCount
=
cntCOM
End
Function
Private
Sub
Command1_Click()
MsgBox
"您的机器有
"
&
GetCOMCount
&
"
个串口。
",
vbOKOnly,
"串口数量
"
End
Sub
Ⅱ 用VB6如何侦测U盘的插入并阻止U盘的自动运行
C++写成DLL,VB调用~
/*--------------------------U盘的安全防护----------------------------*/
/*------------------本程序简单实现了U盘病毒免疫功能------------------*/
#include <windows.h>
#include "Shlwapi.h"
#include <Dbt.h>
#pragma comment(lib,"shlwapi.lib") //连入库文件
//function
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//窗口过程
//获取盘符
TCHAR FirstDriveFromMask (ULONG unitmask);
//病毒从U盘启动时用到的函数
BOOL SetFileAttrib(TCHAR *path);//设置path所指文件的属性
//从C盘启动时用到函数
BOOL CopyToUAndSet();//复制自己到U盘
//global variable
TCHAR U[2];//保存U盘的盘符
//constant
const TCHAR *szAutoRunFile="AutoRun.inf";
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[]=TEXT ("UUUUUU");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style =0;
wndclass.lpfnWndProc =WndProc;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hInstance =hInstance;
wndclass.hIcon =0;
wndclass.hCursor =0;
wndclass.hbrBackground =0;
wndclass.lpszMenuName =NULL;
wndclass.lpszClassName =szAppName;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL,"窗口注册失败!", szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName, "U盘的安全防护",
WS_DISABLED,
//WS_OVERLAPPEDWINDOW,
0,0,
0,0,
NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT OnDeviceChange(HWND hwnd,WPARAM wParam, LPARAM lParam)
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
switch(wParam)
{
case DBT_DEVICEARRIVAL: //插入
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
U[0]=FirstDriveFromMask(lpdbv -> dbcv_unitmask);//得到u盘盘符,即逻辑驱动器号
MessageBox(0,U,"Notice!",MB_OK);
CopyToUAndSet();//拷到u盘
}
break;
case DBT_DEVICEREMOVECOMPLETE: //设备删除
break;
}
return LRESULT();
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE: //处理一些下面要用到的全局变量
U[1]=':';
return 0;
case WM_DEVICECHANGE: //检测U盘插入拔出的消息映射
OnDeviceChange(hwnd,wParam,lParam);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
//将响应WM_DEVICECHANGE消息事件的内容(即驱动器掩码)作为输入,和系统定义的掩码相比较,
//返回发生变化事件的驱动器的逻辑名称
TCHAR FirstDriveFromMask(ULONG unitmask)
{
char i;
for (i = 0; i < 26; ++i)
{
if (unitmask & 0x1)//看该驱动器的状态是否发生了变化
break;
unitmask = unitmask >> 1;
}
return (i + 'A');
}
//设置path所指文件的属性
BOOL SetFileAttrib(TCHAR *path)
{
//设置该文件夹属性为隐藏、只读的
return SetFileAttributes(path,FILE_ATTRIBUTE_HIDDEN|
FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM);
}
BOOL CopyToUAndSet()
{
TCHAR szAutoFile[MAX_PATH];
lstrcpy(szAutoFile,U);
lstrcat(szAutoFile,"\\");
lstrcat(szAutoFile,szAutoRunFile);
if(!PathIsDirectory(szAutoFile))
{
CreateDirectory(szAutoFile,NULL);
SetFileAttrib(szAutoFile);
MessageBox(0,"U盘免疫成功!","U盘病毒免疫盒",MB_OK);
}
else
MessageBox(0,"此盘已进行了免疫!","U盘病毒免疫盒",MB_OK);
return FALSE;
}
Ⅲ VB 如何检测一个程序是否在运行中
看是否运行:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Sub Command5_Click()
Dim lHwnd As Long
lHwnd = FindWindow(vbNullString, "程序的Title或Caption")
If lHwnd <> 0 Then
MsgBox "程序正在运行!"
End If
End Sub
向它发送指令:
AppActivate "程序的Title或Caption"
SendKeys "指令"
要启动程序:
Call Shell("完整路径和程序名称.exe")
Ⅳ 怎样在vb中自动检测到可用串口号的具体程序代码
j = 0x0dx0aFor i = 1 To 16 Step 1x0dx0aIf comISP.PortOpen = True Then '先关闭串口x0dx0acomISP.PortOpen = Falsex0dx0aEnd Ifx0dx0ax0dx0acomISP.CommPort = ix0dx0aOn Error Resume Next '说明当一个运行时错误发生时,控件转到紧接着发生错误的语句之后的语句,并在此继续运行。访问对象时要使用这种形式而不使用 On Error GoTo。x0dx0acomISP.PortOpen = Truex0dx0aIf Err.Number <> 8002 Then '无效的串口号。这样可以检测到虚拟串口,如果用Err.Number = 0的话检测不到虚拟串口x0dx0aIf j = 0 Thenx0dx0aj = ix0dx0aEnd Ifx0dx0acboPort.AddItem "COM" & i '生成串口选择列表x0dx0aEnd Ifx0dx0acomISP.PortOpen = Falsex0dx0aNext ix0dx0ax0dx0aIf j >= 1 Thenx0dx0acboPort.Text = "COM" & j '自动打开可用的最小串口号x0dx0acomISP.CommPort = jx0dx0acomISP.PortOpen = Truex0dx0acmdOpenCom.Caption = "关闭串口"x0dx0ashpCOM.FillColor = vbGreenx0dx0aIf Err.Number = 8005 Then '串口已打开,vbExclamation 'x0dx0acomISP.PortOpen = Falsex0dx0acboPort.Text = ""x0dx0acmdOpenCom.Caption = "打开串口"x0dx0ashpCOM.FillColor = vbRedx0dx0aEnd Ifx0dx0aEnd If
Ⅳ VB6.0中如何自动检测有没有连接上网络
这个可以用API函数来检测,比如InternetGetConnetedStateEx这个函数,具体用法请问度娘
一打开软件就检测 这个可以把检测的代码放在窗体的Form_Load()事件里
Ⅵ 怎样在vb中自动检测到可用串口号的具体程序代码
j = 0
For i = 1 To 16 Step 1
If comISP.PortOpen = True Then '先关闭串口
comISP.PortOpen = False
End If
comISP.CommPort = i
On Error Resume Next '说明当一个运行时错误发生时,控件转到紧接着发生错误的语句之后的语句,并在此继续运行。访问对象时要使用这种形式而不使用 On Error GoTo。
comISP.PortOpen = True
If Err.Number <> 8002 Then '无效的串口号。这样可以检测到虚拟串口,如果用Err.Number = 0的话检测不到虚拟串口
If j = 0 Then
j = i
End If
cboPort.AddItem "COM" & i '生成串口选择列表
End If
comISP.PortOpen = False
Next i
If j >= 1 Then
cboPort.Text = "COM" & j '自动打开可用的最小串口号
comISP.CommPort = j
comISP.PortOpen = True
cmdOpenCom.Caption = "关闭串口"
shpCOM.FillColor = vbGreen
If Err.Number = 8005 Then '串口已打开,vbExclamation '
comISP.PortOpen = False
cboPort.Text = ""
cmdOpenCom.Caption = "打开串口"
shpCOM.FillColor = vbRed
End If
End If
Ⅶ 如何用VB6.0编写一段程序,能实现某个程序运行结束后(例如QQ.EXE运行结束),电脑自动关机.谢谢,重酬.
在模块中加入如下代码:
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32MoleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
'上面声明常数 和 API函数
Function exitproc(ByVal exefile As String) As Boolean
exitproc = False '先将该函数过程赋值为假
Dim hSnapShot As Long, uProcess As PROCESSENTRY32
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&) '给系统进程创建快照 调用成功 则返回快照的句柄 调用失败 返回INVAID_HANDLE_VALUE
uProcess.dwSize = Len(uProcess)
r = Process32First(hSnapShot, uProcess) '初始遍历进程
Do While r
If Left$(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0)) = exefile Then 'left截取null前的字符~也就是进程列表~再判断是否有qq.exe
exitproc = True '该函数过程为真 即有此进程
Exit Do '退出循环
End If
r = Process32Next(hSnapShot, uProcess) '遍历下一个进程
Loop
End Function
窗体代码如下 一个timer控件 一个按钮(可要可不要)
Private Sub Command1_Click()
Timer1.Enabled = False '停止监控
End Sub
Private Sub Form_Load()
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
If exitproc("QQ.exe") Then '检测QQ.exe进程是否存在
Else
x = MsgBox("15秒后关闭计算机", vbYesNo, "警告") '弹出对话框
Timer1.Enabled = False '先将timer控件暂停监视
If x = vbYes Then '如果点击对话框“是”按钮 则执行关机
Shell "cmd /c shutdown -s -t 15", 0
Else
Timer1.Enabled = True '如果点 ”否“则继续监控
End If
End If
End Sub
Ⅷ 用VB做一个超声波检测成像的程序
首先,你需要测试单片机通讯的所有操作步骤,保证单片机通讯正常,此步骤通过串口调试工具实现(如串口助手、串口大师等等,网上很多),确认发送的指令及接受的返回数据正常。
然后你就可以开始编程实现手动的功能,包括发送查询指令,返回数据的接收处理(比如搞清楚传回的图像如何转化为位图之类的协议问题)。
功能全部实现后,根据情况选择增加一部分意外处理,比如通讯中断、返回数据无效等,这样就算完成了。