Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: WMI code giving error on Win8


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Oct 21, 2016 1:19 PM Last Post By: Remy Lebeau (Te...
Yogi Yang

Posts: 57
Registered: 12/23/06
WMI code giving error on Win8  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 15, 2016 1:24 AM
Hello,

I am using following code to extract Motherboard information from a PC.

I am using XE6. This code works well under Win7 as expected. Even in debug mode in the IDE. But if I run it in debug mode in the XE6 IDE under Win8 it gives error.
function GetWMIObject(const objectName: string): IDispatch;
  var
    chEaten: Integer;
    BindCtx: IBindCtx;
    Moniker: IMoniker;
  begin
    asm DB $EB,$04,$0F,$0B,$0F,$0B;
    end; // Start Virtualization
    OleCheck(CreateBindCtx(0, BindCtx));
    OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten,
      Moniker));  //<==Error raised
    OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
    asm DB $EB,$04,$0F,$0B,$0F,$06;
    end; // End Virtualization
  end;


How to solve this error?

TIA

Yogi Yang
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: WMI code giving error on Win8  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 15, 2016 11:21 AM   in response to: Yogi Yang in response to: Yogi Yang
Am 15.10.2016 um 10:24 schrieb Yogi Yang:
Hello,

I am using following code to extract Motherboard information from a PC.

I am using XE6. This code works well under Win7 as expected. Even in debug mode in the IDE. But if I run it in debug mode in the XE6 IDE under Win8 it gives error.
function GetWMIObject(const objectName: string): IDispatch;
  var
    chEaten: Integer;
    BindCtx: IBindCtx;
    Moniker: IMoniker;
  begin
    asm DB $EB,$04,$0F,$0B,$0F,$0B;
    end; // Start Virtualization
    OleCheck(CreateBindCtx(0, BindCtx));
    OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten,
      Moniker));  //<==Error raised
    OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
    asm DB $EB,$04,$0F,$0B,$0F,$06;
    end; // End Virtualization
  end;


How to solve this error?

TIA

Yogi Yang

Hello,

which error does it give?
The right group for this question would also be
embarcadero.public.delphi.nativeapi

Greetings

Markus

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: WMI code giving error on Win8  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 17, 2016 4:51 PM   in response to: Yogi Yang in response to: Yogi Yang
Yogi wrote:

I am using XE6. This code works well under Win7 as expected. Even in
debug mode in the IDE. But if I run it in debug mode in the XE6 IDE
under Win8 it gives error.

What error exactly? What object name are you requesting? Please be more specific.

Why are you using inline assembly? What is that "virtualization" doing exactly?

Also, your use of StringToOleStr() is leaking memory. It returns a dynamically
allocated BSTR that must be freed with SysFreeString(). Use WideString instead,
let it automate that for you:

OleCheck(MkParseDisplayName(BindCtx, PWideChar(WideString(objectName)), chEaten, 
Moniker));


--
Remy Lebeau (TeamB)
Yogi Yang

Posts: 57
Registered: 12/23/06
Re: WMI code giving error on Win8  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 20, 2016 10:54 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Yogi wrote:

I am using XE6. This code works well under Win7 as expected. Even in
debug mode in the IDE. But if I run it in debug mode in the XE6 IDE
under Win8 it gives error.

What error exactly? What object name are you requesting? Please be more specific.

Why are you using inline assembly? What is that "virtualization" doing exactly?

Also, your use of StringToOleStr() is leaking memory. It returns a dynamically
allocated BSTR that must be freed with SysFreeString(). Use WideString instead,
let it automate that for you:

OleCheck(MkParseDisplayName(BindCtx, PWideChar(WideString(objectName)), chEaten, 
Moniker));

Thanks for the code sample. But this also gives error.

Here is the link for the [image|http://imagebucket.net/3ua39fs6g7jv/Error_ScreenShot.jpg] .

I also tried the changes as per your suggestion but that also did not work.

Here is the full code for accessing Motherboard information using WMI:
function TMainForm.GetMotherBoardInfo(): string;
var
  MotherBoardSerial, FinalString: String;
  objWMIService: OLEVariant;
  colItems: OLEVariant;
  colItem: OLEVariant;
  oEnum: IEnumvariant;
  iValue: LongWord;
  MyGuid: TGUID;
  i, CharValue: Integer;
  function GetWMIObject(const objectName: string): IDispatch;
  var
    chEaten: Integer;
    BindCtx: IBindCtx;
    Moniker: IMoniker;
  begin
    OleCheck(CreateBindCtx(0, BindCtx));
    OleCheck(MkParseDisplayName(BindCtx, PWideChar(WideString(objectName)), chEaten, Moniker));
    OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
  end;
 
begin
  // Code to read MotherBoard Details
  objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
  colItems := objWMIService.ExecQuery
    ('SELECT SerialNumber FROM Win32_BaseBoard', 'WQL', 0);
  oEnum := IUnknown(colItems._NewEnum) as IEnumvariant;
  if oEnum.Next(1, colItem, iValue) = 0 then
    MotherBoardSerial := VarToStr(colItem.serialNumber);
  if Length(Trim(MotherBoardSerial)) < 5 then
  begin
    MotherBoardSerial := MotherBoardSerial + 'F08ED98806BAD2';
  end;
 
  if Length(MotherBoardSerial) > 14 then
  begin
    MotherBoardSerial := MidStr(MotherBoardSerial, 1, 14);
  end;
 
  if Length(MotherBoardSerial) < 14 then
  begin
    case Length(MotherBoardSerial) of
      1:
        MotherBoardSerial := MotherBoardSerial + '08ED98806BAD2';
      2:
        MotherBoardSerial := MotherBoardSerial + '8ED98806BAD2';
      3:
        MotherBoardSerial := MotherBoardSerial + '8ED98806BA2';
      4:
        MotherBoardSerial := MotherBoardSerial + '8ED98806BA';
      5:
        MotherBoardSerial := MotherBoardSerial + '8ED98806A';
      6:
        MotherBoardSerial := MotherBoardSerial + '8ED98806';
      7:
        MotherBoardSerial := MotherBoardSerial + '8ED9880';
      8:
        MotherBoardSerial := MotherBoardSerial + 'ED9880';
      9:
        MotherBoardSerial := MotherBoardSerial + 'D9880';
      10:
        MotherBoardSerial := MotherBoardSerial + '9880';
      11:
        MotherBoardSerial := MotherBoardSerial + '980';
      12:
        MotherBoardSerial := MotherBoardSerial + '98';
      13:
        MotherBoardSerial := MotherBoardSerial + '8';
      14:
        MotherBoardSerial := MotherBoardSerial + '';
    end; // case Length(MotherBoardSerial) of
  end; // if Length(MotherBoardSerial) < 14 then
  FinalString := '';
  for i := 1 to Length(MotherBoardSerial) do
  begin
    CharValue := Ord(MotherBoardSerial[i]);
    if ((CharValue >= 48) AND (CharValue <= 57)) OR
      ((CharValue >= 65) AND (CharValue <= 90)) OR
      ((CharValue >= 97) AND (CharValue <= 122)) then
    begin
      FinalString := FinalString + MidStr(MotherBoardSerial, i, 1);
    end
    else
    begin
      FinalString := FinalString + '-';
    end;
  end;
  Result := UpperCase(FinalString);
end;


TIA

Yogi Yang
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: WMI code giving error on Win8  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2016 1:19 PM   in response to: Yogi Yang in response to: Yogi Yang
Yogi wrote:

Thanks for the code sample. But this also gives error.

Here is the link for the

[image|http://imagebucket.net/3ua39fs6g7jv/Error_ScreenShot.jpg] .

There is nothing in that line of code that can be causing that kind of error.
Are you sure it is not on another line?

Here is the full code for accessing Motherboard information using WMI:

That code works fine as-is for me. It does not crash.

--
Remy Lebeau (TeamB)
Angus Robertson

Posts: 205
Registered: 3/17/00
Re: WMI code giving error on Win8  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2016 12:33 AM   in response to: Yogi Yang in response to: Yogi Yang
I am using following code to extract Motherboard information from
a PC.

You could just use the free Magenta Systems WMI and SMART Component
instead, which is very reliable, and supports Delphi 7 and Windows XP
and later. Works fine on Windows 10 and Berlin.

https://www.magsys.co.uk/delphi/magwmi.asp

Angus
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02