Board index » delphi » WNetAddConnection2 example needed!

WNetAddConnection2 example needed!

Has anyone tried to use this call?
I am having trouble with the NETRESOURCE Struct.  

Thanks.

--
-----------------------------------------------------------------
Arto Oltaci
Distributed Computing Support
Information Technology      
Canadian National Railway Company
(514) 399-7218
-----------------------------------------------------------------

 

Re:WNetAddConnection2 example needed!


Quote
Arto Oltaci wrote:

> Has anyone tried to use this call?
> I am having trouble with the NETRESOURCE Struct.

> Thanks.

Here is what I have so far.  I wonder why it won't compile.

var
   NetRes       : TNetResource;
   STMapResult  : DWORD;

begin

     NetRes.dwScope := RESOURCE_GLOBALNET;
     NetRes.dwType := RESOURCETYPE_DISK;
     NetRes.dwDisplayType := RESOURCEDISPLAYTYPE_SHARE;
     NetRes.dwUsage := RESOURCEUSAGE_CONNECTABLE;
     NetRes.lpLocalName := 'M';
     NetRes.lpRemoteName := '\\FS Wrk Grp@CN_MTL_HQ5B@Servers';
     NetRes.lpComment := nil;
     NetRes.lpProvider := 'StreetTalk';

     STMapResult := WNetAddConnection2(NetRes,nil,nil,nil);

end

** The compiler stops at the last line:
Error:.... Types of actual and formal parameters must be identical.
Error:.... Incompatible types: 'Integer' and 'Pointer'.

Wouldn't it be nice if Borland provided *examples* in the help for
each function?  It's the first thing I noticed!

--
-----------------------------------------------------------------
Arto Oltaci
Distributed Computing Support
Information Technology      
Canadian National Railway Company
(514) 399-7218
-----------------------------------------------------------------

Re:WNetAddConnection2 example needed!


Quote
Arto Oltaci <olt...@cn.ca> wrote:
>Has anyone tried to use this call?
>I am having trouble with the NETRESOURCE Struct.  
>Thanks.

Hello there Arto,

I use  WNetAddConnection2 in a lot of programs at work but not at home

so all this is from memory ,but I think should work

the Wnet family of Functions for the windows network can be viewed in
delphi 2.0\source\rtl\win\windows.pas

It is useful to open this & have a root around
to look at the function & data type definitions
Don't change it though!

The function header looks like this:

function WNetAddConnection2(var lpNetResource: TNetResource;
  lpPassword, lpUserName: PChar; dwFlags: DWORD): DWORD; stdcall;

The definition of Tnetresource is:

  TNetResourceA = packed record
    dwScope: DWORD;
    dwType: DWORD;
    dwDisplayType: DWORD;
    dwUsage: DWORD;
    lpLocalName: PAnsiChar;
    lpRemoteName: PAnsiChar;
    lpComment: PAnsiChar;
    lpProvider: PAnsiChar;
  end;

  TNetResource = TNetResourceA;

Use the function pchar() to cast strings to Pansichar

To use WNetAddConnection2 try

Function Connect:boolean;
var
  nr:Tnetresource;
  err:dword;
begin
  nr.dwtype:=ResourceType_Any;
  nr.lplocalname:=nil;
  nr.lpremotename:=pchar('\\bobs comp\c');
  // name of remote computer is  bobs comp, it has C drive  shared
  nr.lpprovider:=nil;

  err:=WNetAddConnection2(nr,pchar('letmein'),nil,0);
  //password is letmein

  result:=true;
  IF err<>NO_ERROR THEN
  begin
    messagedlg(syserrormessage(err),MtError,[mbok],0);
    result:=false;
  end;

end;

This is a very basic function which returns true if connection is
Succesful, and returns false & displays an error message if not.
There are many ways to improve this function, but it should get
you started.

Once you have established a connection, you can access files by just
giving the path,  e.g if bobs comp has a file
        C:\my documents\UnlimitedWealth.doc
You can access it using the path as
         '\\bobs comp\c\my documents\UnlimitedWealth.doc'

Check out win32 help for various Other options eg redirecting to a
local name  etc.

Hope this helps

Greeny

Other Threads