Shared Windows NT / 2000 / XP in Folders in LAN
In the LAN, through the program to share the sharing of folders, I know at least two implementations should be implemented. First, modify the registry, but the problem of this method is also very obvious, and the machine must be restarted to take effect. The second is to use the Windows API function netshareadd, which we can easily implement the sharing of folders through this function, without restarting your computer. When using this function, we must pay attention to the use of the WINDOWS NT / 2000 / XP and Windows 95/98 / ME, this, I believe everyone has experience, clearly achieved it in 95 or 98. However, there is a problem in one to NT.
In fact, it is not only a friend, I have already raised this problem, and I have never solved it. Now I am fine, I hope to give you a little bit after reading it.
NetshareAdd function declaration under Windows 95/98 / ME declaration in the SvRAPI.dll dynamic connection library, and declares in the NetApi32.dll dynamic connection library in 2000 / XP / NT. So we must pay attention to call different DLL libraries in different operating systems. Detailed declarations of these functions are introduced in the new MSDN 2002. Since there is no declaration in Delphi and their parameters so we must implement this function (may be Delphi declaration I don't know in that unit). By the way, I am using the delphi5.0 version, but the help file is really too old, or first look at the statement of the NetShareAdd function in MSDN 2002!
Windows NT / 2000 / XP:
NET_API_STATUS NETSHAREADD (
LPWSTR ServerName, // PWIDECHAR in Delphi
DWord Level, // Corresponding to Delphi Dwoed
LPBYTE BUF, // Corresponds to Delphi PBYTE
LPDWORD PARM_ERR / / PDWORD in Delphi
);
Windows 95/98 / ME: Don't say it right below the corresponding parameters! You can look at the Delphi Help file directly.
Extern API_Function
Netshareadd
Const Char Far * PSZServer,
Short Slevel,
Const Char Far * PBBuffer,
Unsigned Short Cbbuffer
);
With particular emphasis on:
When we declare the above functions, the function parameters must be written correctly, which must be correctly corresponding to Delphi's own type. Otherwise, the function function cannot be implemented, I have tried this. The reason why it does not implement the main or parameter type under NT. We also need to declare a record type, declare under 98/95 / mE and NT / 2000 / XP as follows:
Windows NT / 2000 / XP: Share_INFO_2 and Share_INFO_502 Structure
Windows 95/98 / Me: SHARE_INFO_50 Structure
The statement of the above structure should pay attention to the correct correspondence of the parameter type. The original statement is as follows:
Typedef struct _share_info_502 {
LPWSTR shi502_netname; // pwidechar;
DWORD SHI502_TYPE; // DWORD;
LPWSTR shi502_remark; // pwidechar;
DWORD SHI502_PERMISSIONS; // DWORD;
DWORD SHI502_MAX_USES; // DWORD;
DWORD SHI502_CURRENT_USES; // DWORD; LPWSTR SHI502_PATH; / / PWIDECHAR;
LPWSTR SHI502_PASSWD; // PWIDECHAR;
DWORD SHI502_RESERVED; // DWORD;
// psecurity_descriptor; generally set to nil
Psecurity_descriptor shi502_security_descriptor;
} Share_info_502, * pshare_info_502, * lpshare_info_502;
The corresponding Delphi record declaration is as follows: Be sure to pay attention to the correct correspondence of the parameter type, if you declare the PWIDECHAR to PCHAR functions will not be able to implement this function, I have tried, you can try again, what is the reason, I am not very clear .
Type
Tshare_INFO_502 = Record
Shi502_netname: PWIDECHAR;
SHI502_TYPE: DWORD;
Shi502_remark: pwidechar;
SHI502_PERMISSITIONS: DWORD;
SHI502_MAX_USES: DWORD;
SHI502_CURRENT_USES: DWORD;
SHI502_PATH: PWIDECHAR;
Shi502_passwd: pwidechar;
SHI502_RESERVED: DWORD;
SHI502_Security_Descriptor: psecurity_descriptor;
END;
The following is the completed program code, which have two parts, main programs and unit files. Running Environment Windows 2000 ADS Development Tool Delphi5.0. Run.
Unit Share;
Interface
Uses
Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs,
Stdctrls, FileCtrl, My_Share;
Type
TFormshare = Class (TFORM)
Label1: TLABEL;
Label2: TLABEL;
Label3: TLABEL;
Btselect: tbutton;
EditDir: Tedit; // File Share Directory
Editsharename: TEDIT; // Share Name
Editinfo: Tedit; // Remarks
Button1: tbutton;
Button2: tbutton;
Procedure BtselectClick (Sender: TOBJECT);
Procedure Button1Click (Sender: TOBJECT);
Private
{Private Declarations}
public
{Public declarations}
END;
VAR
Formshare: tFormshare;
IMPLEMENTATION
{$ R * .dfm}
Procedure TFormshare.btselectclick (sender: TOBJECT);
VAR
Directory: String;
Begin
If SELECTDIRECTORY ('Choose a directory', '', directory) THEN
EditDir.Text: = Directory;
END;
Procedure TFormshare.Button1Click (Sender: TOBJECT);
Begin
if EditDir.Text = '' THEN
Begin
Application.MessageBox ('Please select a directory first!', 'Share', MB_ICONITION MB_OK); btselect.click;
EXIT;
END;
if editsharename.text = '' THEN
Begin
Application.MessageBox ('Please enter the shared name first!', 'Share', MB_ICONITION MB_OK);
Editsharename.Setfocus;
EXIT;
END;
Shareresource ('EENGI', EditDir.Text, EditSharename.text, Editinfo.text);
{Note: If you add a $ symbol after the shared directory name, you can't see this folder in the network neighbor.
But actually shared, you can see it locally.
END;
End.
The following is a unit file:
Unit my_share;
Interface
Uses
Windows, sysutils;
Type
// Record type statement, pay attention to the correct correspondence of parameter type, it is best not to see Delphi's help, causing misleading
Tshare_INFO_502 = Record
Shi502_netname: PWIDECHAR;
SHI502_TYPE: DWORD;
Shi502_remark: pwidechar;
SHI502_PERMISSITIONS: DWORD;
SHI502_MAX_USES: DWORD;
SHI502_CURRENT_USES: DWORD;
SHI502_PATH: PWIDECHAR;
Shi502_passwd: pwidechar;
SHI502_RESERVED: DWORD;
SHI502_Security_Descriptor: psecurity_descriptor;
END;
// Add sharing
Function netshareadd (ServerName: WideString; Level: DWORD; BUF: PBYTE
VAR PARM_ERR: PDWORD: DWORD; STDCALL;
// Delete Sharing
Function netsharedl (servername); NetName: WideString
Reserved: DWORD: Integer; stdcall;
Const
{Shared type}
STYPE_DISKTREE = 0;
STYPE_PRINTQ = 1;
STYPE_DEVICE = 2;
STYPE_IPC = 3;
{access permission}
Access_read = 0;
Access_write = 1;
Access_create = 2;
Access_exec = 3;
Access_delete = 4;
Access_all = 7;
// The function of your own declaration, for the convenience of calling, the parameters don't explain it!
Function Shareresource (ServerName, Filepath, NetName, Remark: String): Integer;
// Function deleteshare (ServerName: String; NetName: String): Integer;
IMPLEMENTATION
/ (Note that the DLL library under Windows95 / 98 / ME is SvRAPI.dll, and the parameter type is also changed! Function netshareadd; external 'Netapi32.dll' name 'netshareadd';
Function netsharedel; external 'Netapi32.dll' name 'netshaedel';
Function Shareresource (ServerName, Filepath, NetName, Remark: String): Integer;
VAR
SHINFO: TSHARE_INFO_502;
PARM_ERR: PDWORD;
_Filepath, _neetname, _remark: pwidecha;
_ServerName: pchar;
Begin
GetMem (_ServerName, 255); // Assign memory
GetMem (_filepath, 255);
GetMem (_NetName, 255);
GetMem (_Remark, 255);
StringTowideChar (filepath, _filepath, 255); // String conversion, must be converted correctly
StringTowideChar (NetName, _NetName, 255);
Stringtowidechar (Remark, _Remark, 255);
Strpcopy (_ServerName, ServerName);
// Start creating structure
With shinfo do
Begin
SHI502_NETNAME: = _netName;
SHI502_TYPE: = STYPE_DISKTREE
Shi502_remark: = _Remark;
SHI502_MAX_USES: = $ fffffff;
SHI502_CURRENT_USES: = 10;
SHI502_PATH: = _filepath;
SHI502_PASSWD: = NIL;
SHI502_RESERVED: = 0;
SHI502_Security_Descriptor: = nil;
Shi502_permissions: = access_all;
END;
Try
Result: = netshareadd (_ServerName, 502, @shinfo, parm_err);
Finally // Don't forget to release memory
FreeMem (_ServerName, 255);
Freemem (_filepath, 255);
FreeMem (_NetName, 255);
Freemem (_Remark, 255);
END;
END;
End.
to sum up:
After running the process, I believe that everyone has the same feeling. In fact, it is a little bit in front of you, but it is difficult to span this. The reason why the program has been unable, I am afraid we have not considered whether the parameter type declaration is correct. However, I should be very grateful to the "absolute rookie" reply on 9CBS. If it is not his program, I am afraid I am still waiting to explore.