type
TWinType = (wtWindows95, wtWindowsNT, wtWin32s, wtUnknown);
TForm1 = class(TForm)
Button1: TButton;
// Ne pas déclarer GetWinType ici, inutile
procedure Button1Click(Sender: TObject);
procedure ExitWindowsProc(ExitType: Cardinal);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetWinType: TWinType;
var
VersionInfo: TOSVersionInfo;
begin
Result := wtUnknown;
VersionInfo.dwOSVersionInfoSize := SizeOf(VersionInfo);
GetVersionEx(VersionInfo);
case VersionInfo.dwPlatformId of
VER_PLATFORM_WIN32S : Result := wtWin32s;
VER_PLATFORM_WIN32_WINDOWS : Result := wtWindows95;
VER_PLATFORM_WIN32_NT : Result := wtWindowsNT;
end;
end;
procedure ExitWindowsProc(ExitType: Cardinal);
var
TokenHandle: THandle;
NewState, PreviousState: TTokenPrivileges;
ReturnLength: DWORD;
begin
if GetWinType = wtWindowsNT then begin
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY, TokenHandle) then
RaiseLastWin32Error; // On fixe nos privilèges
try
NewState.PrivilegeCount := 1;
if not LookupPrivilegeValue(nil, 'SeshutdownPrivilege',
NewState.Privileges[0].LUID) then begin
RaiseLastWin32Error;
end;
NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
ReturnLength := 0;
if not AdjustTokenPrivileges(TokenHandle, False, NewState,
SizeOf(NewState), PreviousState, ReturnLength) then begin
RaiseLastWin32Error;
end;
finally
CloseHandle(TokenHandle);
if not ExitWindowsEx(ExitType, 0) then
RaiseLastWin32Error; // On ferme
end;
end
else begin
if not ExitWindowsEx(ExitType,0) then begin
RaiseLastWin32Error;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ExitWindowsProc(EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF);
end;
Voilà : si tu déclares GetWinType dans l'interface de ta fiche, il faut que l'implémentation soit précédée de l'instance de ta fiche (ici TForm1). Tu pourrais le faire, mais c'est inutile.
Bacterius
Messages postés3792Date d'inscriptionsamedi 22 décembre 2007StatutMembreDernière intervention 3 juin 201610 25 sept. 2008 à 17:39
procedure ExitWindowsProc(ExitType: Cardinal);
var
TokenHandle: THandle;
NewState, PreviousState: TTokenPrivileges;
ReturnLength: DWORD;
begin
if GetWinType = wtWindowsNT then begin
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY, TokenHandle) then
RaiseLastWin32Error; // On fixe nos privilèges
try
NewState.PrivilegeCount := 1;
if not LookupPrivilegeValue(nil, 'SeshutdownPrivilege',
NewState.Privileges[0].LUID) then begin
RaiseLastWin32Error;
end;
NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
ReturnLength := 0;
if not AdjustTokenPrivileges(TokenHandle, False, NewState,
SizeOf(NewState), PreviousState, ReturnLength) then begin
RaiseLastWin32Error;
end;
finally
CloseHandle(TokenHandle);
if not ExitWindowsEx(ExitType, 0) then
RaiseLastWin32Error; // On ferme
end;
end
else begin
if not ExitWindowsEx(ExitType,0) then begin
RaiseLastWin32Error;
end;
end;
end;
_____________________________________
Paramètres à passer dans ExitType :
Eteindre ordinateur : EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF
Bacterius
Messages postés3792Date d'inscriptionsamedi 22 décembre 2007StatutMembreDernière intervention 3 juin 201610 25 sept. 2008 à 17:48
oups petit problème ^^
Voilà la version corrigée
...
interface
type
TWinType = (wtWindows95, wtWindowsNT, wtWin32s, wtUnknown);
...
implementation
function GetWinType: TWinType; // Le type de Windows, mais plus simplement
var
VersionInfo: TOSVersionInfo;
begin
Result := wtUnknown;
VersionInfo.dwOSVersionInfoSize := SizeOf(VersionInfo);
GetVersionEx(VersionInfo);
case VersionInfo.dwPlatformId of
VER_PLATFORM_WIN32S : Result := wtWin32s;
VER_PLATFORM_WIN32_WINDOWS : Result := wtWindows95;
VER_PLATFORM_WIN32_NT : Result := wtWindowsNT;
end;
end;
procedure ExitWindowsProc(ExitType: Cardinal);
var
TokenHandle: THandle;
NewState, PreviousState: TTokenPrivileges;
ReturnLength: DWORD;
begin
if GetWinType = wtWindowsNT then begin
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY, TokenHandle) then
RaiseLastWin32Error; // On fixe nos privilèges
try
NewState.PrivilegeCount := 1;
if not LookupPrivilegeValue(nil, 'SeshutdownPrivilege',
NewState.Privileges[0].LUID) then begin
RaiseLastWin32Error;
end;
NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
ReturnLength := 0;
if not AdjustTokenPrivileges(TokenHandle, False, NewState,
SizeOf(NewState), PreviousState, ReturnLength) then begin
RaiseLastWin32Error;
end;
finally
CloseHandle(TokenHandle);
if not ExitWindowsEx(ExitType, 0) then
RaiseLastWin32Error; // On ferme
end;
end
else begin
if not ExitWindowsEx(ExitType,0) then begin
RaiseLastWin32Error;
end;
end;
end;
Warium
Messages postés36Date d'inscriptionjeudi 27 mars 2008StatutMembreDernière intervention27 janvier 2010 25 sept. 2008 à 18:01
Désolé bacterius esque SVP tu peux me corriger ça ::
Il me signal l'erreur suivante :
[Error] Unit1.pas(13): Unsatisfied forward or external declaration: 'TForm1.GetWinType'
type
TWinType = (wtWindows95, wtWindowsNT, wtWin32s, wtUnknown);
TForm1 = class(TForm)
Button1: TButton;
function GetWinType(): TWinType;
procedure Button1Click(Sender: TObject);
procedure ExitWindowsProc(ExitType: Cardinal);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetWinType(): TWinType; // Le type de Windows, mais plus simplement
var
VersionInfo: TOSVersionInfo;
begin
Result := wtUnknown;
VersionInfo.dwOSVersionInfoSize := SizeOf(VersionInfo);
GetVersionEx(VersionInfo);
case VersionInfo.dwPlatformId of
VER_PLATFORM_WIN32S : Result := wtWin32s;
VER_PLATFORM_WIN32_WINDOWS : Result := wtWindows95;
VER_PLATFORM_WIN32_NT : Result := wtWindowsNT;
end;
end;
procedure ExitWindowsProc(ExitType: Cardinal);
var
TokenHandle: THandle;
NewState, PreviousState: TTokenPrivileges;
ReturnLength: DWORD;
begin
if GetWinType = wtWindowsNT then begin
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY, TokenHandle) then
RaiseLastWin32Error; // On fixe nos privilèges
try
NewState.PrivilegeCount := 1;
if not LookupPrivilegeValue(nil, 'SeshutdownPrivilege',
NewState.Privileges[0].LUID) then begin
RaiseLastWin32Error;
end;
NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
ReturnLength := 0;
if not AdjustTokenPrivileges(TokenHandle, False, NewState,
SizeOf(NewState), PreviousState, ReturnLength) then begin
RaiseLastWin32Error;
end;
finally
CloseHandle(TokenHandle);
if not ExitWindowsEx(ExitType, 0) then
RaiseLastWin32Error; // On ferme
end;
end
else begin
if not ExitWindowsEx(ExitType,0) then begin
RaiseLastWin32Error;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ExitWindowsProc(EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF);
end;
type
TWinType = (wtWindows95, wtWindowsNT, wtWin32s, wtUnknown);
TForm1 = class(TForm)
Button1: TButton;
function GetWinType(): TWinType;
procedure Button1Click(Sender: TObject);
procedure ExitWindowsProc(ExitType: Cardinal);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetWinType: TWinType;
var
VersionInfo: TOSVersionInfo;
begin
Result := wtUnknown;
VersionInfo.dwOSVersionInfoSize := SizeOf(VersionInfo);
GetVersionEx(VersionInfo);
case VersionInfo.dwPlatformId of
VER_PLATFORM_WIN32S : Result := wtWin32s;
VER_PLATFORM_WIN32_WINDOWS : Result := wtWindows95;
VER_PLATFORM_WIN32_NT : Result := wtWindowsNT;
end;
end;
procedure ExitWindowsProc(ExitType: Cardinal);
var
TokenHandle: THandle;
NewState, PreviousState: TTokenPrivileges;
ReturnLength: DWORD;
begin
if GetWinType = wtWindowsNT then begin
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY, TokenHandle) then
RaiseLastWin32Error; // On fixe nos privilèges
try
NewState.PrivilegeCount := 1;
if not LookupPrivilegeValue(nil, 'SeshutdownPrivilege',
NewState.Privileges[0].LUID) then begin
RaiseLastWin32Error;
end;
NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
ReturnLength := 0;
if not AdjustTokenPrivileges(TokenHandle, False, NewState,
SizeOf(NewState), PreviousState, ReturnLength) then begin
RaiseLastWin32Error;
end;
finally
CloseHandle(TokenHandle);
if not ExitWindowsEx(ExitType, 0) then
RaiseLastWin32Error; // On ferme
end;
end
else begin
if not ExitWindowsEx(ExitType,0) then begin
RaiseLastWin32Error;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ExitWindowsProc(EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF);
end;
Nicolas___
Messages postés992Date d'inscriptionjeudi 2 novembre 2000StatutMembreDernière intervention24 avril 20131 26 sept. 2008 à 19:55
bof ...
je préfère de loin ma solution à une ligne que ta function ...
de plus il voulait comprendre ShutDown ben voila , c'est chose faire maintenant !
Bacterius
Messages postés3792Date d'inscriptionsamedi 22 décembre 2007StatutMembreDernière intervention 3 juin 201610 26 sept. 2008 à 21:01
Ouais mais ton truc c'est pas propre ... tu règles même pas les privilèges !
Imagine que les droits de l'utilisateur de ta "function" soient au niveau Esclave (le droit de se connecter et d'aller sur PriceMinister.com). Enfin de manière générale de ne pas pouvoir appeler cette commande ShutDown ! Erreur, pas le droit de privilèges ...
Evidemment 1 ligne = plus facile à comprendre, et à mettre en oeuvre.
Mais je préfère de loin avoir un gros code, qui soit fonctionnel, plutôt qu'un petit code de 2 ou 3 lignes, qui peut avoir des occasions de de foutre sur la gueule ^^
Mais nous n'allons pas en faire un débat philosophique, sinon on va basculer dans les grosses API et l'asm ... et j'ai pas envie ^^
[HORS SUJET]
Sinon, dans ton jeu space-shoot, je n'ai pas vu ma fonction pour créer du cosmos ... aurais-tu oublié de l'ajouter ? Ou n'as-tu pas eu envie ? :'(
En passant, il manque la DLL FMod ^^
SUJET
Nicolas___
Messages postés992Date d'inscriptionjeudi 2 novembre 2000StatutMembreDernière intervention24 avril 20131 26 sept. 2008 à 21:34
En passant, il manque la DLL FMod ^^
euh déso de te contredire mais elle y est !
J'ai pas encore mis a jour depuis le 15 donc ton cosmos n'est pas encore pris en compte ...
j'ai posté des commentaires par rapport aux jeux ...
A mon avis je ferais encore 1 maj et puis basta ... manque de temps , d'interêt et d'autre projet en vue ...
Explication de ma méthode "pas propre " de shutdown
Bonjour.
Je voudrais avec un simple clique sur un bouton, éteindre mon ordinateur.
j'ai vu dans l'aide une méthode appelée ShutDown mais ça ne marche pas ou je ne sais pas l'utiliser
Merci pour votre attention.
Voila , j'ai répondu à ça requête ... Ca ne sert à rien de sortir le bazooka quand la tapette est suffisante...
Si plus tard tu veux bosser dans l'info , je t'encourage à baisser ton enthousiasme ... il faut répondre a la demande, ni plus, ni moins !
En plus ici, il n'y a pas de rémunérations alors ...
the lower, the better !
Bacterius
Messages postés3792Date d'inscriptionsamedi 22 décembre 2007StatutMembreDernière intervention 3 juin 201610 26 sept. 2008 à 21:49
Je ne suis pas d'accord : "the lower the better" "pas de rémunérations". Justement : celui qui a du temps fournit une réponse plus structurée et tout.
Je suppose que celui qui utilise ta méthode, quand il s'apercevra que ça ne marche pas dans certains environnements, il se dira "mince alors, ou est la méthode de l'autre ?"
Alors bon, toi tu dois sûrement être grand, connaître le boulot d'un développeur mieux que moi ...
Je sais que sur un truc il faut répondre à la demande, mais c'est tout. Mais, si tu lui fournis une réponse incomplète (qui ne marchera pas dans toutes les conditions), tu ne réponds pas à la demande.
Et d'ailleurs, si tu regardes bien, il ne demandait pas une réponse simple.
Il voulait que son clic de bouton se fasse simplement (il voulait peut-être un fluidificateur de souris ^^) avec un shutdown en plus.
Bref, n'en parlons plus. Après tout, il prendra celle qu'il préfère (la tienne je pense).
Sinon, pour FMod, je te jure qu'elle n'y était pas ...
Nicolas___
Messages postés992Date d'inscriptionjeudi 2 novembre 2000StatutMembreDernière intervention24 avril 20131 26 sept. 2008 à 22:01
% a Fmod ; tu m'as mis un commentaire le 10/09/2008 19:34:35 que La Dll n'était pas présente et j'ai répondu
<dt>le 10 septembre 2008 19:46:55 : dll manquante ... T'as vu la réactivité ^^
%Au sujet : On va arrêter , tu ne comprends pas ... Et on dévie assez