我们从前的项目写过一个这样的函数。你可以在访问共享目录之前,用程序登录一次。就是把WNetAddConnection2封装了一下。
DWORD NetConnect(char* RemotePath,LPCTSTR lpPassword,LPCTSTR lpUsername,DWORD dwFlags)
{
DWORD dwResult;
NETRESOURCE nr;
memset (&nr, 0, sizeof (NETRESOURCE));nr.dwType = RESOURCETYPE_DISK;
nr.lpLocalName = "";
nr.lpRemoteName = RemotePath;
nr.lpProvider = NULL;dwResult = WNetAddConnection2(&nr,(LPCSTR) lpPassword,(LPCSTR) lpUsername,0);// The local device is already connected to a network resource.
/*if (dwResult == ERROR_ALREADY_ASSIGNED)
{
return TRUE;
}
else if(dwResult == ERROR_NO_NET_OR_BAD_PATH)
{
return FALSE;
}
// An entry for the local device already exists in the user profile.
else if (dwResult == ERROR_DEVICE_ALREADY_REMEMBERED)
{
return FALSE;
}
else if(dwResult != NO_ERROR)
{
// Call an application-defined error handler.
return FALSE;
}*/DWORD dwEroResut,lpError;
LPTSTR lpErrorBuf = ""; // error description buffer
LPTSTR lpNameBuf = ""; // buffer for provider nameif(dwResult == ERROR_EXTENDED_ERROR)
{
dwEroResut =WNetGetLastError(&lpError,lpErrorBuf,256,lpNameBuf,128);
if(dwEroResut == NO_ERROR)
return lpError;
else
return dwEroResut;
}return dwResult;
}Mailto:[email protected]
DWORD NetConnect(char* RemotePath,LPCTSTR lpPassword,LPCTSTR lpUsername,DWORD dwFlags)
{
DWORD dwResult;
NETRESOURCE nr;
memset (&nr, 0, sizeof (NETRESOURCE));nr.dwType = RESOURCETYPE_DISK;
nr.lpLocalName = "";
nr.lpRemoteName = RemotePath;
nr.lpProvider = NULL;dwResult = WNetAddConnection2(&nr,(LPCSTR) lpPassword,(LPCSTR) lpUsername,0);// The local device is already connected to a network resource.
/*if (dwResult == ERROR_ALREADY_ASSIGNED)
{
return TRUE;
}
else if(dwResult == ERROR_NO_NET_OR_BAD_PATH)
{
return FALSE;
}
// An entry for the local device already exists in the user profile.
else if (dwResult == ERROR_DEVICE_ALREADY_REMEMBERED)
{
return FALSE;
}
else if(dwResult != NO_ERROR)
{
// Call an application-defined error handler.
return FALSE;
}*/DWORD dwEroResut,lpError;
LPTSTR lpErrorBuf = ""; // error description buffer
LPTSTR lpNameBuf = ""; // buffer for provider nameif(dwResult == ERROR_EXTENDED_ERROR)
{
dwEroResut =WNetGetLastError(&lpError,lpErrorBuf,256,lpNameBuf,128);
if(dwEroResut == NO_ERROR)
return lpError;
else
return dwEroResut;
}return dwResult;
}Mailto:[email protected]
VB里面也是可以用的,你把它重新用vb封装一下吧。Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As LongMailto:[email protected]