2、使用DOS findstr 命令辅助筛选符合要求的进程PID
3、使用DOS tasklist 命令查询PID对应的进程信息
4、使用DOS findstr 命令辅助筛选符合要求的进程名
5、在VC中执行DOS命令
WinExec 异步执行。不能等待命令结束,较简单
ShellExecute 麻烦
CreateProcess 麻烦
注:使用任何一种方法,都需要将结果输出到外部,然后再读取结果分析
二、DOS查询端口使用示例 比如要查看8080端口被哪个程序占用了,windows命令行窗口下执行:运行--cmd
C:\>netstat -aon|findstr ":8080 " ,输出
TCP 127.0.0.1:80 0.0.0.0:0 LISTENING 2448
端口被进程号为2448的进程占用,继续执行下面命令:
C:\>tasklist /fi "pid eq 2448" /nh
thread.exe 2016 Console 0 16,064 K
表示thread.exe程序占用了端口8080
三、windows下VC实现代码
#include <windows.h>
#include <string>
using namespace std;
//
//根据端口查询进程名,如果有多个进程,只返回第一个
//
bool GetProcNameByPort(int nPort, string &strResult)
{
bool bSuc = false;
char pszPort[16] = {0};
itoa(nPort, pszPort, 10);
char pResult[80] = {0};
const char* pPortFilePath = "c:\\~vtmp";
const char* pProcessFilePath = "c:\\~vvtmp";
sprintf(pResult, "cmd /c netstat -ano|findstr \":%d \" > %s", nPort, pPortFilePath);
//WinExec 执行cmd命令
WinExec(pResult, SW_HIDE);
Sleep(450);
//查找端口号
FILE *pPortFile = fopen(pPortFilePath, "r");
if ( pPortFile )
{
while ( !feof(pPortFile) )
{
memset(pResult, 0, sizeof(pResult));
fread(pResult, sizeof(pResult), 1, pPortFile);
pResult[sizeof(pResult)-1] = 0x00;
string strPortTmp = pResult;
int offset = (int)strPortTmp.find_last_of(0x0A);
if ( offset > -1 )
{
pResult[offset] = 0x00;
strPortTmp = strPortTmp.substr(0, offset);
if ( !feof(pPortFile) )
{
fseek(pPortFile, (long)(strPortTmp.length()+1-sizeof(pResult)), SEEK_CUR);
}
offset = (int)strPortTmp.find_first_of(':');
if ( offset > -1 )
{
strPortTmp = strPortTmp.substr(offset+1, 6);
offset = (int)strPortTmp.find_last_not_of(' ');
if ( offset > -1 )
{
strPortTmp = strPortTmp.substr(0, offset+1);
if ( strPortTmp == pszPort )
{
strPortTmp = pResult;
offset = (int)strPortTmp.find_last_of(' ');
if ( offset > -1 )
{
strPortTmp = strPortTmp.substr(offset+1);
sprintf(pResult, "cmd /c tasklist /fi \"pid eq %s\" /nh> %s", strPortTmp.c_str(), pProcessFilePath);
//根据端口号查找进程ID
WinExec(pResult, SW_HIDE);
Sleep(450);
FILE *pProcessFile = fopen(pProcessFilePath, "r");
if ( pProcessFile )
{
while (!feof(pProcessFile))
{
memset(pResult, 0, sizeof(pResult));
fread(pResult, sizeof(pResult), 1, pProcessFile);
pResult[sizeof(pResult)-1] = 0x00;
string strProcessTmp = pResult;
int offset = (int)strProcessTmp.find_last_of(0x0A);
if ( offset > -1 )
{
pResult[offset] = 0x00;
strProcessTmp = strProcessTmp.substr(0, offset);
if ( !feof(pProcessFile) )
{
fseek(pProcessFile, (long)(strProcessTmp.length()+1-sizeof(pResult)), SEEK_CUR);
}
if ( 0x0A == pResult[0] ) //首行只有一个字符 0x0A
{
strProcessTmp = pResult+1;
}
else
{
strProcessTmp = pResult;
}
offset = (int)strProcessTmp.find_first_of(' ');
if ( offset > -1 )
{
{
{
{
{
strProcessTmp = strProcessTmp.substr(0, offset);
if ( "" != strProcessTmp )
{
//查找成功,结束
strResult += "[" + strProcessTmp + "]";
bSuc = true;
}
continue;
}
}
}
}
}
}
}
fclose(pProcessFile);
}
sprintf(pResult, "cmd /c del %s", pProcessFilePath);
WinExec(pResult, SW_HIDE);
if(bSuc){ continue; }
}
}
}
}
}
}
fclose(pPortFile);
}
if(!bSuc){ strResult=""; };
sprintf(pResult, "cmd /c del %s", pPortFilePath);
WinExec(pResult, SW_HIDE);
return bSuc;
}
int main()
{
int count = 100;
string str = "";
while(count--)
{
str = "";
GetProcNameByPort(843, str);
if ( str != "" )
printf("_%s_\n", str.c_str());
Sleep(1000);
}
printf("____End____");
getchar();
return 0;
}
转载请注明来自Master.R(石硕)的CSDN博客:blog.csdn.net/shishuo365 如有疑问请发邮件shishuo365#126.com(将#更换为@)
近几年,PC行业的发展势头明显不再像前些年那么猛烈,在智能手机和平板电脑的冲击下,PC行业走势变得更加理性和平稳。实际上,传统PC产品并未遭遇某些媒体所宣称的“没落”,只不过是被火爆一时的平板等PC+产品抢了风头罢了。
sqlite3 数据库简介
SQLite 数据库,是一个非常轻量级自包含(lightweight and self-contained)的DBMS,它可移植性好,很容易使用,很小,高效而且可靠。
SQLite嵌入到使用它的应用程序中,它们共用相同的进程空间,而不是单独的一个进程。从外部看,它并不像一个RDBMS,但在进程内部,它却是完整的,自包含的数据库引擎。
嵌入式数据库的一大好处就是在你的程序内部不需要网络配置,也不需要管理。因为客户端和服务器在同一进程空间运行。
SQLite 的数据库权限只依赖于文件系统,没有用户帐户的概念。SQLite 有数据库级锁定,没有网络服务器。它需要占用内存,但其它开销很小,适合用于嵌入式设备,你需要做的仅仅是把它正确的编译到你的程序。sqlite常用查询语句
SQLite创建数据库
SQLite使用起来非常方便,仅仅需要敲入带有SQLite数据库名字的"sqlite3"命令即可。如果文件不存在,则创建一个新的(数据库)文件。然后sqlite3程序将提示你输入SQL。敲入SQL语句以分号“;”结束,敲回车键之后,SQL语句就会执行。例如,创建一个包含一个数据库为“user.db”表“cars”的SQLite数据库。创建命令:
创建数据库user.db
sqlite3 user.db创建表cars
create table carsl(name varchar(10), years smallint, price float);
查询表
.table
插入数据
insert into cars values('audi', 1994, 530000.5);insert into cars values('volvo', 2001, 303478.5);
显示头
.header on
修改显示模式
.mode column
查询数据
select * from cars; //查询cars表 中所有数据
sqlite> select * from tbl1 where one='xxx';
删除全部语句
sqlite> delete from cars;
更新语句
sqlite>update cars set name='kaka' where name="mini";
删除语句
sqlite> delete from cars where name="kaka";
sqlite>delete from cars; //删除 cars表 中所有数据
条件语句的应用 "and" "or" "and or"
sqlite> delete from cars where name="volvo" or name="audi";
sqlite> delete from cars where name="volvo" and price=324200;
sqlite> delete from cars where (name="volvo" or name="audi") and price=324203;
显示结果:
SQLite可以使用8种方式显示查询结果,大大方便了程序对数据的处理,sqlite3程序可以以八种不同的格式显示一个查询的结果:
.mode MODE ?TABLE? Set output mode where MODE is one of:
csv Comma-separated values
column Left-aligned columns. (See .width)
html HTML <table> code
insert SQL insert statements for TABLE
line One value per line
list Values delimited by .separator string
tabs Tab-separated values
tcl TCL list elements
使用方法: .mode column (输出格式切换到行模式,如上图)
SQLite3 导入导出数据库
导出数据库
sqlite> .databases (显示数据库)
sqlite> .backup main user.sql (备份数据库main, 即完成数据库的复制)sqlite> .backup user2.sql (备份默认数据库main)
同时sqlite具有导入/出表,文件,数据库等功能(具体详见 ./help)
参考来源: http://www.cnblogs.com/wdpp/archive/2011/11/30/2386714.html
推荐参考:sqlite官网
sqlite数据类型
sql语句
http://blog.csdn.net/xing_hao/article/details/6660589