WNetAddConnection2 fails with error code 1312
-
Hi! I have a win2000 service written in visual c++ 6.0 that has to access files from a shared folder from the local network neighborhood. The shared folders are password protected, so I have to create a connection to that folder. I tried to use WNetAddConnection2. But WNetAddConnection2 fails and returns the error code no. 1312. I looked it up in the winapi headers, error 1312 is called ERROR_NO_SUCH_LOGON_SESSION, the explanation given is : 'A specified logon session does not exist. It may already have been terminated.'. The list of files/locations is taken from a database, so is dynamically changing over time. Has anybody any idea how could I access files shared on other computers without logging in to the system? Thanks, Alma
-
Hi! I have a win2000 service written in visual c++ 6.0 that has to access files from a shared folder from the local network neighborhood. The shared folders are password protected, so I have to create a connection to that folder. I tried to use WNetAddConnection2. But WNetAddConnection2 fails and returns the error code no. 1312. I looked it up in the winapi headers, error 1312 is called ERROR_NO_SUCH_LOGON_SESSION, the explanation given is : 'A specified logon session does not exist. It may already have been terminated.'. The list of files/locations is taken from a database, so is dynamically changing over time. Has anybody any idea how could I access files shared on other computers without logging in to the system? Thanks, Alma
The solution i found is to use the CreateService function with user and password parameters set. Ex: -------------------------------------------------------------------------------- CString user = _T(".\\User"); CString pswd = _T("pswd"); LPSTR u = user.GetBuffer(user.GetLength()+1); LPSTR p = pswd.GetBuffer(pswd.GetLength()+1); SC_HANDLE hService = ::CreateService( hSCM, m_szServiceName, m_szServiceName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, szFilePath, NULL, NULL, _T("RPCSS\0"), u, p); -------------------------------------------------------------------------------- To create the connection: -------------------------------------------------------------------------------- NETRESOURCE nr; char* a = new char[MAX_PATH]; memset(a, 0, MAX_PATH); char* b = new char[MAX_PATH]; memset(b, 0, MAX_PATH); strcpy(a, (char*)"\\\\alma\\geza"); strcpy(b,(char*)"x:"); char pswd[20] = "pswd"; char account[20] = "user"; nr.lpRemoteName = a; nr.lpLocalName = b; nr.lpProvider = NULL; nr.dwType = RESOURCETYPE_DISK; DWORD flags = CONNECT_UPDATE_PROFILE; DWORD res; res = WNetAddConnection2(&nr, (char*)pswd, (char*)account, flags); delete []a; delete []b; if (res == NO_ERROR) return TRUE; else return FALSE;