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