Detect bitlocker partition state using delphi

575 views Asked by At

I enqrypted one of my partitios with bitlocker before and it works perfect. For now how can i detect if this partition is open or not? I mean the partition is locked or not?

2

There are 2 answers

1
RBA On

As I can not test now the following code, you could give it a try:

program WmiTest;

{$APPTYPE CONSOLE}


uses
  SysUtils
  ,ActiveX
  ,ComObj
  ,Variants;


function GetWMIstring(wmiHost, root, wmiClass, wmiProperty: string): string;
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;

  function GetWMIObject(const objectName: String): IDispatch;
  var
    chEaten: Integer;
    BindCtx: IBindCtx;//for access to a bind context
    Moniker: IMoniker;//Enables you to use a moniker object
  begin
    OleCheck(CreateBindCtx(0, bindCtx));
    OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));//Converts a string into a moniker that identifies the object named by the string
    OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));//Binds to the specified object
  end;

begin
  objWMIService := GetWMIObject(Format('winmgmts:\\%s\%s',[wmiHost,root]));
  colItems      := objWMIService.ExecQuery(Format('SELECT * FROM %s',[wmiClass]),'WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;
  while oEnum.Next(1, colItem, iValue) = 0 do
  begin
     Result:=colItem.Properties_.Item(wmiProperty, 0);
  end;
end;

begin
 try
    CoInitialize(nil);
    try
      WriteLn(GetWMIstring('.', 'Root\CIMV2\Security\MicrosoftVolumeEncryption', 'Win32_EncryptableVolume','LockStatus'));
      Readln;
    finally
    CoUninitialize;
    end;
 except
    on E:Exception do
    Begin
        Writeln(E.Classname, ': ', E.Message);
        Readln;
    End;
  end;
end.

based on RRUZ's answer from here https://stackoverflow.com/a/2762023/368364 and on the query provided by Norman Bauer here https://www.normanbauer.com/2010/09/28/how-to-get-some-information-on-bitlocker-using-vbscript-and-wmi/

1
Angelica On

If you want to find Encryption State than you can use GetEncryptionMethod

GetEncryptionMethod

uint32 GetEncryptionMethod(
  [out] uint32 EncryptionMethod,
  [out] string SelfEncryptionDriveEncryptionMethod
);

If EncryptionMethod is 0 then The volume is not encrypted else encrypted.