K
KenLee
Unregistered / Unconfirmed
GUEST, unregistred user!
我的三层结构的程序结构是这样的,数据库服务器、中间层应用程序服务器都用2000server,
客户端用98。中间层程序和客户端程序都放在中间层(一个2000server)一个共享目录里,
客户端(98)机器只使用该共享目录中的客户端程序的快捷方式。
由于客户端程序需要读写其他很多配置文件,这些文件也都放在该共享目录下,并且所有客户
端(98)机器都有读写该共享目录的权限。问题是,这些配置文件我希望只能由程序去读写它,
但不允许其他人为的修改。
解决的办法我想是把这些配置文件放在另一个目录下,并不给98客户端机器有访问权限,然后
在程序中动态建立网络连接,用完释放。
尝试过的函数有:WNetCancelConnection2、net use 和LogonUser,都失败了。
WNetCancelConnection2(98下)“can not open file ……”
LogonUser在98下提示该功能仅在win32模式下有效;在2000professional中又提示用户没有
需要的特权(但是已经受权'SeTcbPrivilege',见所附程序)
附:测试程序。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
Label1: TLabel;
Memo1: TMemo;
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
NRW: TNetResource;
d : dword;
begin
with NRW do
begin
dwType:= RESOURCETYPE_ANY;
lpLocalName:='T:';//PChar(LocalDriver); //映射的网络驱动器,取自己最高盘符后面一个就可以了。
lpRemoteName:='//APPSERVER/photo';
//PChar(ShareName); //共享文件夹名
lpProvider:='';
end;
try
d := WNetAddConnection2(NRW, PChar('fwzh'), PChar('fwzh'), CONNECT_UPDATE_PROFILE);
if d=NO_ERROR then
Image1.Picture.LoadFromFile(NRW.lpRemoteName + '/FaSongBtn_Hot.bmp')
else begin
showMessage( IntToStr(d));
case d of
ERROR_ACCESS_DENIED :showMessage(' Access to the network resource was denied. ');
ERROR_ALREADY_ASSIGNED :showMessage(' The local device specified by lpLocalName is already connected to a network resource.');
ERROR_BAD_DEV_TYPE :showMessage(' The type of local device and the type of network resource do not match.');
ERROR_BAD_DEVICE :showMessage(' The value specified by lpLocalName is invalid.');
ERROR_BAD_NET_NAME :showMessage(' The value specified by lpRemoteName is not acceptable to any network resource provider. The resource name is invalid, or the named resource cannot be located.');
ERROR_BAD_PROFILE :showMessage(' The user profile is in an incorrect format.');
ERROR_BAD_PROVIDER :showMessage(' The value specified by lpProvider does not match any provider.');
ERROR_BUSY :showMessage('The router or provider is busy, possibly initializing. The caller should retry.');
ERROR_CANCELLED :showMessage('The attempt to make the connection was cancelled by the user through a dialog box from one of the network resource providers, or by a called resource.');
ERROR_CANNOT_OPEN_PROFILE : showMessage('The system is unable to open the user profile to process persistent connections.');
ERROR_DEVICE_ALREADY_REMEMBERED: showMessage('An entry for the device specified in lpLocalName is already in the user profile.');
ERROR_EXTENDED_ERROR : showMessage('A network-specific error occured. Call the WNetGetLastError function to get a description of the error.');
ERROR_INVALID_PASSWORD : showMessage('The specified password is invalid.');
ERROR_NO_NET_OR_BAD_PATH : showMessage('A network component has not started, or the specified name could not be handled.');
ERROR_NO_NETWORK : showMessage('There is no network present.');
end;
end;
finally
d := WNetCancelConnection2(PChar('T:'),CONNECT_UPDATE_PROFILE, FALSE);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Edit1.Text := ExtractFilePath(ParamStr(0));
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
WinExec(Pchar('net use //appserver/photo/ fwzh/fwzh'),sw_Hide);
Image1.Picture.LoadFromFile('//appserver/photo/FaSongBtn_Hot.bmp');
WinExec(Pchar('net use //appserver/photo /delete'),sw_Hide);
end;
function GetErrorMessage(Code:Integer):String;
var
hErrLib:THandle;
msgChar;
flags:Integer;
function MAKELANGID(p,s:word):Integer;
begin
result:=(s shl 10) or p
end;
begin
hErrLib:=LoadLibraryEx ('netmsg.dll', 0, LOAD_LIBRARY_AS_DATAFILE);
try
flags:=FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_IGNORE_INSERTS or FORMAT_MESSAGE_FROM_SYSTEM;
if hErrLib<>0 then flags := flags or FORMAT_MESSAGE_FROM_HMODULE;
if FormatMessage(flags,pointer(hErrLib),code,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),PChar(@msg),0,Nil)<>0 Then
try
result:=msg;
finally
LocalFree(Integer(msg));
end
finally
if hErrLib <> 0 then FreeLibrary (hErrLib)
end
end;
procedure TForm1.Button4Click(Sender: TObject);
var
hdlProcessHandle : Thandle;
hdlTokenHandle : Thandle;
tmpLuid : Int64;
tkp : TOKEN_PRIVILEGES;
tkpNewButIgnored : TTokenPrivileges;// TOKEN_PRIVILEGES;
lBufferNeeded : Cardinal;
tkn : Thandle;
FErrorWORD;
begin
hdlProcessHandle := GetCurrentProcess;
OpenProcessToken(hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY), hdlTokenHandle);
// Get the LUID for shutdown privilege.
if not LookupPrivilegeValue('', 'SeTcbPrivilege', tmpLuid) then
begin
showmessage('LookupPrivilegeValue');
FError:=GetLastError;
Application.MessageBox(PChar(GetErrorMessage(FError)),'',mb_OK + mb_ICONINFORMATION);//显示出错原因
end;
tkp.PrivilegeCount := 1; // ' One privilege to set
tkp.Privileges[0].Luid := tmpLuid;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
// Enable the shutdown privilege in the access token of this process.
//AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );
if not AdjustTokenPrivileges(hdlTokenHandle, False, tkp,
SizeOf({tkpNewButIgnored}TTokenPrivileges), tkpNewButIgnored, lBufferNeeded) then
begin
showmessage('AdjustToken');
FError:=GetLastError;
Application.MessageBox(PChar(GetErrorMessage(FError)),'',mb_OK + mb_ICONINFORMATION);//显示出错原因
end;
if not LogonUser(PChar('fwzh'),
pchar('appdomain'),
PChar('fwzh'),
LOGON32_LOGON_NETWORK,
LOGON32_PROVIDER_DEFAULT,
hdlTokenHandle) then
begin
FError:=GetLastError;
Application.MessageBox(PChar(GetErrorMessage(FError)),'',mb_OK + mb_ICONINFORMATION);//显示出错原因
Exit;
end
end;
end.
客户端用98。中间层程序和客户端程序都放在中间层(一个2000server)一个共享目录里,
客户端(98)机器只使用该共享目录中的客户端程序的快捷方式。
由于客户端程序需要读写其他很多配置文件,这些文件也都放在该共享目录下,并且所有客户
端(98)机器都有读写该共享目录的权限。问题是,这些配置文件我希望只能由程序去读写它,
但不允许其他人为的修改。
解决的办法我想是把这些配置文件放在另一个目录下,并不给98客户端机器有访问权限,然后
在程序中动态建立网络连接,用完释放。
尝试过的函数有:WNetCancelConnection2、net use 和LogonUser,都失败了。
WNetCancelConnection2(98下)“can not open file ……”
LogonUser在98下提示该功能仅在win32模式下有效;在2000professional中又提示用户没有
需要的特权(但是已经受权'SeTcbPrivilege',见所附程序)
附:测试程序。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
Label1: TLabel;
Memo1: TMemo;
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
NRW: TNetResource;
d : dword;
begin
with NRW do
begin
dwType:= RESOURCETYPE_ANY;
lpLocalName:='T:';//PChar(LocalDriver); //映射的网络驱动器,取自己最高盘符后面一个就可以了。
lpRemoteName:='//APPSERVER/photo';
//PChar(ShareName); //共享文件夹名
lpProvider:='';
end;
try
d := WNetAddConnection2(NRW, PChar('fwzh'), PChar('fwzh'), CONNECT_UPDATE_PROFILE);
if d=NO_ERROR then
Image1.Picture.LoadFromFile(NRW.lpRemoteName + '/FaSongBtn_Hot.bmp')
else begin
showMessage( IntToStr(d));
case d of
ERROR_ACCESS_DENIED :showMessage(' Access to the network resource was denied. ');
ERROR_ALREADY_ASSIGNED :showMessage(' The local device specified by lpLocalName is already connected to a network resource.');
ERROR_BAD_DEV_TYPE :showMessage(' The type of local device and the type of network resource do not match.');
ERROR_BAD_DEVICE :showMessage(' The value specified by lpLocalName is invalid.');
ERROR_BAD_NET_NAME :showMessage(' The value specified by lpRemoteName is not acceptable to any network resource provider. The resource name is invalid, or the named resource cannot be located.');
ERROR_BAD_PROFILE :showMessage(' The user profile is in an incorrect format.');
ERROR_BAD_PROVIDER :showMessage(' The value specified by lpProvider does not match any provider.');
ERROR_BUSY :showMessage('The router or provider is busy, possibly initializing. The caller should retry.');
ERROR_CANCELLED :showMessage('The attempt to make the connection was cancelled by the user through a dialog box from one of the network resource providers, or by a called resource.');
ERROR_CANNOT_OPEN_PROFILE : showMessage('The system is unable to open the user profile to process persistent connections.');
ERROR_DEVICE_ALREADY_REMEMBERED: showMessage('An entry for the device specified in lpLocalName is already in the user profile.');
ERROR_EXTENDED_ERROR : showMessage('A network-specific error occured. Call the WNetGetLastError function to get a description of the error.');
ERROR_INVALID_PASSWORD : showMessage('The specified password is invalid.');
ERROR_NO_NET_OR_BAD_PATH : showMessage('A network component has not started, or the specified name could not be handled.');
ERROR_NO_NETWORK : showMessage('There is no network present.');
end;
end;
finally
d := WNetCancelConnection2(PChar('T:'),CONNECT_UPDATE_PROFILE, FALSE);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Edit1.Text := ExtractFilePath(ParamStr(0));
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
WinExec(Pchar('net use //appserver/photo/ fwzh/fwzh'),sw_Hide);
Image1.Picture.LoadFromFile('//appserver/photo/FaSongBtn_Hot.bmp');
WinExec(Pchar('net use //appserver/photo /delete'),sw_Hide);
end;
function GetErrorMessage(Code:Integer):String;
var
hErrLib:THandle;
msgChar;
flags:Integer;
function MAKELANGID(p,s:word):Integer;
begin
result:=(s shl 10) or p
end;
begin
hErrLib:=LoadLibraryEx ('netmsg.dll', 0, LOAD_LIBRARY_AS_DATAFILE);
try
flags:=FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_IGNORE_INSERTS or FORMAT_MESSAGE_FROM_SYSTEM;
if hErrLib<>0 then flags := flags or FORMAT_MESSAGE_FROM_HMODULE;
if FormatMessage(flags,pointer(hErrLib),code,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),PChar(@msg),0,Nil)<>0 Then
try
result:=msg;
finally
LocalFree(Integer(msg));
end
finally
if hErrLib <> 0 then FreeLibrary (hErrLib)
end
end;
procedure TForm1.Button4Click(Sender: TObject);
var
hdlProcessHandle : Thandle;
hdlTokenHandle : Thandle;
tmpLuid : Int64;
tkp : TOKEN_PRIVILEGES;
tkpNewButIgnored : TTokenPrivileges;// TOKEN_PRIVILEGES;
lBufferNeeded : Cardinal;
tkn : Thandle;
FErrorWORD;
begin
hdlProcessHandle := GetCurrentProcess;
OpenProcessToken(hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY), hdlTokenHandle);
// Get the LUID for shutdown privilege.
if not LookupPrivilegeValue('', 'SeTcbPrivilege', tmpLuid) then
begin
showmessage('LookupPrivilegeValue');
FError:=GetLastError;
Application.MessageBox(PChar(GetErrorMessage(FError)),'',mb_OK + mb_ICONINFORMATION);//显示出错原因
end;
tkp.PrivilegeCount := 1; // ' One privilege to set
tkp.Privileges[0].Luid := tmpLuid;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
// Enable the shutdown privilege in the access token of this process.
//AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );
if not AdjustTokenPrivileges(hdlTokenHandle, False, tkp,
SizeOf({tkpNewButIgnored}TTokenPrivileges), tkpNewButIgnored, lBufferNeeded) then
begin
showmessage('AdjustToken');
FError:=GetLastError;
Application.MessageBox(PChar(GetErrorMessage(FError)),'',mb_OK + mb_ICONINFORMATION);//显示出错原因
end;
if not LogonUser(PChar('fwzh'),
pchar('appdomain'),
PChar('fwzh'),
LOGON32_LOGON_NETWORK,
LOGON32_PROVIDER_DEFAULT,
hdlTokenHandle) then
begin
FError:=GetLastError;
Application.MessageBox(PChar(GetErrorMessage(FError)),'',mb_OK + mb_ICONINFORMATION);//显示出错原因
Exit;
end
end;
end.