首先在VC项目属性,开发依赖项里添加MPR.lib;然后,配置文件里填入以下信息。  

   //本地映射盘符
  MapDriver=T:

  //目标根目录
  //MapSharedPath=\\192.168.0.2\D$
  MapSharedPath=\\192.168.0.2\QuarkCall

  //目标机器用户名
  MapUser=administrator

  //目标机器密码
  MapPassword=administrator

这四个字段信息分别写入全局变量:LocalName,RemoteName,UserName,Password中。

以下是代码:

 int Redirect(const char *LocalName, const char *RemoteName, const char *UserName, const char *Password)
{
  int nResult = ;   NETRESOURCE nr;
  DWORD res;   char szUserName[],
  szPassword[],
  szLocalName[],
  szRemoteName[MAX_PATH];   strcpy(szUserName,UserName);
  strcpy(szPassword,Password);
  strcpy(szLocalName,LocalName);
  strcpy(szRemoteName,RemoteName);   nr.dwType = RESOURCETYPE_ANY;
  nr.lpLocalName = szLocalName;
  nr.lpRemoteName = szRemoteName;
  nr.lpProvider = NULL;   //if(NO_ERROR!=WNetCancelConnection2(szLocalName,0,false))
    // printf("断开失败");   res = WNetAddConnection2(&nr, szPassword, szUserName, CONNECT_UPDATE_PROFILE);   switch(res)
  {
    case NO_ERROR:
      g_DebugMsg.Sprintf("网络驱动器映射成功%s\n",RemoteName);
      nResult = ;
      break;
    case ERROR_BAD_PROFILE:
      g_DebugMsg.Sprintf("ERROR_BAD_PROFILE\n");
      break;
    case ERROR_CANNOT_OPEN_PROFILE:
      g_DebugMsg.Sprintf("ERROR_CANNOT_OPEN_PROFILE\n");
      break;
    case ERROR_DEVICE_IN_USE:
      g_DebugMsg.Sprintf("ERROR_DEVICE_IN_USE\n");
      break;
    case ERROR_EXTENDED_ERROR:
      g_DebugMsg.Sprintf("ERROR_EXTENDED_ERROR\n");
      break;
    case ERROR_NOT_CONNECTED:
      g_DebugMsg.Sprintf("ERROR_NOT_CONNECTED\n");
      break;
    case ERROR_OPEN_FILES:
      g_DebugMsg.Sprintf("ERROR_OPEN_FILES\n");
      break;
    default:
      g_DebugMsg.Sprintf("未知错误,可能需要帐号和密码认证,或者该主机或文件不存在\n");
      break;
  }
  return nResult;
}
05-23 15:14