论安全萌新的自我修养

   :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "mpr.lib")

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <Winnetwk.h>

//wmain是main函数的宽字符版本  在Unicode编码下使用
int wmain(int argc, wchar_t* argv[]) {//argc是输入参数的个数,argv存储了所有命令行的参数
	DWORD dwRetVal;
	NETRESOURCE nr;
	DWORD dwFlags;

	if (argc != 4) {
		wprintf(L"Usage: +  remotename username password          \n");
		wprintf(L"Eg:    +  \\\\192.168.3.144 domain\\user password \n");
		exit(1);    //程序的退出,同break
	}
	memset(&nr, 0, sizeof(NETRESOURCE));//将NetResource设为0
	//NetResource的值的分配设定的都是lpNetResource的参数
	nr.dwType = RESOURCETYPE_ANY;//lpLocalName为NULL
	nr.lpComment = NULL;//lpLocalName为NULL,则该函数将建立与网络资源的连接,而无需重定向本地设备
	nr.lpRemoteName = argv[1];//目标机器
	nr.lpProvider = NULL;//操作系统尝试通过解析lpRemoteName成员指向的字符串来确定正确的提供程序
	//设定连接选项
	dwFlags = CONNECT_UPDATE_PROFILE;//记住网络连接
	//调用连接函数
	dwRetVal = WNetAddConnection2(&nr, argv[3], argv[2], dwFlags);

	if (dwRetVal == NO_ERROR) {
		wprintf(L"success to &s\n", nr.lpRemoteName);
	}
	else
	{
		wprintf(L"something is wrong:%u\n", dwRetVal);
	}
	exit(1);
}


参考
https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/nf-winnetwk-wnetaddconnection2a
posted on 2021-02-09 21:41  Yangsir34  阅读(677)  评论(0编辑  收藏  举报