为什么映射共享目录后打不开文件呢?手工登陆的就可以.GetLastError报的错误是无法打开文件,第一个解决的把分数全给它
#include "stdafx.h"
#include <Winnetwk.h>
#include <iostream>
#include <atlstr.h>
#include <fstream>
using namespace std;
#pragma comment(lib, "Mpr.lib")
CString ErrorIdToMsg(DWORD ErrorId)
{
LPVOID lpMsgBuf;
if (!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL ))
{
// Handle the error.
return _T("can not convert!");
}
CString tmp((LPCTSTR)lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
return tmp;
}
int _tmain(int argc, _TCHAR* argv[])
{
NETRESOURCE resource;
::ZeroMemory(&resource, sizeof(NETRESOURCE));
resource.dwType = RESOURCETYPE_DISK;
resource.lpLocalName = _T("x:");
resource.lpRemoteName = _T("\\\\192.168.0.28\\共享");
resource.dwScope = RESOURCE_CONNECTED;
resource.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
resource.dwUsage = RESOURCEUSAGE_CONNECTABLE;
resource.lpComment = NULL;
resource.lpProvider = NULL;
DWORD retval;
retval=WNetAddConnection2(&resource, _T("Administrator"), _T("19826017"), CONNECT_UPDATE_PROFILE);
if (NO_ERROR == retval)
cout << "Connect Sucessed!" << endl;
else
cout << ErrorIdToMsg(retval) << endl;
ifstream infile("x:\\test.txt");
if (!infile)
{
cout << ErrorIdToMsg(GetLastError());
}
else
{
//read the file content
cout << "Open Sucessed!" << endl;
char str[100];
while (infile.getline(str, 100))
cout << str << endl;
infile.close();
}
retval=WNetCancelConnection2(resource.lpLocalName, CONNECT_UPDATE_PROFILE, FALSE);
if (NO_ERROR == retval)
cout << "Disconnect Sucessed!" << endl;
else
cout << ErrorIdToMsg(retval) << endl;
return 0;
}
大家看看,这代码有什么问题吗?