zoukankan      html  css  js  c++  java
  • 实现:ipc命名管道连接

    #pragma comment(lib, "mpr.lib")
    
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <Winnetwk.h>
    #include <string>
    
    //using namespace std;
    
    int wmain(int argc, wchar_t * argv[]) {
    	///*
    	//DWORD WNetAddConnection2W(
    	//	LPNETRESOURCEW lpNetResource,
    	//	LPCWSTR        lpPassword,
    	//	LPCWSTR        lpUserName,
    	//	DWORD          dwFlags
    	//);
    	//*/
    
    
    	DWORD dwRetVal;
    	std::wstring MyRemoteName;
    	NETRESOURCE nr;
    	DWORD dwFlags;
    
    	MyRemoteName.append(L"\\");
    
    	if (argc != 5 && argc != 4) {
    		wprintf(L"Usage: %s <localname> <remotename> <username> <password>
    ",argv[0]);
    		wprintf(L"Usage: %s <remotename> <username> <password>
    ",argv[0]);
    		exit(1);
    	}
    
    	if (argc == 5) {
    		MyRemoteName.append(argv[2]);
    		wprintf(L"Calling WNetAddConnection2 with
    ");
    		wprintf(L"  lpLocalName = %s
    ", argv[1]);
    		wprintf(L"  lpRemoteName = %s
    ", MyRemoteName.c_str());
    		wprintf(L"  lpUsername = %s
    ", argv[3]);
    		wprintf(L"  lpPassword = %s
    ", argv[4]);
    		
    		// Zero out the NETRESOURCE struct
    		memset(&nr, 0, sizeof(NETRESOURCE));
    
    		// Assign our values to the NETRESOURCE structure.
    
    		nr.dwType = RESOURCETYPE_ANY;
    		nr.lpLocalName = argv[1];
    		nr.lpRemoteName = (LPWSTR)MyRemoteName.c_str();
    		nr.lpProvider = NULL;
    
    		// Assign a value to the connection options
    		dwFlags = CONNECT_TEMPORARY;  //连接类型 是否可持续
    
    		// Call the WNetAddConnection2 function to assign
    		//   a drive letter to the share.
    		//
    		dwRetVal = WNetAddConnection2(&nr, argv[4], argv[3], dwFlags);
    		//
    		// If the call succeeds, inform the user; otherwise,
    		//  print the error.
    		//
    		if (dwRetVal == NO_ERROR)
    			wprintf(L"Connection added to %s
    ", nr.lpRemoteName);
    		else {
    			wchar_t * pMsgBuf;
    			FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS
    				, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&pMsgBuf, 0, NULL);
    			wprintf(L"WNetAddConnection2 failed with error: %u, %ls 
    ", dwRetVal, pMsgBuf);
    			LocalFree(pMsgBuf);
    		}
    	}
    	else if (argc == 4) {
    		MyRemoteName.append(argv[1]);
    		wprintf(L"Calling WNetAddConnection2 with
    ");
    		wprintf(L"  lpRemoteName = %s
    ", (LPWSTR)MyRemoteName.c_str());
    		wprintf(L"  lpUsername = %s
    ", argv[2]);
    		wprintf(L"  lpPassword = %s
    ", argv[3]);
    
    		// Zero out the NETRESOURCE struct
    		memset(&nr, 0, sizeof(NETRESOURCE));
    
    		// Assign our values to the NETRESOURCE structure.
    
    		nr.dwType = RESOURCETYPE_ANY;
    		nr.lpLocalName = NULL;
    		nr.lpRemoteName = (LPWSTR)MyRemoteName.c_str();
    		nr.lpProvider = NULL;
    
    		// Assign a value to the connection options
    		dwFlags = CONNECT_TEMPORARY; //连接类型 是否可持续
    
    		// Call the WNetAddConnection2 function to assign
    		//   a drive letter to the share.
    		//
    		dwRetVal = WNetAddConnection2(&nr,argv[3], argv[2], dwFlags);
    		//
    		// If the call succeeds, inform the user; otherwise,
    		//  print the error.
    		//
    		if (dwRetVal == NO_ERROR)
    			wprintf(L"Connection added to %s
    ", nr.lpRemoteName);
    		else {
    			wchar_t * pMsgBuf;
    			FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS
    				, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&pMsgBuf, 0, NULL);
    			wprintf(L"WNetAddConnection2 failed with error: %u, %s 
    ", dwRetVal, pMsgBuf);
    			LocalFree(pMsgBuf);
    		}
    	}
    
    	return 0;
    }
    
    
  • 相关阅读:
    标签和过滤器
    【android】简单的布局和控件&简单的练习作品
    【android】配置模拟器以及第一个“Hello World!”
    【android】sdk安装及环境变量配置、android studio的安装及新建项目
    【javaweb】库存物资管理系统思路与总结
    【java】关于异常处理的思考
    【作业】对于对象的课程作业
    【作业】神奇的代码,包装类Integre,100==100,129!=129
    【作业】随机数+参数可变的方法+实验任务(输出素数+使用递归,判断是否为回文+统计一篇英语问斩单词出现频率)
    【作业】三个关于java的探索和两个实验题
  • 原文地址:https://www.cnblogs.com/zpchcbd/p/12190083.html
Copyright © 2011-2022 走看看