用API: GetVersionExdwMajorVersion The major version number of the operating system. This member can be one of the following values.Value Meaning 4 The operating system is Windows NT 4.0, Windows Me, Windows 98, or Windows 95. 5 The operating system is Windows Server 2003 R2, Windows Server 2003, Windows XP, or Windows 2000. 6 The operating system is Windows Vista or Windows Server "Longhorn". dwMinorVersion The minor version number of the operating system. This member can be one of the following values.Value Meaning 0 The operating system is Windows Vista, Windows Server "Longhorn", Windows 2000, Windows NT 4.0, or Windows 95. 1 The operating system is Windows XP. 2 The operating system is Windows Server 2003 R2, Windows Server 2003, or Windows XP Professional x64 Edition. 10 The operating system is Windows 98. 90 The operating system is Windows Me. Operating system Version number Windows Server "Longhorn" 6.0 Windows Vista 6.0 Windows Server 2003 R2 5.2 Windows Server 2003 5.2 Windows XP 5.1 Windows 2000 5.0 Windows Me 4.90 Windows 98 4.10 Windows NT 4.0 4.0 Windows 95 4.0
不行啊,我的意思是:if myver<5.1 then quit; //2000 if myver>5.1 then 相关代码;在2k下相关代码还是出错的。
function Myver:real; var VersionInfo:TOSVersionInfo; begin Result:=5.1; //xp=5.1 2000=5.0 2003=5.2 VersionInfo.dwOSVersionInfoSize:=SizeOf(TOSVersionInfo); if Windows.GetVersionEx(VersionInfo) then Result:=VersionInfo.dwMajorVersion+(VersionInfo.dwMinorVersion) / 10; end;
2楼是说用dll吗,没有别的办法吗,程序不想带一个dll。
那就动态加载advapi32.dll这个dll
同志们,不行啊,这是完整代码,谁受累看看 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,unitacl, StdCtrls,JwaWinSafer;type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm} function dropme(RightLever: Integer; ApplicationName, CommandLine, CurrDir: String): DWORD; var hSaferLevel: DWORD; hAuthzLevel: SAFER_LEVEL_HANDLE; hToken: THandle; si:STARTUPINFO; pi: PROCESS_INFORMATION; begin Result := ERROR_SUCCESS; hSaferLevel := SAFER_LEVELID_NORMALUSER; hAuthzLevel := 0; if (SaferCreateLevel(SAFER_SCOPEID_USER, SAFER_LEVELID_NORMALUSER, 0, @hAuthzLevel, nil)) then begin hToken := 0; if (SaferComputeTokenFromLevel( hAuthzLevel, 0, @hToken, 0, nil)) then begin ZeroMemory(@si, sizeof(STARTUPINFO)); si.cb := sizeof(STARTUPINFO); si.lpDesktop := nil; if (CreateProcessAsUser(hToken, PAnsiChar(ApplicationName), PAnsiChar(CommandLine), nil, nil, False, CREATE_NEW_CONSOLE, nil, PAnsiChar(CurrDir), si, pi)) then begin CloseHandle(pi.hProcess); CloseHandle(pi.hThread); end else Result := GetLastError; end else Result := GetLastError; SaferCloseLevel(hAuthzLevel); end else Result := GetLastError; end; procedure TForm1.Button1Click(Sender: TObject); begin dropme(2, paramstr(0), '', ExtractFilePath(paramstr(0))); end; end.
改成动态还是不行: unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,unitacl, StdCtrls,JwaWinSafer;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm} function testdllfunc():boolean; var DllHandle:THandle; Dllfarproc:Tfarproc; begin result:=false; //动态调用dll DllHandle := LoadLibrary('advapi32.dll'); try if DllHandle <> 0 then begin Dllfarproc := GetProcAddress(DllHandle, 'SaferComputeTokenFromLevel'); //找函数名,大小写敏感 if Dllfarproc <> nil then begin result:=true; end; end; finally FreeLibrary(DllHandle); end; end; function dropme(RightLever: Integer; ApplicationName, CommandLine, CurrDir: String): DWORD; var hSaferLevel: DWORD; hAuthzLevel: SAFER_LEVEL_HANDLE; hToken: THandle; si:STARTUPINFO; pi: PROCESS_INFORMATION; begin Result := ERROR_SUCCESS; hSaferLevel := SAFER_LEVELID_NORMALUSER; hAuthzLevel := 0; if (SaferCreateLevel(SAFER_SCOPEID_USER, SAFER_LEVELID_NORMALUSER, 0, @hAuthzLevel, nil)) then begin hToken := 0; if (SaferComputeTokenFromLevel( hAuthzLevel, 0, @hToken, 0, nil)) then begin ZeroMemory(@si, sizeof(STARTUPINFO)); si.cb := sizeof(STARTUPINFO); si.lpDesktop := nil; if (CreateProcessAsUser(hToken, PAnsiChar(ApplicationName), PAnsiChar(CommandLine), nil, nil, False, CREATE_NEW_CONSOLE, nil, PAnsiChar(CurrDir), si, pi)) then begin CloseHandle(pi.hProcess); CloseHandle(pi.hThread); end else Result := GetLastError; end else Result := GetLastError; SaferCloseLevel(hAuthzLevel); end else Result := GetLastError; end; procedure TForm1.Button1Click(Sender: TObject); begin if testdllfunc then dropme(2, paramstr(0), '', ExtractFilePath(paramstr(0))); end; end.
The major version number of the operating system. This member can be one of the following values.Value Meaning
4 The operating system is Windows NT 4.0, Windows Me, Windows 98, or Windows 95.
5 The operating system is Windows Server 2003 R2, Windows Server 2003, Windows XP, or Windows 2000.
6 The operating system is Windows Vista or Windows Server "Longhorn". dwMinorVersion
The minor version number of the operating system. This member can be one of the following values.Value Meaning
0 The operating system is Windows Vista, Windows Server "Longhorn", Windows 2000, Windows NT 4.0, or Windows 95.
1 The operating system is Windows XP.
2 The operating system is Windows Server 2003 R2, Windows Server 2003, or Windows XP Professional x64 Edition.
10 The operating system is Windows 98.
90 The operating system is Windows Me. Operating system Version number
Windows Server "Longhorn" 6.0
Windows Vista 6.0
Windows Server 2003 R2 5.2
Windows Server 2003 5.2
Windows XP 5.1
Windows 2000 5.0
Windows Me 4.90
Windows 98 4.10
Windows NT 4.0 4.0
Windows 95 4.0
if myver>5.1 then 相关代码;在2k下相关代码还是出错的。
var
VersionInfo:TOSVersionInfo;
begin
Result:=5.1; //xp=5.1 2000=5.0 2003=5.2
VersionInfo.dwOSVersionInfoSize:=SizeOf(TOSVersionInfo);
if Windows.GetVersionEx(VersionInfo) then
Result:=VersionInfo.dwMajorVersion+(VersionInfo.dwMinorVersion) / 10;
end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,unitacl, StdCtrls,JwaWinSafer;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function dropme(RightLever: Integer; ApplicationName, CommandLine, CurrDir: String): DWORD;
var
hSaferLevel: DWORD;
hAuthzLevel: SAFER_LEVEL_HANDLE;
hToken: THandle;
si:STARTUPINFO;
pi: PROCESS_INFORMATION;
begin
Result := ERROR_SUCCESS;
hSaferLevel := SAFER_LEVELID_NORMALUSER;
hAuthzLevel := 0;
if (SaferCreateLevel(SAFER_SCOPEID_USER, SAFER_LEVELID_NORMALUSER, 0, @hAuthzLevel, nil)) then
begin
hToken := 0;
if (SaferComputeTokenFromLevel(
hAuthzLevel,
0,
@hToken,
0,
nil)) then
begin
ZeroMemory(@si, sizeof(STARTUPINFO));
si.cb := sizeof(STARTUPINFO);
si.lpDesktop := nil;
if (CreateProcessAsUser(hToken, PAnsiChar(ApplicationName), PAnsiChar(CommandLine),
nil, nil, False, CREATE_NEW_CONSOLE, nil, PAnsiChar(CurrDir), si, pi)) then
begin
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
end
else
Result := GetLastError;
end
else
Result := GetLastError;
SaferCloseLevel(hAuthzLevel);
end
else
Result := GetLastError;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
dropme(2, paramstr(0), '', ExtractFilePath(paramstr(0)));
end;
end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,unitacl, StdCtrls,JwaWinSafer;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function testdllfunc():boolean;
var
DllHandle:THandle;
Dllfarproc:Tfarproc;
begin
result:=false;
//动态调用dll
DllHandle := LoadLibrary('advapi32.dll');
try
if DllHandle <> 0 then begin
Dllfarproc := GetProcAddress(DllHandle, 'SaferComputeTokenFromLevel'); //找函数名,大小写敏感
if Dllfarproc <> nil then begin
result:=true;
end;
end;
finally
FreeLibrary(DllHandle);
end;
end;
function dropme(RightLever: Integer; ApplicationName, CommandLine, CurrDir: String): DWORD;
var
hSaferLevel: DWORD;
hAuthzLevel: SAFER_LEVEL_HANDLE;
hToken: THandle;
si:STARTUPINFO;
pi: PROCESS_INFORMATION;
begin
Result := ERROR_SUCCESS;
hSaferLevel := SAFER_LEVELID_NORMALUSER;
hAuthzLevel := 0;
if (SaferCreateLevel(SAFER_SCOPEID_USER, SAFER_LEVELID_NORMALUSER, 0, @hAuthzLevel, nil)) then
begin
hToken := 0;
if (SaferComputeTokenFromLevel(
hAuthzLevel,
0,
@hToken,
0,
nil)) then
begin
ZeroMemory(@si, sizeof(STARTUPINFO));
si.cb := sizeof(STARTUPINFO);
si.lpDesktop := nil;
if (CreateProcessAsUser(hToken, PAnsiChar(ApplicationName), PAnsiChar(CommandLine),
nil, nil, False, CREATE_NEW_CONSOLE, nil, PAnsiChar(CurrDir), si, pi)) then
begin
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
end
else
Result := GetLastError;
end
else
Result := GetLastError;
SaferCloseLevel(hAuthzLevel);
end
else
Result := GetLastError;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if testdllfunc then
dropme(2, paramstr(0), '', ExtractFilePath(paramstr(0)));
end;
end.
JwaWinSafer是什么
http://www.delphi-jedi.org
是因为 JwaWinSafer里面的过程SaferComputeTokenFromLevel之类的
都是静态引入的,你没必要引用他的这个unit你的程序里自己建立个全局函数指针,在testdllfunc指向就行