WNetCancelConnection2 - IPC Connection does not disconnect - what am I doing wrong ?

I'm using the following function to make an IPC connection to another machine:

function ConnectToServer(sDrive, sServerName, sAccountName, sPassword: String; bConnectViaIPC, bRaiseException: Boolean; Var iErrorStatus: Integer; Var sErrorMessage: String): Boolean;
Var
  bResult     : Boolean;
  iStatus     : Integer;
  sRemoteName : String;
  NetResource : TNetResource;
  tfTest      : TextFile;
begin
  bResult := False;
  iStatus := 0;
  sErrorMessage := '';
  if bConnectViaIPC then
    sRemoteName := '\\' + sServerName + '\IPC$'
    else sRemoteName := '\\' + sServerName + '\C$';
  { fill our TNetResource record structure }
  NetResource.dwType       := RESOURCETYPE_ANY; //RESOURCETYPE_DISK;
  if bConnectViaIPC then
    NetResource.lpLocalName := ''
    else NetResource.lpLocalName  := PChar(sDrive);
  NetResource.lpRemoteName := PChar(sRemoteName);
  NetResource.lpProvider   := '';
  { map our network drive using our TNetResource record structure }
  Try
    iStatus := WNetAddConnection2(NetResource, PChar(sPassword), PChar(sAccountName), 0);
    if iStatus <> 0 then
    Begin
      iErrorStatus := iStatus;
      sErrorMessage := SysErrorMessage(iErrorStatus);
    End;
  Except
    if bRaiseException then
      exception.Create(PChar('Connection to ' + sRemoteName + ' failed'));
  End;
  Result := iStatus = 0;
end;

and this function is used to disconnect from that machine:

function DisconnectFromServer(sDrive, sServerName: String; bViaIPC: Boolean): Integer;
begin
  if bViaIPC then
    Result := WNetCancelConnection2(PChar('\\' + sServerName + '\IPC$'), 0, true )
    else Result := WNetCancelConnection2(PChar(sDrive), CONNECT_UPDATE_PROFILE, true );
end;

I can connect without any problems and do everything I need to do but the disconnect always fails with error "2404 - The device is in use by an active process and cannot be disconnected" but if I drop down to a cmd prompt and do "net use \\servername\ipc$ /delete" the disconnect works without any problems. I have tried connecting to different machines but that doesn't make any different. I have also tried the WNetCancelConnection2 command with just "\\servername" rather than "\\servername\IPC$" but that does make any different either.

It is driving me crazy, what am I doing wrong ????

Thanks,

Graham
LVL 1
Graham9295Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ziolkoConnect With a Mentor Commented:
well this works for me just fine:

procedure TForm1.Button5Click(Sender: TObject);
var nr: TNetResource;
begin
   nr.dwType := RESOURCETYPE_ANY;
   nr.lpLocalName := nil;
   nr.lpRemoteName := '\\machine_name\IPC$';
   nr.lpProvider := nil;
   if WNetAddConnection2(nr, nil, nil, CONNECT_UPDATE_PROFILE) = NO_ERROR then begin
     ShowMessage('connected');
     if WNetCancelConnection2(nr.lpRemoteName, CONNECT_UPDATE_PROFILE, True) = NO_ERROR then
       ShowMessage('disconnected');
   end;
end;

ziolko.
0
 
Graham9295Author Commented:
Ziolko - Many thanks for that, not only does it work I think I have worked out what I was doing wrong. In my code I wasn't declaring the TNetResource in the disconnect and thus not setting LocalName & Provider to Nil and I suspect it was a combination of all three that caused my issues. Disconnect works like a dream now and I can stop banging my head against the wall!!!
0
 
ziolkoCommented:
somrtimes most obvious solutions are hardest to find:)

hey watch out for that wall don't damage it to much ;)

ziolko.
0
All Courses

From novice to tech pro — start learning today.