只是我用BCB写的代码,希望对你有所帮助: void __fastcall TForm1::SpeedButton1Click(TObject *Sender) { NETRESOURCE NR; DWORD dwResult; AnsiString Msg ; char Remote[128], Local[128]; char Login[32], Password[32] ; wsprintf((LPSTR) Remote , "%s", txtRemoteName->Text); wsprintf((LPSTR) Local , "%s", txtLocalName->Text); wsprintf((LPSTR) Login , "%s", txtLogin->Text); wsprintf((LPSTR) Password, "%s", txtPassword->Text); NR.dwScope = RESOURCE_GLOBALNET ; NR.dwType = RESOURCETYPE_ANY ; NR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE ; NR.dwUsage = RESOURCEUSAGE_CONNECTABLE ; NR.lpRemoteName = Remote ; NR.lpLocalName = Local; NR.lpProvider = NULL ; // 连接网络驱动器 dwResult = WNetAddConnection3(Form1->Handle, &NR, Password, Login, false) ; switch (dwResult) { case ERROR_ACCESS_DENIED: Msg = "Access to the network resource was denied." ; break ; case ERROR_ALREADY_ASSIGNED: Msg = "The local device is already connecteded to a network resource." ; break ; case ERROR_BAD_DEV_TYPE: Msg = "The type of local device and the type of network resource do not match." ; break ; case ERROR_BAD_DEVICE: Msg = "The value specified by lpLocalName is invalid." ; break ; case ERROR_BAD_NET_NAME: Msg = "The resource name is invalid, or the named resource cannot be located." ; break ; case ERROR_BAD_PROFILE: Msg = "The user profile is in an incorrect format." ; break ; case ERROR_BAD_PROVIDER: Msg = "The value specified by lpProvider does not match any provider." ; break ; case ERROR_BUSY: Msg = "The value specified by lpProvider does not match any provider." ; break ; case ERROR_CANCELLED: Msg = "The connection was cancelled by the user." ; break ; case ERROR_CANNOT_OPEN_PROFILE: Msg = "The system is unable to open the user profile." ; break ; case ERROR_DEVICE_ALREADY_REMEMBERED: Msg = "An entry for the device specified in lpLocalName is already in the user profile." ; break ; case ERROR_EXTENDED_ERROR: Msg = "A network-specific error occured." ; break ; case ERROR_INVALID_PASSWORD: Msg = "The password is invalid." ; break ; case ERROR_NO_NET_OR_BAD_PATH: Msg = "A network component has not started." ; break ; case ERROR_NO_NETWORK: Msg = "No network." ; break ; } if (Msg.Length() > 0) MessageDlg(Msg, mtError, TMsgDlgButtons() << mbOK, 0) ; }
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
NETRESOURCE NR;
DWORD dwResult;
AnsiString Msg ; char Remote[128], Local[128];
char Login[32], Password[32] ; wsprintf((LPSTR) Remote , "%s", txtRemoteName->Text);
wsprintf((LPSTR) Local , "%s", txtLocalName->Text);
wsprintf((LPSTR) Login , "%s", txtLogin->Text);
wsprintf((LPSTR) Password, "%s", txtPassword->Text); NR.dwScope = RESOURCE_GLOBALNET ;
NR.dwType = RESOURCETYPE_ANY ;
NR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE ;
NR.dwUsage = RESOURCEUSAGE_CONNECTABLE ;
NR.lpRemoteName = Remote ;
NR.lpLocalName = Local;
NR.lpProvider = NULL ; // 连接网络驱动器
dwResult = WNetAddConnection3(Form1->Handle, &NR,
Password,
Login,
false) ; switch (dwResult)
{
case ERROR_ACCESS_DENIED:
Msg = "Access to the network resource was denied." ;
break ;
case ERROR_ALREADY_ASSIGNED:
Msg = "The local device is already connecteded to a network resource." ;
break ;
case ERROR_BAD_DEV_TYPE:
Msg = "The type of local device and the type of network resource do not match." ;
break ;
case ERROR_BAD_DEVICE:
Msg = "The value specified by lpLocalName is invalid." ;
break ;
case ERROR_BAD_NET_NAME:
Msg = "The resource name is invalid, or the named resource cannot be located." ;
break ;
case ERROR_BAD_PROFILE:
Msg = "The user profile is in an incorrect format." ;
break ;
case ERROR_BAD_PROVIDER:
Msg = "The value specified by lpProvider does not match any provider." ;
break ;
case ERROR_BUSY:
Msg = "The value specified by lpProvider does not match any provider." ;
break ;
case ERROR_CANCELLED:
Msg = "The connection was cancelled by the user." ;
break ;
case ERROR_CANNOT_OPEN_PROFILE:
Msg = "The system is unable to open the user profile." ;
break ;
case ERROR_DEVICE_ALREADY_REMEMBERED:
Msg = "An entry for the device specified in lpLocalName is already in the user profile." ;
break ;
case ERROR_EXTENDED_ERROR:
Msg = "A network-specific error occured." ;
break ;
case ERROR_INVALID_PASSWORD:
Msg = "The password is invalid." ;
break ;
case ERROR_NO_NET_OR_BAD_PATH:
Msg = "A network component has not started." ;
break ;
case ERROR_NO_NETWORK:
Msg = "No network." ;
break ;
} if (Msg.Length() > 0)
MessageDlg(Msg, mtError, TMsgDlgButtons() << mbOK, 0) ;
}