Showing posts with label Windows. Show all posts
Showing posts with label Windows. Show all posts

Sunday, July 20, 2014

Memory management of BSTR objects in COM

When working with BSTR with COM objects, the following questions have to be answered:
1. When passing BSTR to a COM object, who is responsible to free the memory allocated to BSTR?
2. When a COM object returns BSTR, who is responsible to free the memory allocated to BSTR?

Answer to both these questions is: caller of the COM object is responsible for freeing memory allocated to BSTR.

Examples:
1. When passing BSTR to COM object
Solution:
    {
            CComPtr<IComDog> dog;
            dog.CoCreateInstance(Animals::CLSID_Dog);
            BSTR nameOfDog = SysAllocString(L"Tiger");
            dog->put_NameOfDog(nameOfDog);
            .....
            // other code for dogs
            .....
            SysFreeString(nameOfDog);
            nameOfDog = NULL;
    }

Instead of using BSTR directly, use CComBSTR which takes care of memory management.
    {
            CComPtr<IComDog> dog;
            dog.CoCreateInstance(Animals::CLSID_Dog);
            CComBSTR nameOfDog = L"Tiger";
            dog->put_Name(nameOfDog);
            .....
            // other code for dogs
            // free'ing memory allocated to nameOfDog is taken care of by CComPtr
     }

2. When COM return a BSTR
     TBD

Wednesday, July 16, 2014

PowerShell Script to find Process IDs of an Executable

To find the priority of Threads of a process, copy the following script and execute in PowerShell

$timeoutInSeconds = 100;
$processName = "myExecutable.exe"

$processList = Get-WmiObject win32_process | where { $_.ProcessName -eq "$processName" } | select Handle
foreach ($processIds in $processList)
{
    $processId = $processIds.Handle
    Write-Host "process id = $processId"
}


Disclaimer: Use at your own risk. The author does not take any responsibility for the behavior of this script.

Lock Windows Desktop during Remote Session

In order to lock a Windows desktop during remote session, execute the following command

rundll32.exe user32.dll,LockWorkStation

Common WinDbg commands

How to set Symbol path
srv*c:\symbols*http://msdl.microsoft.com/download/symbols;
srv*c:\symbols*c:\temp\localsymbols;
srv*c:\symbols*\\network_share\symbols;

or
set environment variable name: _NT_SYMBOL_PATH
set environment variable value: srv*c:\symbols*http://msdl.microsoft.com/download/symbols;
                     srv*c:\symbols*c:\temp\localsymbols; srv*c:\symbols*\\network_share\symbols;


How to load Wow64 on x64 debugger
.load wow64exts

Other commands
lml
.ecxr
!analyze -v

How to verify if the module and pdb are a match
!itoldyouso <ModuleName> <ModulePDB>

Setup WinDbg as Postmortem Debugger
windbg -I

Setup ProcMon as Postmortem Debugger
procmon -i -ma

Redirect output to a log file
000> .logopen c:\temp\hello.txt
000> <do what ever commands> - all output is shown in windbg window and redirected to log file
000> .logclose

Find machine name (.NET dmp)
kd> x srv!SrvComputerName
fffff880`06ec4540 srv!SrvComputerName = <no type information>
kd> dq fffff880`06ec4540
fffff880`06ec4540  00000000`001a001a fffff8a0`02441df0
fffff880`06ec4550  00000000`00000000 00000000`00000000
fffff880`06ec4560  00000000`00000000 00000000`000c000a
...
kd> du fffff8a0`02441df0
fffff8a0`02441df0  "ABC"


Read GUID from a PDB using WinDbg

Here are the steps for reading GUID of a PDB

1. Install WinDbg
2. Set symbol path to the location of PDB
3. Load any executable into WinDbg
4. Execute in WinDbg 'ld <ModuleName>'
5. Execute in WinDbg '!chksym <ModuleName>
    c:\temp\<name_of_module>.dll
    Timestamp: 53FEA125
    SizeOfImage: DE000
    pdb: c:\temp\sample.pdb
    pdb sig: aaaaaaaa-bbbb-bbbb-bbbb-aaaaaaaaaaaa
    age: 1

Kill Remote Process using PowerShell

Here are the steps to kill a process running on remote machine

1. Start PowerShell
2. $processes = Get-WmiObject -class win32_process -ComputerName <name_of_computer> -Filter "Name='notepad.exe'"
3. $x = $processes.terminate()
4. echo $x

Return value should be 0

Friday, September 13, 2013

Renew IP Address

When computers are moved from one network to another network, its IP address changes. This change is not reflected immediately in network adapters. In order to force a reset of these adapters, the following commands are used.
Renew IP Address

Windows
1. Open a cmd prompt in elevated mode
2. Execute the command
    c:\windows\system32> ipconfig /renew


Linux
1. Open a Terminal as root
2. Execute the command
    $ ifconfig eth0 down
    $ ifconfig eth0 up

Saturday, August 10, 2013

Programs for Windows Administration

This is a list of programs, that you can use for Windows administration.

  1. Computer Management: compmgmt.msc
  2. Component Services: comexp.msc 
  3. Event Viewer: eventvwr.exe 
  4. Local Security Policy: secpol.msc 
  5. ODBC Data Sources: odbcad32.exe
  6. Performance Monitor: perfmon.msc
  7. Print Management: printmanagement.msc
  8. Services: services.msc
  9. System Configuration: msconfig.exe 
  10. Task Scheduler:taskschd.msc
  11. Windows Firewall with Advanced Security: WF.msc 

Common Tools for Regular Use

I use a variety of tools during development. Here is a list of some of them. This list keeps growing as I use additional software.

  1. Beyond Compare: for file, folder comparison and file merge
  2. gVim: for file editing, comparison and merge
  3. WinMerge: file comparison and merge
  4. Application Verifier: runtime verification of c, c++ code on Windows
  5. Driver Verifier: Windows kernel mode driver runtime verifier
  6. WinDbg: debugging C and C++ code on Windows
  7. WireShark: network packet analysis and sniffing
  8. Maven: build, package
  9. Gradle: build, package, deploy
  10. Selenium: web site testing
  11. JUnit: framework for unit testing of Java code
  12. Fortify SCA: source code analysis
  13. Clover: code coverage for Java and Groovy code
  14. SonarQube: for trend analysis of various aspects in a project
  15. BlackDuck: open source software management
  16. VMWare Player: free virtual machine software
  17. psftp: sftp client
  18. putty: telnet client
  19. pscp: scp client
  20. tomcat: web server
  21. process explorer: Windows processes, open files, handles etc
  22. dependency finder: for finding dependencies in java code
  23. lattix: for finding dependencies in java/c/c++/.net/database code

Disclaimer: This is just a list of software that I used. I do not make any recommendations regarding the software that I listed here.

Saturday, July 20, 2013

Windows - Auto Logon

In order to perform auto-logon in Windows
1. Create a batch file with the following content

set autologon_username=
set autologon_password=
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /f /v AutoAdminLogon /t REG_SZ /d 1
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /f /v DefaultUserName /t REG_SZ /d %autologon_username%
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /f /v DefaultUserName /t REG_SZ /d %autologon_password%

2. Execute the batch file in an elevated mode
On rebooting the machine, you should be able to logon automatically to the user account specified with .
Note:Revealing password in plain text is a bad idea. Use this feature cautiously because you are exposing the plain text password.

Windows - Reboot from cmd

In order to reboot from the cmd prompt in Windows, execute the following command. Make sure you saved all open files before executing this command.

shutdown -r