Wednesday, 21 December 2011

Retrieve CPU brand by C and Win32 API

Retrieve CPU brand by C and Win32 API
This task use C and Win32 API to read CPU brand.
Read CPU brand using cpuid
Call GetCPUBrand method to retrieve CPU brand.
string GetCPUBrand() {
    char pszCPUBrand[49];  
    memset(pszCPUBrand, ' ', 49);  
     
    _asm {
        mov eax, 80000002h
        cpuid
  
        // getting information from EAX
  
        mov pszCPUBrand[0], al
        mov pszCPUBrand[1], ah
        ror eax, 16
        mov pszCPUBrand[2], al
        mov pszCPUBrand[3], ah
  
        // getting information from EBX
  
        mov pszCPUBrand[4], bl
        mov pszCPUBrand[5], bh
        ror ebx, 16
        mov pszCPUBrand[6], bl
        mov pszCPUBrand[7], bh
  
        // getting information from ECX
  
        mov pszCPUBrand[8], cl
        mov pszCPUBrand[9], ch
        ror ecx, 16
        mov pszCPUBrand[10], cl
        mov pszCPUBrand[11], ch
  
        // getting information from EDX
  
        mov pszCPUBrand[12], dl
        mov pszCPUBrand[13], dh
        ror edx, 16
        mov pszCPUBrand[14], dl
        mov pszCPUBrand[15], dh
  
        mov eax, 80000003h
        cpuid
  
        // getting information from EAX
  
        mov pszCPUBrand[16], al
        mov pszCPUBrand[17], ah
        ror eax, 16
        mov pszCPUBrand[18], al
        mov pszCPUBrand[19], ah
  
        // getting information from EBX
  
        mov pszCPUBrand[20], bl
        mov pszCPUBrand[21], bh
        ror ebx, 16
        mov pszCPUBrand[22], bl
        mov pszCPUBrand[23], bh
  
        // getting information from ECX
  
        mov pszCPUBrand[24], cl
        mov pszCPUBrand[25], ch
        ror ecx, 16
        mov pszCPUBrand[26], cl
        mov pszCPUBrand[27], ch
  
        // getting information from EDX
  
        mov pszCPUBrand[28], dl
        mov pszCPUBrand[29], dh
        ror edx, 16
        mov pszCPUBrand[30], dl
        mov pszCPUBrand[31], dh
  
        mov eax, 80000004h
        cpuid
  
        // getting information from EAX
  
        mov pszCPUBrand[32], al
        mov pszCPUBrand[33], ah
        ror eax, 16
        mov pszCPUBrand[34], al
        mov pszCPUBrand[35], ah
  
        // getting information from EBX
   
        mov pszCPUBrand[36], bl
        mov pszCPUBrand[37], bh
        ror ebx, 16
        mov pszCPUBrand[38], bl
        mov pszCPUBrand[39], bh
  
        // getting information from ECX
  
        mov pszCPUBrand[40], cl
        mov pszCPUBrand[41], ch
        ror ecx, 16
        mov pszCPUBrand[42], cl
        mov pszCPUBrand[43], ch
  
        // getting information from EDX
  
        mov pszCPUBrand[44], dl
        mov pszCPUBrand[45], dh
        ror edx, 16
        mov pszCPUBrand[46], dl
        mov pszCPUBrand[47], dh
  
    }
  
    pszCPUBrand[48] = '\0';
  
    return string(pszCPUBrand);
}
    

  Protected by Copyscape Online Copyright Protection

Execute thread from java with timeout

Execute thread from java with timeout
This task use java to run thread. After period of time, if thread does not end normally, it is killed.
Execute thread using ExecutorService
Create task class which implements Callable. Call executeThreadWithTimeout with instance of task class and timeout number.
private class Task implements Callable {
    public Integer call() throws Exception {
        try {
             // Task go here
        } catch (Exception e) {
        }
        return 0;
    }
}

private void executeThreadWithTimeout(Callable task, int timeout) {
    try {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.invokeAll(Arrays.asList(task), timeout, TimeUnit.SECONDS);
        executor.shutdown();
    } catch (Exception e) {
    }
}
    

  Protected by Copyscape Online Copyright Protection

Execute command line process from java with timeout

Execute command line process from java with timeout
This task use java to run command line process. After period of time, if process does not end normally, it is killed.
Execute command line using CallableProcess
Call executeCommand method with try ... catch. If exception is thrown, process does not start or end normally.
public static void executeCommand(String command, long timeoutInSeconds) throws Exception {
    ExecutorService service = Executors.newSingleThreadExecutor();
    Process process = Runtime.getRuntime().exec(command);
    try {
        Callable<Integer> call = new CallableProcess(process);
        Future<Integer> future = service.submit(call);
        int exitValue = future.get(timeoutInSeconds, TimeUnit.SECONDS);
        if (exitValue != 0) {
            throw new Exception("Process did not exit correctly");
        }
    } catch (ExecutionException e) {
        throw new Exception("Process failed to execute", e);
    } catch (TimeoutException e) {
        process.destroy();
        throw new Exception("Process timed out", e);
    } finally {
        service.shutdown();
    }
}
 
private static class CallableProcess implements Callable {
    private Process p;

    public CallableProcess(Process process) {
        p = process;
    }

    public Integer call() throws Exception {
        return p.waitFor();
    }
}
    

  Protected by Copyscape Online Copyright Protection

Check Salesforce connection by java

Check Salesforce connection by java
This task use java to check if Salesforce account (includes username, password, security token) is valid or not.
Check by logging in and logging out
Call check method with try ... catch. If exception is thrown, account is not valid.
 public void check(String username, String password, String token) throws Exception {
        ConnectorConfig cc = new ConnectorConfig();
        URL loginUrl = new URL(Connector.END_POINT);
        String serverUrl = "https://login.salesforce.com";
        cc.setAuthEndpoint(serverUrl + loginUrl.getPath());
        cc.setServiceEndpoint(serverUrl + loginUrl.getPath());
        cc.setManualLogin(true);
        PartnerConnection partner = Connector.newConnection(cc);
  
        LoginResult loginResult = partner.login(username, password + token);
        if (loginResult.getPasswordExpired()) {
         partner = null;
         throw new Exception("User has expired password");
        }
        partner.setSessionHeader(loginResult.getSessionId());
        cc.setServiceEndpoint(loginResult.getServerUrl());
        partner.logout();
 }
    

  Protected by Copyscape Online Copyright Protection

Check Lotus Notes connection by java

Check Lotus Notes connection by java
This task use java to check if Lotus Notes account (includes user id file, password) is valid or not.
Check by creating session
Call check method with try ... catch. If exception is thrown, account is not valid.
public void check(String userid, String password) throws Exception {
    NotesThread.sinitThread();
    Session s = NotesFactory.createSessionWithFullAccess();
    s.createRegistration().switchToID(userid, password);
    s.recycle();
    s = NotesFactory.createSessionWithFullAccess(password);
    s.recycle();
}
    

  Protected by Copyscape Online Copyright Protection