Click here to Skip to main content
15,887,027 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Error in ShowWindow() function ? Pin
krmed14-Sep-05 3:24
krmed14-Sep-05 3:24 
GeneralRe: Error in ShowWindow() function ? Pin
Amarelia14-Sep-05 3:44
Amarelia14-Sep-05 3:44 
AnswerRe: Error in ShowWindow() function ? Pin
kakan14-Sep-05 4:12
professionalkakan14-Sep-05 4:12 
QuestionHow to disable a menu item Pin
snprani14-Sep-05 2:50
snprani14-Sep-05 2:50 
AnswerRe: How to disable a menu item Pin
-Dy14-Sep-05 9:45
-Dy14-Sep-05 9:45 
QuestionHow to disable a menu item Pin
snprani14-Sep-05 2:46
snprani14-Sep-05 2:46 
AnswerRe: How to disable a menu item Pin
kakan14-Sep-05 2:56
professionalkakan14-Sep-05 2:56 
QuestionWMI,Help,help,help,Help,help,help,Help,help,help Pin
Tcpip200514-Sep-05 2:31
Tcpip200514-Sep-05 2:31 
Frown | :( doh:Sigh | :sigh: I use WMI to set Ip Address for my local computer.
every function works well. but nothing changed.
Why?? I use vc6,xp OS. For some header files,you can found them from vc7, just copy them to the Include directory for vc6.
BSTR Path = SysAllocString(L"\\\\MyComputerName\\root\\cimv2");
for a test,the above line must be modified.replace "MyComputerName" with your own computer name.
I paste all the code here. A win32 console app.
Thank you everyone.


#define _WIN32_DCOM
#include
#pragma comment(lib,"Wbemuuid.lib")
#include
//#include "wbemcli.h"
#include "objbase.h"
#include
#include
#pragma comment(lib, "comsupp.lib")
void CreateOneElementBstrArray(VARIANT* v, LPCWSTR s)
{
SAFEARRAYBOUND bound[1];
SAFEARRAY* array;
bound[0].lLbound = 0;
bound[0].cElements = 1;
array = SafeArrayCreate(VT_BSTR, 1, bound);
long index = 0;
BSTR bstr = SysAllocString(s);
SafeArrayPutElement(array, &index, bstr);
SysFreeString(bstr);
VariantInit(v);
v->vt = VT_BSTR | VT_ARRAY;
v->parray = array;
}
HRESULT __fastcall UnicodeToAnsi(LPCOLESTR pszW, LPSTR* ppszA)
{

ULONG cbAnsi, cCharacters;
DWORD dwError;

// If input is null then just return the same.
if (pszW == NULL)
{
*ppszA = NULL;
return NOERROR;
}

cCharacters = wcslen(pszW)+1;
// Determine number of bytes to be allocated for ANSI string. An
// ANSI string can have at most 2 bytes per character (for Double
// Byte Character Strings.)
cbAnsi = cCharacters*2;

// Use of the OLE allocator is not required because the resultant
// ANSI string will never be passed to another COM component. You
// can use your own allocator.
*ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);
if (NULL == *ppszA)
return E_OUTOFMEMORY;

// Convert to ANSI.
if (0 == WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA,
cbAnsi, NULL, NULL))
{
dwError = GetLastError();
CoTaskMemFree(*ppszA);
*ppszA = NULL;
return HRESULT_FROM_WIN32(dwError);
}
return NOERROR;

}

void PrintWMIError(HRESULT hr)
{
IWbemStatusCodeText * pStatus = NULL;
HRESULT hres = CoCreateInstance(CLSID_WbemStatusCodeText, 0,
CLSCTX_INPROC_SERVER,IID_IWbemStatusCodeText, (LPVOID *) &pStatus);
if(S_OK == hres)
{
BSTR bstrError;
hres = pStatus->GetErrorCodeText(hr, 0, 0, &bstrError);
if(S_OK != hres)
bstrError = SysAllocString(L"Get last error failed");
LPSTR pszStatusTextA;
UnicodeToAnsi(bstrError, &pszStatusTextA);
printf("%s\n",pszStatusTextA);
CoTaskMemFree(pszStatusTextA);
pStatus->Release();
SysFreeString(bstrError);
}
}
HRESULT ConfigNet()
{

IWbemLocator *pLocator=NULL;
IWbemServices *pNamespace=NULL;
IWbemClassObject *pClass=NULL;
IWbemClassObject *pInputParamClass=NULL;
IWbemClassObject *pInputParamInstance=NULL;
IWbemClassObject * pOutInst = NULL;
BSTR InstancePath = SysAllocString(L"Win32_NetworkAdapterConfiguration=2");
HRESULT hr;
BSTR Path = SysAllocString(L"\\\\MyComputerName\\root\\cimv2");
BSTR ClassPath = SysAllocString(L"Win32_NetworkAdapterConfiguration");
BSTR MethodName = SysAllocString(L"EnableStatic");
LPCWSTR Arg1Name = L"IPAddress";
VARIANT var1;
LPCWSTR Arg2Name = L"SubnetMask";
VARIANT var2;
__try
{
CreateOneElementBstrArray(&var1, L"10.0.0.101");
CreateOneElementBstrArray(&var2, L"255.255.255.0");

CoCreateInstance(CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID*)&pLocator);

hr = pLocator->ConnectServer(Path,NULL, NULL, NULL, 0, NULL, NULL,
&pNamespace);

hr = CoSetProxyBlanket( pNamespace,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE);

if(!SUCCEEDED(hr))
{
PrintWMIError(hr);
return hr;
}
if(SUCCEEDED(hr))
{
hr = pNamespace->GetObject(ClassPath, 0, NULL, &pClass, NULL);
}
if(!SUCCEEDED(hr))
{
PrintWMIError(hr);
return hr;
}
if(SUCCEEDED(hr))
{
hr = pClass->GetMethod(MethodName, 0, &pInputParamClass, NULL);
}
if(!SUCCEEDED(hr))
{
PrintWMIError(hr);
return hr;
}
if(SUCCEEDED(hr))
{
hr = pInputParamClass->SpawnInstance(0, &pInputParamInstance);
}
if(!SUCCEEDED(hr))
{
PrintWMIError(hr);
return hr;
}
if(SUCCEEDED(hr))
{
hr = pInputParamInstance->Put(Arg1Name, 0, &var1, 0);
}
if(!SUCCEEDED(hr))
{
PrintWMIError(hr);
return hr;
}
if(SUCCEEDED(hr))
{
hr = pInputParamInstance->Put(Arg2Name, 0, &var2, 0);
}
if(!SUCCEEDED(hr))
{
PrintWMIError(hr);
return hr;
}
if(SUCCEEDED(hr))
{
hr = pNamespace->ExecMethod(InstancePath, MethodName, 0, NULL,
pInputParamInstance, &pOutInst, NULL);
}
if(!SUCCEEDED(hr))
{
PrintWMIError(hr);
return hr;
}
}
__finally
{
if(pInputParamInstance)
pInputParamInstance->Release();
if(pInputParamClass)
pInputParamClass->Release();
if(pClass)
pClass->Release();
if(pNamespace)
pNamespace->Release();
if(pLocator)
pLocator->Release();
}
return hr;
}

int _tmain(int argc, _TCHAR* argv[])
{

CoInitialize(NULL);
ConfigNet();
CoUninitialize();
printf("Program end\n");
return 0;
}

AnswerRe: WMI,Help,help,help,Help,help,help,Help,help,help Pin
Tcpip200514-Sep-05 2:37
Tcpip200514-Sep-05 2:37 
GeneralRe: WMI,gni gni gni gni gni Pin
toxcct14-Sep-05 3:00
toxcct14-Sep-05 3:00 
GeneralRe: WMI,gni gni gni gni gni Pin
David Crow14-Sep-05 3:12
David Crow14-Sep-05 3:12 
GeneralRe: WMI,gni gni gni gni gni Pin
toxcct14-Sep-05 3:17
toxcct14-Sep-05 3:17 
GeneralRe: WMI,gni gni gni gni gni Pin
Tcpip200514-Sep-05 3:27
Tcpip200514-Sep-05 3:27 
GeneralRe: WMI,gni gni gni gni gni Pin
toxcct14-Sep-05 3:33
toxcct14-Sep-05 3:33 
GeneralRe: WMI,gni gni gni gni gni Pin
Tcpip200514-Sep-05 3:57
Tcpip200514-Sep-05 3:57 
GeneralRe: WMI,gni gni gni gni gni Pin
Tcpip200514-Sep-05 4:06
Tcpip200514-Sep-05 4:06 
GeneralRe: WMI,gni gni gni gni gni Pin
Tcpip200514-Sep-05 3:22
Tcpip200514-Sep-05 3:22 
GeneralSorry,I'm so sorry.Code here Pin
Tcpip200514-Sep-05 3:53
Tcpip200514-Sep-05 3:53 
GeneralRe: Sorry,I'm so sorry.Code here Pin
David Crow14-Sep-05 8:41
David Crow14-Sep-05 8:41 
GeneralRe: Sorry,I'm so sorry.Code here Pin
G Haranadh15-Sep-05 1:10
G Haranadh15-Sep-05 1:10 
QuestionSOCKETS SUPPORT Pin
REU14-Sep-05 2:22
REU14-Sep-05 2:22 
AnswerRe: SOCKETS SUPPORT Pin
charlieg14-Sep-05 12:04
charlieg14-Sep-05 12:04 
QuestionHow to settle this problem Pin
momer14-Sep-05 1:17
momer14-Sep-05 1:17 
AnswerRe: How to settle this problem Pin
prasad_som14-Sep-05 1:52
prasad_som14-Sep-05 1:52 
QuestionLogin Dialog! Pin
DerDachs14-Sep-05 1:13
DerDachs14-Sep-05 1:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.