如何从远程计算机中获取当前登录的用户名
How to get the currently logged in username from a remote computer
是否有一种方法可以从远程计算机中获取当前登录的用户名?(我正在寻找沿getUsername(((的东西
我有一种有点有效的解决方案,但是(诚实地说实话(感觉就像是用滑板射击的巡航导弹杀死苍蝇(复杂,绝对是长时间的,可能是过高的杀伤力(。
我的解决方案(到目前为止(:
QString NetworkHandle::getUserName(QString entry){
QString s1, s2, command;
std::string temp, line;
const char *cmd;
char buf[BUFSIZ];
FILE *ptr, *file;
int c;
s1 = "wmic.exe /node:";
s2 = " computersystem get username 2> nul";
command = s1 + entry + s2;
temp = command.toLocal8Bit().constData();
cmd = temp.c_str();
file = fopen("buffer.txt", "w");
if(!file){
this->setErrLvl(3);
return "ERROR";
}
if((ptr = popen(cmd, "r")) != NULL){
while (fgets(buf, BUFSIZ, ptr) != NULL){
fprintf(file, "%s", buf);
}
pclose(ptr);
}
fclose(file);
std::ifstream input("buffer.txt");
c = 0;
if(!input){
this->setErrLvl(4);
return "ERROR";
}
while(!input.eof()){
std::getline(input, line);
if(c == 1 && line.size() > 1){
input.close();
entry = QString::fromUtf8(line.data(), line.size());
return entry;
}
c++;
}
input.close();
std::remove("buffer.txt");
return "Could not return Username.";
}
就像我说的那样:在极端非例行的一面只是有点。
Methode与IP地址获得了QString,将其与wmic.exe /node:
和computersystem get username 2> nul
结合在一起,并将WMIC.EXE的输出写入文本文件中,读取所需的行(第二个(,将字符串缩短到必要的信息中并返回所述信息(用户名(
现在我的问题是以下内容:如果我只想在运行时获得一个左右的用户名,这一切都很好,花花公子……我没有。我需要填充整个表(最多包含200个或更多条目,具体取决于网络活动(,这需要10到15分钟。
现在,我的程序处理通过插座获取IP和Computername Collection,但我是此类编程的新手(TBH:我刚刚开始C 来自C,我从未做过任何与网络相关的编程内容(,所以我是在此问题上不是很深。
是否有一种方法可以通过套接字在远程计算机上获取当前登录的用户名?
您可以使用 QProcess
来处理wmic.exe工具:
void NetworkHandle::getUserName(QString entry)
{
QProcess *wmic_process = new QProcess();
wmic_process->setProgram("wmic.exe");
wmic_process->setArguments(QStringList() << QString("/node:%1").arg(entry) << "computersystem" << "get" << "username");
wmic_process->setProperty("ip_address", entry);
connect(wmic_process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(parseUserName(int,QProcess::ExitStatus)) );
wmic_process->start();
}
void NetworkHandle::parseUserName(int exitCode, QProcess::ExitStatus exitStatus)
{
QProcess *process = dynamic_cast<QProcess*>(sender());
if (exitStatus == QProcess::NormalExit)
{
qDebug() << "information for node" << process->property("ip_address").toString();
qDebug() << process->readAllStandardOutput();
}
process->deleteLater();
}
相关文章:
- 如何在 c++ 中将密码和用户名保存到 .txt 文件中.如果用户尝试登录,我仍然希望能够检索它们
- Win7 C++ - 从以用户身份登录的服务启动可执行文件的问题
- 用户注销/登录后,创建托盘图标时出现罕见错误
- 是否有任何 C 函数或 API 来获取当前登录用户下运行的进程列表
- 如何从 Windows 上的服务在未登录用户的桌面上启动应用程序
- 获取 Windows 系统已加入 Azure AD 时登录用户的 UserPrincipalName
- 加密用户数据,而不会丢失安卓应用程序中的登录用户
- 如何检查用户登录到Windows的时间
- 嗨,我正在尝试编码一个允许用户在Visual C 中登录的按钮,程序正在运行,但表单对话框未显示
- 如何从非管理员用户获取当前登录的用户名、域名和用户 sid
- 我如何将登录的用户登录到当前会话中的域用户
- 从 mfc 应用程序点击网络服务,并希望从网络服务登录方法读取用户代码返回
- 如何从另一个登录用户启动登录用户内部的应用程序
- 在多个登录用户系统中检索当前登录的用户 SID
- 如何获取当前登录用户的UPN(用户主体名称)
- 登录用户:程序意外完成
- 是否可以创建一个服务或应用程序,以编程方式在windows中登录用户
- 如何在Windows中获得当前登录用户的域
- 如何在 Windows 中获取登录用户的 SID
- 如果使用system用户令牌启动登录用户进程,是否会危及系统安全