Page 1 of 1

am i crazy ???

Posted: Fri Dec 02, 2016 7:20 pm
by supercdfr
here is a code to get the manufacturer of my pc :

Code: Select all

DataSection
  CLSID_WbemAdministrativeLocator:
  Data.l  $CB8555CC
  Data.w  $9128
  Data.w  $11D1
  Data.b $AD, $9B, $0, $C0, $4F, $D8, $FD, $FF
  IID_IWbemLocator:
  Data.l  $DC12A687
  Data.w  $737F
  Data.w  $11CF
  Data.b $88, $4D, $0, $AA, $0, $4B, $2E, $24
EndDataSection

Procedure.s modele()
  #CLSCTX_INPROC_SERVER = $1
  #WBEM_FLAG_RETURN_IMMEDIATELY = $10
  #WBEM_FLAG_FORWARD_ONLY = $20
  #RPC_C_IMP_LEVEL_IMPERSONATE = 3
  #RPC_C_AUTHN_LEVEL_DEFAULT = 0
  
  If CoInitializeEx_(0, #COINIT_MULTITHREADED) = #S_OK
    If CoInitializeSecurity_(0, -1, 0, 0, #RPC_C_AUTHN_LEVEL_DEFAULT, #RPC_C_IMP_LEVEL_IMPERSONATE, 0, 0, 0) = #S_OK
      pLocator.IWbemLocator
      If CoCreateInstance_(?CLSID_WbemAdministrativeLocator, 0, #CLSCTX_INPROC_SERVER, ?IID_IWbemLocator, @pLocator) = #S_OK
        pServices.IWbemServices
        If pLocator\ConnectServer(@"root\cimv2", 0, 0, 0, 0, 0, 0, @pServices) = #S_OK
          pEnum.IEnumWbemClassObject
          If pServices\ExecQuery(@"WQL", @"SELECT * FROM Win32_ComputerSystem", #WBEM_FLAG_RETURN_IMMEDIATELY | #WBEM_FLAG_FORWARD_ONLY, 0, @pEnum) = #S_OK
            pObject.IWbemClassObject
            v.VARIANT
            returned.l
            If pEnum\Next(#WBEM_INFINITE, 1, @pObject, @returned) = #S_OK
              VariantInit_(@v)
              If pObject\Get(@"model", 0, @v, 0, 0) = #S_OK
                retour$ +  PeekS(v\bstrVal)
                VariantClear_(@v)
              EndIf            
              pObject\Release()
            EndIf 
            pEnum\Release()
          EndIf 
          pServices\Release()
        EndIf
        pLocator\Release()
      EndIf 
    EndIf 
    CoUninitialize_()
  EndIf
  ProcedureReturn retour$
EndProcedure


Debug modele()


End

If InitNetwork() = 0
  MessageRequester("Error", "Impossible d'initialiser le reseau !")
EndIf
When i try, i obtain NOTHING.
When i remove the messagerequeter, it works :?: :?: :?:

How is it possible ?

Re: am i crazy ???

Posted: Fri Dec 02, 2016 7:45 pm
by heartbone
am i crazy ???
You are sane.
How is it possible ?
Good question.
My answer is PureBasic.

Re: am i crazy ???

Posted: Fri Dec 02, 2016 8:04 pm
by fryquez
You not crazy :D

Your call to CoInitializeEx_() returns #RPC_E_CHANGED_MODE

replace it with CoInitialize_(0)

Code: Select all

DataSection
  CLSID_WbemAdministrativeLocator:
  Data.l  $CB8555CC
  Data.w  $9128
  Data.w  $11D1
  Data.b $AD, $9B, $0, $C0, $4F, $D8, $FD, $FF
  IID_IWbemLocator:
  Data.l  $DC12A687
  Data.w  $737F
  Data.w  $11CF
  Data.b $88, $4D, $0, $AA, $0, $4B, $2E, $24
EndDataSection

Macro SUCCEEDED(HRESULT)
  HRESULT & $80000000 = 0
EndMacro

Macro FAILED(HRESULT)
  HRESULT & $80000000
EndMacro

Procedure.s modele()
  #CLSCTX_INPROC_SERVER = $1
  #WBEM_FLAG_RETURN_IMMEDIATELY = $10
  #WBEM_FLAG_FORWARD_ONLY = $20
  #RPC_C_IMP_LEVEL_IMPERSONATE = 3
  #RPC_C_AUTHN_LEVEL_DEFAULT = 0
  
  
  If SUCCEEDED(CoInitialize_(0))
    
    If SUCCEEDED(CoInitializeSecurity_(0, -1, 0, 0, #RPC_C_AUTHN_LEVEL_DEFAULT, #RPC_C_IMP_LEVEL_IMPERSONATE, 0, 0, 0))
      pLocator.IWbemLocator
      
      If SUCCEEDED(CoCreateInstance_(?CLSID_WbemAdministrativeLocator, 0, #CLSCTX_INPROC_SERVER, ?IID_IWbemLocator, @pLocator))
        pServices.IWbemServices
        If pLocator\ConnectServer(@"root\cimv2", 0, 0, 0, 0, 0, 0, @pServices) = #S_OK
          pEnum.IEnumWbemClassObject
          If pServices\ExecQuery(@"WQL", @"SELECT * FROM Win32_ComputerSystem", #WBEM_FLAG_RETURN_IMMEDIATELY | #WBEM_FLAG_FORWARD_ONLY, 0, @pEnum) = #S_OK
            pObject.IWbemClassObject
            v.VARIANT
            returned.l
            If pEnum\Next(#WBEM_INFINITE, 1, @pObject, @returned) = #S_OK
              VariantInit_(@v)
              If pObject\Get(@"model", 0, @v, 0, 0) = #S_OK
                retour$ +  PeekS(v\bstrVal)
                VariantClear_(@v)
              EndIf            
              pObject\Release()
            EndIf 
            pEnum\Release()
          EndIf 
          pServices\Release()
        EndIf
        pLocator\Release()
      EndIf 
    EndIf 
    CoUninitialize_()
  EndIf
  ProcedureReturn retour$
EndProcedure


Debug modele()


End

If InitNetwork() = 0
  MessageRequester("Error", "Impossible d'initialiser le reseau !")
EndIf

Re: am i crazy ???

Posted: Fri Dec 02, 2016 9:35 pm
by Mistrel
It output the model of my motherboard. That's cool.

Re: am i crazy ???

Posted: Sat Dec 03, 2016 6:24 am
by chi
hmm, on my machine the output is: "System Product Name"

Re: am i crazy ???

Posted: Sat Dec 03, 2016 7:22 am
by nco2k
http://purebasic.fr/english/viewtopic.php?f=12&t=64822

Code: Select all

Debug WMI("SELECT Manufacturer, Model FROM Win32_ComputerSystem")
as chi pointed out however, not everyone fills out those informations. usually only OEMs do.

to answer your question, MessageRequester() uses CoInitialize_(). when you use a different mode, the function returns #RPC_E_CHANGED_MODE. so you have to either use the same mode or ignore #RPC_E_CHANGED_MODE. if you ignore it, make sure to NOT call CoUninitialize_(). i overlooked that as well and updated my code accordingly.

c ya,
nco2k

Re: am i crazy ???

Posted: Tue Dec 06, 2016 6:20 pm
by supercdfr
i just try your code with this at the end

Code: Select all

;List only the "Caption" and "OSArchitecture" Properties from the "Win32_OperatingSystem" Class
Debug WMI("SELECT Caption, OSArchitecture FROM Win32_OperatingSystem")
Debug "****"
;List all Properties (*) from the "Win32_OperatingSystem" Class
Debug WMI("Select * FROM Win32_OperatingSystem")

MessageRequester("","")
and it doesn't work netheir.

Re: am i crazy ???

Posted: Tue Dec 06, 2016 6:26 pm
by supercdfr
find a solution with this :

Code: Select all

    CoUninitialize_()
    CoInit = CoInitializeEx_(0, #COINIT_APARTMENTTHREADED | #COINIT_DISABLE_OLE1DDE)
it works like that, but may be not the good way to do it.

Re: am i crazy ???

Posted: Tue Dec 06, 2016 11:41 pm
by supercdfr
in fact, it works for my program the first time, not 2-3 times consecutivly.

Anyone know if there is another way to have acces wmi in PB ?

Re: am i crazy ???

Posted: Wed Dec 07, 2016 8:39 am
by nco2k
it looks like the requester lib is calling CoInitialize_() on program start and not on the function call. that would explain why the CoUninitialize_() "workaround" works.

if you use your own message requester, then it works:

Code: Select all

Procedure MessageBox(WindowID, Title$, Text$, Flags=0)
  Protected Result, CoInit
  CoInit = CoInitializeEx_(0, #COINIT_APARTMENTTHREADED | #COINIT_DISABLE_OLE1DDE)
  If CoInit = #S_OK Or CoInit = #S_FALSE Or CoInit = #RPC_E_CHANGED_MODE
    Result = MessageBox_(WindowID, Text$, Title$, Flags)
    If CoInit <> #RPC_E_CHANGED_MODE
      CoUninitialize_()
    EndIf
  EndIf
  ProcedureReturn Result
EndProcedure
i wonder what other pb libraries are calling CoInitialize_() on program start. :?:

c ya,
nco2k