映射網際網路共用資源為本地硬碟

來源:互聯網
上載者:User
#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;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.