Thursday, July 03, 2008

Connecting to network computer with WNetAddConnection2

Source:http://delphi.newswhat.com/geoxml/forumhistorythread?groupname=borland.public.delphi.nativeapi.win32&messageid=45eed721@newsgroups.borland.com

I'm on winXP and trying to create a network connection to win98 virtual pc
(\\Win98_vpc) with the WNetAddConnection2 function. I've shared the C drive
of the virtual pc with a password ("password").

I use DirectoryExists('\\win98_vpc\C\Downloads') to test whether I'm
connected or not.

I call ConnectToNetwork('\\win98_vpc', 'Guest', 'password') (below) to
connect. The function returns 0 (No errors), but call to DirectoryExists
will still fail.

If I try to browse the C drive in windows explorer login screen still pops
up. My assumption is that a successful WNetAddConnection2 should remove this
login dialog, and likewise WNetCancelConnection2 should make it come back
again. Even though my calls to these functions succeed, they still do not
effect the windows explorer (after I login with pop up screen and then call
WNetCancelConnection2, the login screen will not appear).

Any ideas of what's wrong?

Thanks
Rael


Function ConnectToNetwork(MachineName, Username, Password: String): Integer;
Var
NetResource: TNetResource;
Begin
ZeroMemory(@NetResource, sizeof(TNetResource));
With NetResource do Begin
dwType := RESOURCETYPE_ANY;
lpLocalName := nil;
lpProvider := nil;
lpRemoteName := PChar(MachineName);
End;

Result := WNetAddConnection2(NetResource, pchar(Password),
pchar(Username), 0);
Form1.Memo1.Lines.Add(SysErrorMessage(GetLastError));
Form1.Memo1.Lines.Add(IntToStr(Result));

If Result = ERROR_SESSION_CREDENTIAL_CONFLICT Then Begin
WNetCancelConnection2(PChar(MachineName), 0, false);
Result := WNetAddConnection2(NetResource, pchar(Password),
pchar(Username), 0);
Form1.Memo1.Lines.Add(SysErrorMessage(GetLastError));
Form1.Memo1.Lines.Add(IntToStr(Result));
End;
End;

(no difference if I add '\IPC$' to machinename)

0 Comments: