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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ziolkoCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.