映射网络共享资源为本地硬盘

#include "stdafx.h"

#include <windows.h>
#include <stdio.h>
#include <winnetwk.h>

#pragma comment(lib, "mpr.lib")

//添加映射
DWORD AddConnection(LPCTSTR szRemoteName, LPCTSTR szLocalname)
{
  //
  // Assign values to the NETRESOURCE structure.
  //
  NETRESOURCE nr = {0};
  nr.dwType = RESOURCETYPE_ANY;
  nr.lpLocalName = (LPTSTR)szLocalname;
  nr.lpRemoteName = (LPTSTR)szRemoteName;//
  //
  // Call the WNetAddConnection2 function to assign
  //   a drive letter to the share.
  //
  DWORD dwResult = WNetAddConnection2(&nr, NULL, NULL, 0);
  //
  // If the call succeeds, inform the user; otherwise,
  //  print the error.
  //
  if(dwResult != NO_ERROR)
  {
    #define caseErr(x) case(x): { printf(#x "\n"); break;  }

    switch(dwResult)
    {
      caseErr(ERROR_ACCESS_DENIED); //The caller does not have access to the network resource.
      caseErr(ERROR_ALREADY_ASSIGNED); //The local device specified by the lpLocalName member is already connected to a network resource.
      caseErr(ERROR_BAD_DEV_TYPE); //The type of local device and the type of network resource do not match.
      caseErr(ERROR_BAD_DEVICE); //The value specified by lpLocalName is invalid.
      caseErr(ERROR_BAD_NET_NAME); //The value specified by the lpRemoteName member is not acceptable to any network resource provider, either because the resource name is invalid, or because the named resource cannot be located.
      caseErr(ERROR_BAD_PROFILE); //The user profile is in an incorrect format.
      caseErr(ERROR_BAD_PROVIDER); //The value specified by the lpProvider member does not match any provider.
      caseErr(ERROR_BUSY); //The router or provider is busy, possibly initializing. The caller should retry.
      caseErr(ERROR_CANCELLED); //The attempt to make the connection was canceled by the user through a dialog box from one of the network resource providers, or by a called resource.
      caseErr(ERROR_CANNOT_OPEN_PROFILE); //The system is unable to open the user profile to process persistent connections.
      caseErr(ERROR_DEVICE_ALREADY_REMEMBERED); //An entry for the device specified by lpLocalName is already in the user profile.
      caseErr(ERROR_EXTENDED_ERROR); //A network-specific error occurred. Call the WNetGetLastError function to obtain a description of the error.
      caseErr(ERROR_INVALID_PASSWORD); //The specified password is invalid and the CONNECT_INTERACTIVE flag is not set.
      caseErr(ERROR_NO_NET_OR_BAD_PATH); //The operation cannot be performed because a network component is not started or because a specified name cannot be used.
      caseErr(ERROR_NO_NETWORK); //The network is unavailable.
      caseErr(ERROR_BAD_NETPATH); //The network path was not found.
    default:
      {
        printf("Error: %ld\n", dwResult);
      }
    } 
  }

  return dwResult;
}

//断开映射
DWORD CancelConnection(LPCTSTR szLocalname)
{
  DWORD dwResult; 

  // Call the WNetCancelConnection2 function, specifying
  //  that the connection should no longer be a persistent one.
  //
  dwResult = WNetCancelConnection2(szLocalname, 
    CONNECT_UPDATE_PROFILE, // remove connection from profile 
    FALSE);                 // fail if open files or jobs 

  // Process errors.
  //  The device is not a local redirected device.
  //
  if (dwResult == ERROR_NOT_CONNECTED) 
  { 
  }
  // Call an application-defined error handler.
  //
  else if(dwResult != NO_ERROR) 
  { 
    #define caseErr(x) case(x): { printf(#x "\n"); break;  }

    switch(dwResult)
    { 
      caseErr(ERROR_BAD_PROFILE);//The user profile is in an incorrect format.
      caseErr(ERROR_CANNOT_OPEN_PROFILE);//The system is unable to open the user profile to process persistent connections.
      caseErr(ERROR_DEVICE_IN_USE);//The device is in use by an active process and cannot be disconnected.
      caseErr(ERROR_EXTENDED_ERROR);//A network-specific error occurred. To obtain a description of the error, call the WNetGetLastError function.
      caseErr(ERROR_NOT_CONNECTED);//The name specified by the lpName parameter is not a redirected device, or the system is not currently connected to the device specified by the parameter.
      caseErr(ERROR_OPEN_FILES);//There are open files, and the fForce parameter is FALSE.
    default:
      {
        printf("Error: %ld\n", dwResult);
      }
    }

  }
  else
  {
    printf("Connection closed for %s.", szLocalname); 
  }

  return dwResult;
}

int _tmain(int argc, _TCHAR* argv[])
{
  AddConnection(_T("\\\\192.168.0.66\\SharedDocs"), _T("Q:"));
  CancelConnection(_T("Q:"));
  return 0;
}


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值