Blog moving to http://blog.xploiter.com

15 01 2009

This blog will be moving, actually it already has, i’m just keeping the two side by side until the search engines grab a hold of it properly.

Anyways the new URL is:

http://blog.xploiter.com

Pass it around, add comments, use and abuse it and let me know what you want me to cover 🙂

Si





How to programmatically turn on the Numlock Key

7 11 2008

The following code will allow you to turn on the numlock key upon startup. Can’t credit anyone as i’m not sure where it came from but it does work 100%

using System;
using System.Runtime.InteropServices;

class SetNumlockKeyOn
{
[StructLayout(LayoutKind.Sequential)]
public struct INPUT
{
internal int type;
internal short wVk;
internal short wScan;
internal int dwFlags;
internal int time;
internal IntPtr dwExtraInfo;
int dummy1;
int dummy2;
internal int type1;
internal short wVk1;
internal short wScan1;
internal int dwFlags1;
internal int time1;
internal IntPtr dwExtraInfo1;
int dummy3;
int dummy4;
}
[DllImport(“user32.dll”)]
static extern int SendInput(uint nInputs, IntPtr pInputs, int cbSize);

public static void SetNumlockOn()
{
const int mouseInpSize = 28;//Hardcoded size of the MOUSEINPUT tag !!!
INPUT input = new INPUT();
input.type = 0x01; //INPUT_KEYBOARD
input.wVk = 0x90; //VK_NUMLOCK
input.wScan = 0;
input.dwFlags = 0; //key-down
input.time = 0;
input.dwExtraInfo = IntPtr.Zero;

input.type1 = 0x01;
input.wVk1 = 0x90;
input.wScan1 = 0;
input.dwFlags1 = 2; //key-up
input.time1 = 0;
input.dwExtraInfo1 = IntPtr.Zero;

IntPtr pI = Marshal.AllocHGlobal(mouseInpSize * 2);
Marshal.StructureToPtr(input, pI, false);
int result = SendInput(2, pI, mouseInpSize); //Hardcoded size of the MOUSEINPUT tag !!!

//if (result == 0 || Marshal.GetLastWin32Error() != 0)
// Console.WriteLine(Marshal.GetLastWin32Error());
Marshal.FreeHGlobal(pI);
}
}





Deleting elements from XML Documents

7 11 2008

If you need to delete either a Node, Element or Attribute from your XML document, try the following code as it actually works!!!

XML Document Structure

<?xml version=1.0encoding=utf-8standalone=yes?>
<!–
XML Config file–>
<Tests
>
<
Test>
<
ID>47d272cd-877d-4d0f-ad4a-cfa31200d966</ID>
<
Object>Obj1</Object>
<
TestType>Fail Yes</TestType>
<
Command>fail yes</Command>
<
Time>202</Time>
<
Expiry>0</Expiry>
</
Test>
<
Test>
<
ID>71b211a7-99e4-4041-ada7-2925b6aff46f</ID>
<
Object>Obj2</Object>
<
TestType>Fail No</TestType>
<
Command>fail no</Command>
<
Time>243</Time>
<
Expiry>0</Expiry>
</
Test>
</
Tests>

To delete the element based on the GUID I Use:

///
/// Allows you to delete an element from the XML File
///
/// String GUID ID to remove from the config file
/// Name of the element to delete
public void RemoveElement(string elementName, string id)
{
System
.Xml.XmlDocument doc = new System.Xml.XmlDocument();

doc.Load(filename);

System.Xml.XmlNodeList list = doc.GetElementsByTagName(Test);

for (int i = 0; i < list.Count; i++)
{

int j = 0;
for (j = 0; j < list[i].ChildNodes.Count; j++)
{

if (list[i].ChildNodes[j].Name.ToUpper() == “ID”)
break;
}

if (j < list[i].ChildNodes.Count && list[i].ChildNodes[0].InnerText == id)
{
System
.Xml.XmlNode parent = list[i].ParentNode;
parent
.RemoveChild(list[i]);
}
}
doc
.Save(filename);
}

Another way that i’ve not got around to testing (but which I like) is to use a DataView to suck the XML in, parse using the filter property and then after deleting, re-export as XML:

DataSet ds = new DataSet();
ds
.ReadXml(“xmlAdd file as above”);
if (ds != null && ds.Tables.Contains(“stickies”))
{

DataView dv = new DataView(ds.Tables[“stickies”]);
dv
.RowFilter = “id=1”;
if (dv.Count >= 1)
{
dv[
0].Delete();
ds
.AcceptChanges();
ds
.WriteXml(“updated.xml”);
}
}





Using Regular Expressions to see if string validates as a numeric or number value

7 11 2008

The following method will take in a string and return a bool value if it’s a valid number:

///         
/// Uses Regular expressions to see if we are a number        
///         
///         
///         
static bool IsNumeric(string inputString)
{

return System.Text.RegularExpressions.Regex.IsMatch(inputString, “^[0-9]+$”);
}





Using KeyPreview to handle form shortcut keys

31 10 2008

If you need to trap shortcut keys in your application to perform misc tasks, use the following code to set KeyPreview to true and then in the KeyDown event handle the actual clicks:

 private void Form_OnLoad(object sender, EventArgs e)
{
KeyPreview
= true;
}

private void form_KeyDown(object sender, KeyEventArgs e)
{

if (e.Alt && e.KeyCode.ToString() == “L”)
{

//DO Something            
}
}





SQL Datetime Woes – The ultimate guide to the datetime datatypes

3 10 2008

I’ve not worked with SQL Server DateTime values for a while now so was a little rusty. I needed to do some date filtering and came across the following site by MVP Tibor Karaszi that I found really useful:

http://www.karaszi.com/SQLServer/info_datetime.asp





Dotnet Charting

17 09 2008

I was looking for some suitable dotnet charting components/code and came across the following three articles.

http://www.c-sharpcorner.com/UploadFile/pseabury/2DCharting11152005052210AM/2DCharting.aspx

http://www.c-sharpcorner.com/UploadFile/steve_hall/drawinglinechart02072007053420AM/drawinglinechart.aspx

http://www.c-sharpcorner.com/UploadFile/jodonnell/ChartingInGDIplus11192005063716AM/ChartingInGDIplus.aspx

Heres a screenshot of the one i’m using:





Get your local machine/computer name in c#

17 09 2008

Four ways to get your local network/machine name:

string name = Environment.MachineName;
string name = System.Net.Dns.GetHostName();
string name = System.Windows.Forms.SystemInformation.ComputerName;
string name = System.Environment.GetEnvironmentVariable(“COMPUTERNAME”);





Need a volume or slider control in your application? Check out the Advanced TrackBar

17 09 2008

I needed a slider control to use as fader sliders in an audio application I was writing using the ASIO libraries to control a MAYA44 USB audio device. Came across this one immediately and got right into using it, I really like it and it has a nice Mac look and feel about it too.

Advanced TrackBar (Slider) Control with MAC Style





Need to left click on a NotifyIcon?

17 09 2008

I’m working on a client/server application that uses touch screens for user input. The app uses the NotifyIcon class to minimise the app to the tray when not required with a ContextMenuStrip to handle right click operations, herin lies the problem – touch screens are using touch, not mice to interface with the application hence you cannot right click!

Solution is the code below, it allows you to left click (or touch) the trayicon and still display the ContextMenu. What a PIA it was to find this!

using System.Reflection;

private void notifyIcon1_MouseUp(object sender, MouseEventArgs e)
{

if (e.Button == MouseButtons.Left)
{

MethodInfo mi = typeof(NotifyIcon).GetMethod(“ShowContextMenu”, BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(notifyIcon1,
null);

}
}