Hi,
I have this code that I am trying to compile, but I am getting the
following errors

'CoInitializeSecurity': identifier not found
'EOAC_NONE' : undeclared identifier

I have pasted the code below:



#include "stdafx.h"
#include "wbemidl.h"
#include <comdef.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

void OnButtonGetinfo()
{
CoInitialize(NULL);


if(CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
0)
!= S_OK)
return;

IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices = NULL;
IEnumWbemClassObject * pEnumObject = NULL;

BSTR bstrNamespace = (L"root\\cimv2");

if(CoCreateInstance (
CLSID_WbemAdministrativeLocator,
NULL ,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER ,
IID_IUnknown ,
( void ** ) & pIWbemLocator
) != S_OK)
return;

if(pIWbemLocator->ConnectServer(
bstrNamespace, // Namespace
NULL, // Userid
NULL, // PW
NULL, // Locale
0, // flags
NULL, // Authority
NULL, // Context
&pWbemServices
) != S_OK)
return;

HRESULT hRes;
BSTR strQuery = (L"SELECT * FROM Win32_OperatingSystem");

BSTR strQL = (L"WQL");
hRes = pWbemServices-
>ExecQuery(strQL,strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject);

if(hRes != S_OK)
{
printf("Could not execute Query");
return;
}

ULONG uCount = 1, uReturned;
IWbemClassObject * pClassObject = NULL;

hRes = pEnumObject->Reset();

if(hRes != S_OK)
{
printf("Could not Enumerate");
return;
}

hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject,
&uReturned);
if(hRes != S_OK)
{
printf("Could not Enumerate");
return;
}

VARIANT v1;
BSTR strClassProp = SysAllocString(L"NumberOfProcesses");
hRes = pClassObject->Get(strClassProp, 0, &v1, 0, 0);

if(hRes != S_OK)
{
printf("Could not Get Value");
return;
}

SysFreeString(strClassProp);

_bstr_t bstrPath = &v1; //Just to convert BSTR to ANSI
char* strPath=(char*)bstrPath;

if (SUCCEEDED(hRes))
printf(strPath);
else
printf("Error in getting object");

VariantClear( &v1 );

pIWbemLocator->Release();
pWbemServices->Release();
pEnumObject->Release();
pClassObject->Release();
CoUninitialize();
}

int _tmain(int argc, _TCHAR* argv[])
{
OnButtonGetinfo();
return 0;

}

Any help I can get is greatly appreciated. Thank you

Re: identifier not found error, undeclared identifier by Igor

Igor
Sun Feb 25 18:46:54 CST 2007

<aarthi28@gmail.com> wrote in message
news:1172446986.085205.141100@t69g2000cwt.googlegroups.com
> I have this code that I am trying to compile, but I am getting the
> following errors
>
> 'CoInitializeSecurity': identifier not found
> 'EOAC_NONE' : undeclared identifier

#define _WIN32_WINNT 0x0400
#include <objbase.h>

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: identifier not found error, undeclared identifier by William

William
Sun Feb 25 18:46:51 CST 2007

<aarthi28@gmail.com> wrote in message
news:1172446986.085205.141100@t69g2000cwt.googlegroups.com...
> I have this code that I am trying to compile, but I am getting the
> following errors
>
> 'CoInitializeSecurity': identifier not found
> 'EOAC_NONE' : undeclared identifier
>
> I have pasted the code below:

Take a look at the help entry for the function, it says that the function is
defined in <objbase.h>

There you'll fin this just above the declaration for the function

#if (_WIN32_WINNT >= 0x0400 ) || defined(_WIN32_DCOM) // DCOM

So, define either of the symbols before you include your headers and / or
include <objbase.h>

Regards,
Will
www.ivrforbeginners.com



Re: identifier not found error, undeclared identifier by Scott

Scott
Sun Feb 25 18:47:03 CST 2007

aarthi28@gmail.com wrote:
> Hi,
> I have this code that I am trying to compile, but I am getting the
> following errors
>
> 'CoInitializeSecurity': identifier not found
> 'EOAC_NONE' : undeclared identifier
>
> I have pasted the code below:
>
>
>
> #include "stdafx.h"
> #include "wbemidl.h"
> #include <comdef.h>

The documentation for this function says it is declared in objbase.h

Did you #include that file?

--
Scott McPhillips [VC++ MVP]


Re: identifier not found error, undeclared identifier by aarthi28

aarthi28
Sun Feb 25 22:29:22 CST 2007

Thanks everyone! I do have another question, though.

I made the following changes to the code and now it says
fatal error C1004: unexpected end-of-file found
I'm not quite sure what to do next.


// WMI_test.cpp : Defines the entry point for the console application.
//
//#define _WIN32_WINNT 0x0400
#include "stdafx.h"
#include "wbemidl.h"
#include <comdef.h>
#include <objbase.h>

#if (_WIN32_WINNT >= 0x0400 ) || defined(_WIN32_DCOM) // DCOM

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



void OnButtonGetinfo()
{
// TODO: Add your control notification handler code here
CoInitialize(NULL);


//Security needs to be initialized in XP first and this was the major
problem
//why it was not working in XP.

if(CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
0
) != S_OK)
return;

IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices = NULL;
IEnumWbemClassObject * pEnumObject = NULL;

BSTR bstrNamespace = (L"root\\cimv2");


if(CoCreateInstance (
CLSID_WbemAdministrativeLocator,
NULL ,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER ,
IID_IUnknown ,
( void ** ) & pIWbemLocator
) != S_OK)
return;

if(pIWbemLocator->ConnectServer(
bstrNamespace, // Namespace
NULL, // Userid
NULL, // PW
NULL, // Locale
0, // flags
NULL, // Authority
NULL, // Context
&pWbemServices
) != S_OK)
return;



HRESULT hRes;
BSTR strQuery = (L"Select * from win32_Processor");
BSTR strQL = (L"WQL");
hRes = pWbemServices->ExecQuery(strQL,
strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject);

if(hRes != S_OK)
{
printf("Could not execute Query");
return;
}

ULONG uCount = 1, uReturned;
IWbemClassObject * pClassObject = NULL;


hRes = pEnumObject->Reset();

if(hRes != S_OK)
{
printf("Could not Enumerate");
return;
}

hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject,
&uReturned);
if(hRes != S_OK)
{
printf("Could not Enumerate");
return;
}

VARIANT v;
BSTR strClassProp = SysAllocString(L"LoadPercentage");
hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0);
if(hRes != S_OK)
{
printf("Could not Get Value");
return;
}

SysFreeString(strClassProp);

_bstr_t bstrPath = &v; //Just to convert BSTR to ANSI
char* strPath=(char*)bstrPath;


if (SUCCEEDED(hRes))
printf(strPath);
else
printf("Error in getting object");

VariantClear( &v );
pIWbemLocator->Release();
pWbemServices->Release();
pEnumObject->Release();
pClassObject->Release();
CoUninitialize();

}

int _tmain(int argc, _TCHAR* argv[])
{
OnButtonGetinfo();
return 0;
}


Re: identifier not found error, undeclared identifier by John

John
Mon Feb 26 01:38:47 CST 2007

<aarthi28@gmail.com> wrote in message
news:1172464162.547164.227120@k78g2000cwa.googlegroups.com
> Thanks everyone! I do have another question, though.
>
> I made the following changes to the code and now it says
> fatal error C1004: unexpected end-of-file found
> I'm not quite sure what to do next.

The error is because you have an #if statement without a matching #endif.

> // WMI_test.cpp : Defines the entry point for the console application.
> //
> //#define _WIN32_WINNT 0x0400
> #include "stdafx.h"
> #include "wbemidl.h"
> #include <comdef.h>
> #include <objbase.h>
>
> #if (_WIN32_WINNT >= 0x0400 ) || defined(_WIN32_DCOM) // DCOM

The line above is the problem. The solution, however, is not to add an
#endif. It is to delete this line entirely since it makes no sense at all to
have it here.

The same line appears inside objbase.h and is used to test whether or not
various identifiers are to be declared. If you have

#define _WIN32_WINNT 0x0400

prior to

#include <objbase.h>

then the #if condition inside objbase.h will evaluate to true and the
relevant identifiers will be declared, which is what you want.

Your job is simply to add the line

#define _WIN32_WINNT 0x0400

It is not your job to provide the #if test.

--
John Carson