SetLocalTime() Falscher Parameter 87



  • #include <windows.h>
    #include <iostream>
    
    #define PRIVILEGE 1
    using namespace std;
    
    BOOL SetPrivilege(
        HANDLE,         // access token handle
        LPCTSTR,		// name of privilege to enable/disable
        BOOL			// to enable or disable privilege
        );
    
    int main()
    {
    	HANDLE hToken;
    	OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
    	SetPrivilege(hToken,SE_SYSTEMTIME_NAME, PRIVILEGE);	//Privileg zum ändern der Lokalzeit erlangen
    	SYSTEMTIME tm;
    	tm.wHour = 1; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    	SetLocalTime(&tm);	// ERROR 87: ERROR_INVALID_PARAMETER Falscher Parameter. Wieso falscher Parameter?
    	cout << GetLastError() << endl;
    	GetLocalTime(&tm);
    	cout << tm.wHour << ":" << tm.wMinute << ":" << tm.wSecond << "." << tm.wMilliseconds << endl;
    	system("pause");
    }
    
    BOOL SetPrivilege(
        HANDLE hToken,          // access token handle
        LPCTSTR lpszPrivilege,  // name of privilege to enable/disable
        BOOL bEnablePrivilege   // to enable or disable privilege
        )
    {
    	TOKEN_PRIVILEGES tp;
    	LUID luid;
    	if ( !LookupPrivilegeValue( 
    			NULL,            // lookup privilege on local system
    			lpszPrivilege,   // privilege to lookup 
    			&luid ) )        // receives LUID of privilege
    	{
    		printf("LookupPrivilegeValue error: %u\n", GetLastError() ); 
    		return FALSE; 
    	}
    
    	tp.PrivilegeCount = 1;
    	tp.Privileges[0].Luid = luid;
    	if (bEnablePrivilege)
    		tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    	else
    		tp.Privileges[0].Attributes = 0;
    
    	// Enable the privilege or disable all privileges.
    
    	if ( !AdjustTokenPrivileges(
    		   hToken, 
    		   FALSE, 
    		   &tp, 
    		   sizeof(TOKEN_PRIVILEGES), 
    		   (PTOKEN_PRIVILEGES) NULL, 
    		   (PDWORD) NULL) )
    	{ 
    		  printf("AdjustTokenPrivileges error: %u\n", GetLastError() ); 
    		  return FALSE; 
    	} 
    
    	if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
    
    	{
    		  printf("The token does not have the specified privilege. \n");
    		  return FALSE;
    	} 
    
    	return TRUE;
    }
    

    Möchte die Lokalzeit ändern. Geht nicht. Wieso?



  • Hallo,

    du stellst nur sicher, dass wHour der SYSTEMTIME-Struktur einen vernünftigen Wert hat, die anderen ignorierst du (die SYSTEMTIME-Struktur tm enthält sonst zufällige Werte!) , z.B. muss wMonth zwischen 1 und 12 liegen, usw...

    MfG,

    Probe-Nutzer



  • Probe-Nutzer schrieb:

    Hallo,

    du stellst nur sicher, dass wHour der SYSTEMTIME-Struktur einen vernünftigen Wert hat, die anderen ignorierst du (die SYSTEMTIME-Struktur tm enthält sonst zufällige Werte!) , z.B. muss wMonth zwischen 1 und 12 liegen, usw...

    MfG,

    Probe-Nutzer

    Danke, das hat mir geholfen.


Anmelden zum Antworten