|
I read about wininet.dll, but i'd like to know if a computer is connected as soon as is connected, how can i know it? i thought using a timer, but i don't think it's a good idea..
|
|
|
|
|
Hope following helps you
//Creating the extern function...
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( out int Description, int ReservedValue ) ;
//Creating a function that uses the API function...
public static bool IsConnectedToInternet( )
{
int Desc = 0 ;
return InternetGetConnectedState( out Desc, 0 ) ;
}
- ashish
|
|
|
|
|
Yes, but the problem isn't this. I need know where a computer is connected, how can i check it? checking with this method every second with a timer?
|
|
|
|
|
So I have the following structure definition:
[StructLayout(LayoutKind.Explicit)]<br />
struct MyStruct<br />
{<br />
[FieldOffset(0)]<br />
public float f;<br />
<br />
[FieldOffset(0)]<br />
public int i;<br />
<br />
[FieldOffset(4)]<br />
public object o;<br />
<br />
[FieldOffset(4)]<br />
public ArrayList al;<br />
}
And the following program:
static void Main(string[] args)<br />
{<br />
MyStruct s;<br />
s.i = 1;<br />
s.f = 1.0f;<br />
s.al = null;<br />
s.o = new object();<br />
Console.WriteLine("float = {0}", s.f);<br />
Console.WriteLine("int = {0:x}", s.i);<br />
Console.WriteLine("object = {0}", s.o);<br />
Console.WriteLine("arraylist = {0}", s.al);<br />
Console.WriteLine("arraylen = {0}", s.al.Capacity);<br />
s.al.Remove(s.o);<br />
Console.ReadLine();<br />
}
Essentially, I'm forcing an ArrayList reference to point to an object of type Object, then calling ArrayList member functions on that Object. As you might imagine, this leads to some fairly disastrous consequences in the runtime. Calling various members of the ArrayList results in an exception being thrown:
s.al.Add(s.o): RemotingException ("Cannot load type")
s.al.Remove(s.o): AccessViolationException
s.al.BinarySearch(s.o): SEHException
Obviously this program is no longer type-safe, but running the peverify utility on the generated executable gives a thumbs-up. All of this brings me to the actual question.. is this the intended behavior of the language and runtime, or is this a bug?
Googling brings me to http://discuss.develop.com/archives/wa.exe?A2=ind0112c&L=dotnet&D=0&P=53651, but this was written five years ago and doesn't actually provide an answer to the question. What do you guys think about this behavior? Am I justified in seeing this as a security vulnerability (perhaps a plugin commits a privilege escalation attack) rather than a simple bug?
|
|
|
|
|
I see this as possible problem.
Imagine having
[StructLayout(LayoutKind.Explicit)]
struct MyStruct
{
[FieldOffset(0)]
public int i;
[FieldOffset(0)]
public byte[] o;
}
now if you set:
x.i = 0x4899;
then you'd be able to access data from any address.
I don't know if peverify can check those.
|
|
|
|
|
The CLR will not allow object references to overlap value types using FieldOffset, but it will allow object references to overlap other object references.
But I think I just answered my own question. Changing the security permission set to require assembly verification causes the runtime to throw an exception when it tries to load the MyStruct type.
Could not load type 'StructLayout.MyStruct' from assembly 'StructLayout, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because objects overlapped at offset 4 and the assembly must be verifiable.
So it looks like the runtime only allows you to do this only if you already had the proper security permission set to execute unsafe code to begin with, which I think should be fine. Still somewhat strange that the peverify utility says that the assembly is verifiable, though.
|
|
|
|
|
I have a listbox I am trying to show the process information in. I'm attempting to fill the listbox dynamically via a new thread as the code below shows.
private void Form1_Load(object sender, EventArgs e)
{
new Thread(new ThreadStart(UpdateProcessList)).Start();
}
private void UpdateProcessList()
{
Process[] OldProcesses = new Process[1];
Process[] NewProcesses = Process.GetProcesses();
while (p_DemoClosing == false)
{
if (OldProcesses.Equals(NewProcesses) == false)
{
ProcessList.BeginInvoke((MethodInvoker)delegate
{
ProcessList.Items.Clear();
});
NewProcesses = Process.GetProcesses();
foreach (Process DisplayProcess in NewProcesses)
{
ProcessList.BeginInvoke((MethodInvoker)delegate
{
ProcessList.Items.Add(DisplayProcess.ProcessName);
});
}
OldProcesses = NewProcesses;
}
Thread.Sleep(50);
}
}
Everytime the script runs no matter what I do all the .ProccessName's say idle rather than displaying the actual name. Once I attempt to get the processes IE for the first display they all come up with a processname of "Idle".
|
|
|
|
|
'twould seem to me it is getting a process name, System Idle Process, shows up as just 'idle'
I don't know why, i also don't know why every process would have this name... hmm
Okay, now its wierd, i copy your code, and implement it, the first 4 processes all come up right, then all others list as Idle...
And now all list as Idle
-- modified at 20:14 Wednesday 18th October, 2006
|
|
|
|
|
Boo-ya! >_<
I tried it with just invoke (not begininvoke) and it works fine, so i think, hmm perhaps we need EndInvoke (well, it would make sense)
so you need to do it like this:
IAsyncResult AR = ProcessList.BeginInvoke((MethodInvoker)delegate<br />
{<br />
ProcessList.Items.Add(DisplayProcess.ProcessName);<br />
});<br />
<br />
ProcessList.EndInvoke(AR)
Well, my lame theory is:
Since you don't end the invoke, it just the listbox (or listview) just keeps updating with the last thing that was sent by ProcessList.BeginInvoke - which would be the process 'Idle' (previously noted as being 'System Idle Process')
|
|
|
|
|
Ok that makes sense. I was under the assumption that once the code block for the delegate executed that the BeginInvoke would terminate itself. Seems like common sense, but then again there must be another reason behind it all.
|
|
|
|
|
v9ExportPacket p = new v9ExportPacket();
p.TemplateRecord = new TemplateRecord[5];
TemplateRecord t_tmp = new TemplateRecord();
t_tmp.FlowSetID=0;
t_tmp.Length=10;
t_tmp.id=23;
t_tmp.FieldCount=4;
t_tmp.Fields= new FieldPairs[t_tmp.FieldCount];
t_tmp.Fields[0] = new FieldPairs(1,2);
t_tmp.Fields[1] = new FieldPairs(3,4);
t_tmp.Fields[2] = new FieldPairs(5,6);
t_tmp.Fields[3] = new FieldPairs(7,8);
p.TemplateRecord[0] = t_tmp;
i was wondering if there is a way that i could just use
p.TemplateRecord[0].id=1;
instead of having to create a TemplateRecord object t_tmp to set the properties of that class and then set the TemplateRecord[0] property object equal to t_tmp object. i have looked into using indexers but i haven't seen an example where you could access a property of an indexed property.
|
|
|
|
|
If you create an indexer and the object is a class, it's passed by reference, so you can automatically alter indexed properties.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Okay so let me see if i understand you correctly. I have two classes. Class A and Class B. I give class A 5 properties
int prop1(){}
int prop2(){}
int prop3(){}
int prop4(){}
classB prop5(){}
what i would like to do is to create a 5th property and make its type equal to ClassB. but i would also like to index this property too. BTW Class B also has some properties to it. So in the end i would be able to access my items like this:
classA xyz = new classA();
xyz.prop1=1;
xyz.prop2=2;
xyz.prop3=3;
xyz.prop4=4;
xyz.prop5[0].prop1=1;
xyz.prop5[0].prop2=2;
xyz.prop5[1].prop1=1;
xyz.prop5[1].prop2=2;
I remember doing stuff like this a while ago in VB6 but i'm pretty new to C# and i'm just learning out to do this sort of stuff. I hope that explain helps a little bit more. I think i understand what your saying though.
|
|
|
|
|
Not really clear on what you're trying to do, but I'll take a stab at it.
The important thing to remember when trying to access an index is what is it returning me?
For instance, take a DataSet.
DataSet ds;
DataTable dt = ds.Tables[0];
DataRow dr = ds.Tables[0].Rows[0];
object o = ds.Tables[0].Rows[0][0];
So, you can continue to index off a property, as long as that property contains an index operation. For your case, you might need to create a 'Collection' class that simply holds a collection of items for you. Assuming I'm even close on what you're asking a question about.
Hope that helps.
|
|
|
|
|
hey thanks for the reply. yea sorry i wasn't too clear as it was a post right as i was leaving work today. i think i understand what you mean and i will give it a try! thanks
|
|
|
|
|
Hi,
I have problem in graphics
in this code no compiletime error occurs.
private void Lines(System.Windows.Forms.PaintEventArgs e)<br />
{<br />
Graphics g = e.Graphics;<br />
Pen pen = new Pen(Color.Red,4);<br />
g.DrawLines(pen,10,10,25,25); <br />
}
But when i call this method with Onclick event of a button this error occurs in runtime:
"Object refrence not set a instance of an object."
and when i want to use his code
Graphics g = new Graphics();
this error occurs :
No overloaded method 'Graphics' takes '0' arguments
I want to call a method and paint some lines in my form but i dont want to override OnPaint method of form
like this :
protected override void OnPaint(<br />
PaintEventArgs paintEvent )<br />
<br />
{<br />
Graphics g = paintEvent.Graphics;<br />
Pen pen = new Pen(Color.Red,4);<br />
g.DrawLines(pen,10,10,25,25); <br />
}
How i can do this ?
---------------------
Areff Bahrami(KAVEH)
Areff.HB@Gmail.com
---------------------
|
|
|
|
|
Areff wrote: I want to call a method and paint some lines in my form but i dont want to override OnPaint method of form
That's the problem. You want to do it all wrong. You're getting that error as you're passing null as the argument, or you're passing a PaintEventArgs that doesn't contain a Graphics object. Either way, not overriding the paint method means you want to write bad code that won't work properly. Why would you want to do that ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
with .Net
---------------------
Areff Bahrami(KAVEH)
Areff.HB@Gmail.com
---------------------
|
|
|
|
|
I have no idea what you mean by that. Using C# means you'll be using .NET. That doesn't change what I said.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Christian Graus wrote: not overriding the paint method means you want to write bad code that won't work properly
I always use the Graphics.FromHandle function to get the Graphics object, why should this be bad code?
|
|
|
|
|
You can get a Graphics object from a windows (or control) handle.
I think the syntax is: Graphics.FromHandle(...);
Pass it a handle from a panel or button, or whatever tot draw on that control...
Hope this helps...
|
|
|
|
|
Use
Graphics g = this.CreateGraphics() in your constructor, and declare g as a class member so you can use it elsewhere. I've used this approach a few times to support custom double buffering on a control. If you want your control to display everything flickerfree you should implement this too (draw into a bitmap and flip it over to your graphics now and then), this can be handy for controls that need a high refresh rate (30fps). If you're not doing that, use OnPaint().
|
|
|
|
|
Hi,
What can i do if an ObjectDisposedException ocures?
I have 2 forms, 1 is calling 2. If i close 2 and restart it again from form1 i get that Exception.
I want to be able to restart the form2 again through from1.
This exception is pointing to the line which starts with:
Show();
Any suggestions?
Thanks in advance!
|
|
|
|
|
You have disposed of the form, you need to create a new instance of form2, as in use new to reassign the variable, or change the code so it does not get disposed.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hi,
That worked, thank you!
|
|
|
|