am i crazy ???

Just starting out? Need help? Post your questions and find answers here.
supercdfr
User
User
Posts: 54
Joined: Tue Mar 16, 2010 9:28 pm

am i crazy ???

Post 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 ?
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: am i crazy ???

Post by heartbone »

am i crazy ???
You are sane.
How is it possible ?
Good question.
My answer is PureBasic.
Keep it BASIC.
fryquez
Enthusiast
Enthusiast
Posts: 391
Joined: Mon Dec 21, 2015 8:12 pm

Re: am i crazy ???

Post 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
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: am i crazy ???

Post by Mistrel »

It output the model of my motherboard. That's cool.
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: am i crazy ???

Post by chi »

hmm, on my machine the output is: "System Product Name"
Et cetera is my worst enemy
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: am i crazy ???

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
supercdfr
User
User
Posts: 54
Joined: Tue Mar 16, 2010 9:28 pm

Re: am i crazy ???

Post 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.
supercdfr
User
User
Posts: 54
Joined: Tue Mar 16, 2010 9:28 pm

Re: am i crazy ???

Post 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.
supercdfr
User
User
Posts: 54
Joined: Tue Mar 16, 2010 9:28 pm

Re: am i crazy ???

Post 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 ?
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: am i crazy ???

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Post Reply