C++ 映射共享文件到本地
2015-08-31 18:48
302 查看
最近要操作共享文件夹种的SJSHQ.dbf 文件的第一段的时间,遇到了。远程连接到共享文件夹的问题。我想到几种解决办法都是无效,一开始用下载到本地的方法,感觉实时性不好,第二个是 ftp方式,协议不适合,放弃了。第三个是映射到本地来。
使用到 WNetAddConnection2()这个函数 。
下面贴出我的例子程序。这是micsoft自带的。我自己的,就是把变量换了一下。就可以了,注意转移字符\,要用\\注释掉。
1907错误码是用户名密码错误
58是重复连接
使用到 WNetAddConnection2()这个函数 。
下面贴出我的例子程序。这是micsoft自带的。我自己的,就是把变量换了一下。就可以了,注意转移字符\,要用\\注释掉。
1907错误码是用户名密码错误
58是重复连接
#ifndef UNICODE #define UNICODE #endif #pragma comment(lib, "mpr.lib") #include <windows.h> #include <tchar.h> #include <stdio.h> #include <Winnetwk.h> // Need to link with Netapi32.lib and Mpr.lib int wmain(int argc, wchar_t * argv[]) { DWORD dwRetVal; NETRESOURCE nr; DWORD dwFlags; if (argc != 5) { wprintf(L"Usage: %s <localname> <remotename> <username> <password>\n", argv[0]); wprintf(L" %s X: \\\\contoso\\public testuser testpasswd\n", argv[0]); exit(1); } wprintf(L"Calling WNetAddConnection2 with\n"); wprintf(L" lpLocalName = %s\n", argv[1]); wprintf(L" lpRemoteName = %s\n", argv[2]); wprintf(L" lpUsername = %s\n", argv[3]); wprintf(L" lpPassword = %s\n", 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 = argv[2]; nr.lpProvider = NULL; // Assign a value to the connection options dwFlags = CONNECT_UPDATE_PROFILE; // // 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\n", nr.lpRemoteName); else wprintf(L"WNetAddConnection2 failed with error: %u\n", dwRetVal); exit(1); }
相关文章推荐
- C++中map容器的使用说明
- c++ boost string split trim replace
- Windows API: 调用FindFirstFile之后发生的内存泄漏(未解决)
- Item 19:把类的设计视作类型设计 Effective C++笔记
- Visual Studio 2015 memory leak detection
- c语言libcurl 使用实例get/post方法+c语言字符串处理
- 使用 C++ 处理 JSON 数据交换格式
- C++的Json解析库:jsoncpp和boost .
- C++ Template之技巧性基础知识 和 typeid(x).name()用法
- C++ List的用法(整理)
- Java 调用C语言JNI库(Hello World实例)
- C++计算输入数据对应二进制中1的个数。
- LINK : fatal error LNK1158: cannot run 'rc.exe'
- vs工程链接出现error LNK2005...already defined
- vs2012 error LNK2038 RuntimeLibrary 不匹配的解决
- Effective C++ Item2
- C++ Builder XE8 安卓开发之获取WIFI状态
- LINK : fatal error LNK1104: 无法打开文件“kernel32.lib”
- map容器的简单概述及入门的定义和使用方法
- 重载赋值运算符返回reference to *this(Effective C++_10)