最近要操作共享文件夹种的SJSHQ.dbf 文件的第一段的时间,遇到了。远程连接到共享文件夹的问题。我想到几种解决办法都是无效,一开始用下载到本地的方法,感觉实时性不好,第二个是 ftp方式,协议不适合,放弃了。第三个是映射到本地来。
使用到 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); }