|
I want to do it to a remote computer, so I suspect it's a WMI thing. Anyone have any idea how to do this through WMI?
|
|
|
|
|
Here is an example:
string server = @"\\" + txtServer.Text;
ManagementScope ms = new ManagementScope(server + @"\root\cimv2");
ObjectQuery oq = new ObjectQuery("select * from win32_OperatingSystem");
ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, oq);
ManagementObjectCollection moc = mos.Get();
foreach(ManagementObject mo in moc)
{
string[] ss = {""};
mo.InvokeMethod("Reboot", ss);
MessageBox.Show("The reboot command has been sent.");
}
The only difference between local and remote reboots is the ManagementScope and the optional ConnectionOptions.
Matt is a network administrator for an auditing company in the midwest. He is shamelessly looking for Windows programming side jobs.
|
|
|
|
|
That last one worked like a champ. Thanks!
|
|
|
|
|
How do I connect to a remote SNMP device using the WMI SNMP provider using System.Management objects? Can someone provide an example for me?
Matt is a network administrator for an auditing company in the midwest.
|
|
|
|
|
First try the SNMP provider:
http://www.microsoft.com/downloads/release.asp?ReleaseID=19776
(included on XP?)
http://www.microsoft.com/windows2000/docs/WMIandSNMP.doc
so you can still use WMI from C#.
|
|
|
|
|
Thank you for the links, but I have been to them. I have visited a lot of sites and the best I can get are vb scripts. I think I know in general what I need to do, but I really need a couple of specific examples.
Matt is a network administrator for an auditing company in the midwest. He is shamelessly looking for Windows programming side work.
|
|
|
|
|
Check out:
http://www.c-sharpcorner.com/Code/2002/Sept/SnmpLib.asp
|
|
|
|
|
Is it possible to do ownerdraw of ListView and TreeViews without using the windows API?
I am constrained by a pure C#/.NET framework requirement. This means no COM objects or accessing DLLs unless that functionality is directly exposed by some .NET framework.
All the exmaples I have seen so far seem to use windows API in the least.
Stupidity dies.
The end of future offspring.
Evolution wins.
- A Darwin Awards Haiku
|
|
|
|
|
Absolutely without - I don't know, but to be honest: I don't believe it (at least if even SendMessage is included into the restriction list). However, did you already had a look into the UtilityLibrary from Carlos Perez?? (just search here in CP)It's not only an excellent tutorial, it even has samples for Listbox- and Treeview implementation.
May be this gives you the ultimative hint?? Or you find a way to re-implement the rare API calls into pure C#?
Wish you success! Bye
Matze
|
|
|
|
|
|
every c#'s object like a pointer in c++. But How can i get a pointer point to pointer (**) in c#?
I just work on a undo/redo engine in my c# application.
For example, there is two type of object to be edit in application
class A
{
string aa;
}
class B
{
int bb;
}
The edit operation change the many objects of type A or type B.
I hope to build a stack of object to store the string aa and int bb. Two objects to be cloned, one before the edit operation, another after it.
If the only way to change a memeber of a object is xx.yy = zz, i find it impossible to do the undo. Because i have to know the parent object (A or B) and which type of it (A or B) and judge in code whether to use aa or bb.
I doubt whether reflection ability in c# can help me.
But in c++ style, it is very easy to use object*. something like
*orgin = cloneBefore
or
*orgin = cloneAfter
Do i make some mistake? Please give me some suggestion!!
or any undo/redo engine written by c# or java in the world?
|
|
|
|
|
|
I think you can get the same behavior by simply creating a class to hold your object reference. You would declare instances of that rather than object*, and then you'll be able to look inside your class and change the reference.
|
|
|
|
|
Yes, I think what you said is what i really want to do.
I need to bypass class A to access item aa in such an engine. But don't know whether c# permit it or not.
class A
{
string aa;
}
A someA;
someA.aa = "...";
In function calling, we can use "ref" keyword to have the object reference.
something like:
CallingFunc(ref someA.aa);
But i dont know how to store the object reference. "ref" seems only be used in calling parameters.
like:
store[i] = ref someA.aa ?
Can you show me some simple demo?
Thanks!!
|
|
|
|
|
You'd need to write a class like:
class ObjectReference
{
public object reference; // this could be a property if you wanted...
}
Then, whenever you create something that needs to be undo-able, you wrap it in one of these objects and store the reference to ObjectReference.
Note that this is going to make your code pretty ugly, as you'll have to cast from object to the real type whenever you use an object.
|
|
|
|
|
You should look into using the Command Pattern[^] to create a undo/redo framework.
|
|
|
|
|
Hi all,
I desperately need a hint guiding me the right direction...
I want to retrieve the NCM information using SystemParametersInfo. Well, to define the function call is rather easy, and the structure itself does not make lots of problems, too:
[StructLayout(LayoutKind.Sequential)]
public class NONCLIENTMETRICS
{
public int cbSize;
public int iBorderWidth;
public int iScrollWidth;
public int iScrollHeight;
public int iCaptionWidth;
public int iCaptionHeight;
public LOGFONT lfntCaptionFont;
public int iSMCaptionWidth;
public int iSMCaptionHeight;
public LOGFONT lfntSMCaptionFont;
public int iMenuWidth;
public int iMenuHeight;
public LOGFONT lfntMenuFont;
public LOGFONT lfntStatusFont;
public LOGFONT lfntMessageFont;
public NONCLIENTMETRICS()
{
cbSize = 340;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct LOGFONT
{
public int lfHeight;
public int lfWidth;
public int lfEscapement;
public int lfOrientation;
public int lfWeight;
public byte lfItalic;
public byte lfUnderline;
public byte lfStrikeOut;
public byte lfCharSet;
public byte lfOutPrecision;
public byte lfClipPrecision;
public byte lfQuality;
public byte lfPitchAndFamily;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=32)]
public string lfFaceName;
public static LOGFONT Empty
{
get
{... }
}
}
[DllImport("User32.dll"]
public static extern bool SystemParametersInfo(int Action, int Param, NONCLIENTMETRICS Param, int WinIni);
The call is
NONCLIENTMETRICS ncm = new NONCLIENTMETRICS();
bool b = SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, ncm, 0);
And now I'm getting confused:
b is true, that means the call succeeded, but ncm has not been touched. Is there anything wrong with the structure layout?? At the end I tried all combinations, instead of a class I declared NCM as a structure and used ref, I tried the IntPtr version... nothing didn't help. And I know that it works since MS does similar to build the SystemInformation members (Menufont is contained in NCM, too).
Somewhere I'm blocked. Can anybody help??
Thanks and bye
Matze
|
|
|
|
|
Matze wrote:
public NONCLIENTMETRICS()
{
cbSize = 340;
}
arrrg! Replace with
cbSize = Marshal.SizeOf(this);
Matze wrote:
DllImport("User32.dll"]
public static extern bool SystemParametersInfo(int Action, int Param, NONCLIENTMETRICS Param, int WinIni);
The call is
NONCLIENTMETRICS ncm = new NONCLIENTMETRICS();
bool b = SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, ncm, 0);
The declaration and method calls doesnt match! Why are you passing ncm.cbSize. Some functions however need to be called twice. 1st to find out what size parameter they need, 2nd the actuall call. this mite be the case. See if values from other paramters change during the call.
Hope this helps
DBHelper - SQL Stored Procedure Wrapper & Typed DataSet Generator for .NET low popularity, please visit
|
|
|
|
|
Hi leppie,
thanks for the answer!
leppie wrote:
bool b = SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, ncm, 0);
The declaration and method calls doesnt match! Why are you passing ncm.cbSize. Some functions however need to be called twice. 1st to find out what size parameter they need, 2nd the actuall call. this mite be the case. See if values from other paramters change during the call.
Hmm, the documentation states that for the GETNONCLIENTMAETRICS action the size of NCM must be told as second parameter, that's why I passed it (and so does MS, as one can see in the Decompiler;)). Actually, when I pass anything else than 340 (==Marshall..;)) the function returns false. No values are changed, unfortunately
leppie wrote:
public NONCLIENTMETRICS()
{
cbSize = 340;
}
arrrg! Replace with
cbSize = Marshal.SizeOf(this);
Yepp, of course I did this, but I thought a number is more informative in the message
|
|
|
|
|
Replace the DllImport declaration with :
[DllImport("User32.dll")]
public static extern bool SystemParametersInfo(int Action,
int Param,
IntPtr lpParam,
int WinIni);
And replace the method call with :
const int SPI_GETNONCLIENTMETRICS = 41;
NONCLIENTMETRICS ncm = new NONCLIENTMETRICS();
int size = ncm.cbSize = Marshal.SizeOf(typeof(NONCLIENTMETRICS));
IntPtr pncmetrics = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(ncm, pncmetrics, true);
bool b = SystemParametersInfo(SPI_GETNONCLIENTMETRICS, size, pncmetrics, 0);
Marshal.PtrToStructure(pncmetrics,ncm);
Marshal.FreeHGlobal(pncmetrics);
The .NET 1.0 marshaller has some troubles when it comes to marshalling LPStruct. Hopefully, this will be fixed in 1.1 (may be someone using the Everett beta could tell us whether it is fixed or not).
|
|
|
|
|
Hi Rod,
thanks a lot, that sounds feasible. Will give you immediate feedback today evening when I'm back from the customer..
Bye
Matze
|
|
|
|
|
Oh whow, you are my hero - it works!! Yipppieeeehh!!! You made my day, no, my month;) How for heavens sake did find that out???
Thanks and bye
Matze
|
|
|
|
|
|
Option 2:
- Change the
NONCLIENTMETRICS to a struct (NB: You'll have to remove the constructor.) - Change the declaration to:
[DllImport("User32.dll")]
public static extern bool SystemParametersInfo(int Action,
int uiParam, ref NONCLIENTMETRICS pvParam, int WinIni); - Change the call to:
NONCLIENTMETRICS ncm = new NONCLIENTMETRICS();
ncm.cbSize = Marshal.SizeOf(ncm);
bool b = SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
ncm.cbSize, ref ncm, 0);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
|
Hi Richard,
this was my very first try, before I changed NCM to a class (and before I read any documentation;)). The call succeeds, too, but NCM remains at its initial state...
Referring to leppie I would prefer this method, too, since it's much more NET like. Does this call succeed on your machine?
Thanks and bye
Matze
|
|
|
|