|
|
|
|
Welcome to Satan Eats Cheese Whiz Satan Eats Cheese Whiz is a programming blog, mostly directed at .NET Programming. Here you will find snippets, tutorials and other useful code that you can adapt or use with your projects. When using the code provided, please leave the comments intact, and link back to the page you got it from. Thanks for viewing!
Goto My Wiki Syndicate this Blog (RSS)
| |
|
|
|
|
|
|
|
0 guest(s)
0 member(s)
0 anonymous member(s)
| |
|
|
|
|
Entries on Friday 20th June 2008
|
|
|
|
20 Jun, 2008 - 08:24 PM
|
It has no doubt been a while since I blogged, I am lazy. I've found it is pretty useful to link to MSDN when providing advice to users, but have often been too lazy to bother to go all the way over to their site just for link love. So I developed this:  It parses an XML file I made (namespaces.xml), to fill a combobox that is editable, creates the link for you and checks if it exists. It doesn't do the individual classes, and I was going to make it literally read the namespaces available from the .NET framework assemblies, but it was going to be too much work bothering to sort through all the files in the directory to check if they were actually part of the .NET framework yata yata yata. I'll be releasing it open-source under the LGPL, and you can download it here (rapidshare). It is handy no doubt and saves a lot of time, it is also handy for classes if you know their names (which I'm assuming you do). Let me know what you think on this  . Note that this is a WPF application, which means it will require .NET 3.0 or 3.5 (I forget, most likely 3.0). Either way, you should have both, if not do the world a favour and download it.
| |
|
|
|
Entries on Wednesday 20th February 2008
|
|
|
|
20 Feb, 2008 - 12:47 AM
|
It's been a little while since I've blogged but rest assured the time I actually have without maths homework is, in my opinion spent wisely (e.g. Relaxing). But I have recently been experimenting with a language called BF recently, and I have found it to be a very exciting language as it really requires that you plan out your applications before you start working on them. I would like to make it very clear that BF stands for Brainfuck. The reason why i used it as an acronym is that I'd rather people not be repelled by swearing, for all you politically correct citizens. I caught on to BF fairly quick after about an hour of experimentation, however it may vary for those with more or less programming flow knowledge. There are some prerequisites to this though, you must be willing to give it a try (obviously), and you must have a logical mind. In this entry I am going to try to explain to you how Hello World works. Hello World is your foundation point in any language. If you skip Hello World, you are skipping a very needed stepping stone to knowledge, and you are going to drown in the deep end if you aren't an experienced coder. The BF language works with a combination of 8 characters, these include: 1. + - The Increment Character. This character will increment the byte at the pointer by one. 2. - - The Decrement Character. This character will decrement the byte at the pointer by one. 3. > - The Increment Pointer Character. This character will increment the pointer by one. 4. < - The Decrement Pointer Character. This character will decrement the pointer by one. 5. . - The Output Character. This character will print the ASCII value of the pointer to the screen. 6. , - The Input Character. This character will get the key pressed by the client, and store it in the pointer. 7. [ - The While Character (aka Jump Forward Character). This character will cause iteration, assuming the pointer is a nonzero and is encapsulated with a '[' character. 8. ] - The End While Character (aka Jump Back Character). This character will enclose iteration depicted by a '[' character. Every other character will be ignored. Now that we know what all the characters do, lets get started with our first Hello World (note this is not the correct standard, it is purely for beginning purposes, the 'proper' way will be explained further down). CODE ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++. Print 'H' +++++++++++++++++++++++++++++. Print 'e' +++++++. Print 'l' . Print 'l' +++. Print 'o' -------------------------------------------------------------------------------. Print ' ' +++++++++++++++++++++++++++++++++++++++++++++++++++++++. Print 'W' ++++++++++++++++++++++++. Print 'o' +++. Print 'r' ------. Print 'l' --------. Print 'd'
Short Hand: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++.+++.------.--------.
The resulting code will end up in a 'Hello World' result. Now, I bet you are thinking at the moment, why did I do it that way, and not declare a pointer for every ASCII character? Well, because we won't be actually using any of the singular chars for anything other than printing to the screen, we don't need to be wasting memory on values we won't be accessing ever again. This probably isn't a very efficient method for program size and speed, seeing BF was designed in 1993, to work with Amiga computers. The second last Amiga computer was the Amiga 4000, built in 1992, and it had a blistering fast 25MHz processor with 2MB-18MB of ram. Maybe now you can see why wasting the memory for no reason would be great practice back in the day if you had 2MB of ram.  Ok, now that we have that all out in the open, we can now look at what the so called 'experts' have to say at Wikipedia. This is what their example looks like (taken from this click). CODE ++++++++++ [>+++++++>++++++++++>+++>+<<<<-] The initial loop to set up useful values in the array >++. Print 'H' >+. Print 'e' +++++++. Print 'l' . Print 'l' +++. Print 'o' >++. Print ' ' <<+++++++++++++++. Print 'W' >. Print 'o' +++. Print 'r' ------. Print 'l' --------. Print 'd' >+. Print '!' >. Print newline
Ok, lets look a the loop at the start. As indicated by the 10 '+' characters, the loop will perform 10 iterations. All this loop does is make the program smaller, and set 4 cells to a value near the ASCII values we will be dealing with. Hence, the pointer after the first cell will be 60, as 6*10=60, the second created cell will be 100, as 10*10=100, the same deal for the other two created pointers, 30 and 10. Then, the pointer will be decremented to be back to the starting cell, in order to decrement the iteration. Then, the ASCII values will be printed according to the slight increments and decrements of the four cells we are using to print "Hello World!". I hope you have learnt a little bit from this entry, and hope you start working on some of your own BF projects from this tutorial. For further reading, I recommend Brainloller, it is pretty much exactly the same thing as BF, rather it loads the commands from an image, reading pixel by pixel and parsing commands by pixel colour.
| |
|
|
|
Entries on Monday 4th February 2008
|
|
|
|
4 Feb, 2008 - 11:49 PM
|
DICWriter v0.1DICWriter is an open-source BBCode IDE coded in C# for Dream In Code users. The aim of the project is to encourage the creation of larger tutorials for authors, and for posts that need to be answered over and over again, like how we don't hand out code to those that don't provide their own, etc. Features:o WYSIWYG Preview function (you can see what your BBCode will look like on a webpage). o A template system, for those that don't like having to say things over and over again (to create a template, you must save the bbcode file to the /Templates/ folder). o The most common BBCode tags are supported. o Use of the Color Dialog to make choosing colors more efficient and powerful. To-Do List:1. Add more tags to the BBCode parser, such as inline etc. 2. Make the preview look more attractive, with quotes, code, and other things that should generally look a little bit more 'encapsulated'. 3. Add support for smileys, in both the toolbar and preview function. 4. Intellisence for both tags and smileys. 5. Support for different mainstream bulliten boards (long-term of course, and yes I will be providing support for GrimBB if I attempt this ambitious project  ). 6. Add more default templates. Buglist:It is sort of expected to have bugs after about 4 days of development, and by 4 days I mean like days I have actually touched it (e.g. 6 days since I started developing it). Not all bugs are known, so please report them in the comments section, whatever bug it may be. 1. Need to double-click the open button when selecting templates. 2. The main toolbar doesn't appear properly in the source, and requires 'toying' with the panels to display properly in designer view. Download:Application (Rapidshare): ClickSource (Rapidshare): ClickIf you like where this project is going, feel free to drop me a line in the comments section to see how you can participate in the development of this applicaton (whether it be development or beta testing). The more support I recieve, the more chance of there being updates  . Hope this was of use to you, it sure was for creating this article!
| |
|
|
|
Entries on Saturday 26th January 2008
|
|
|
|
26 Jan, 2008 - 03:40 PM
|
Ever needed to dim a screen down programmatically to save power while a lengthy operation completes its task? Ever needed to make the screen brighter because users were having difficulty viewing your application, or you just wanted to piss the user off fading the screen in and out of brightness to prompt the user for input? Either way, all three of these options are possible using the WINAPI! Note that setting the brightness may not work on some graphics cards, but it should work on most of them. First we need to make a declaration to the CreateFile method. This will return the pointer of the device we would like to communicate with, and we want to use it for communication with the graphics card. CODE Private Declare Auto Function CreateFile Lib "kernel32" _ (ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _ ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _ ByVal dwCreationDisposition As Int32, _ ByVal dwFlagsAndAttributes As Int32, ByVal hTemplateFile As IntPtr) As IntPtr
Now we need a couple of declarations to tell the function that we want to read the device (because we aren't doing any writing as of yet), and that it is already existing. CODE Const GENERIC_READ As Int32 = &H80000000% Const SHARE_ALL As Int32 = &H7% Const OPEN_EXISTING As Int32 = &H3%
Now that we have the CreateFile method setup, we are able to create some dependencies for the DeviceIoControl method we will be using to actually set the value. CODE Const IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS As Integer = &H23049C%
Private Structure BRIGHTNESS Public ucDisplayPolicy As Byte Public ucACBrightness As Byte Public ucDCBrightness As Byte End Structure
We can now declare the function like so: CODE Private Declare Auto Function DeviceIoControl Lib "kernel32" _ (ByVal hDevice As IntPtr, ByVal dwIoControlCode As Int32, _ ByRef lpInBuffer As BRIGHTNESS, ByVal nInBufferSize As Int32, _ ByVal lpOutBuffer As IntPtr, ByVal nOutBufferSize As Int32, _ ByRef lpBytesReturned As Int32, ByVal lpOverlapped As IntPtr) As Boolean
We will also need to declare the CloseHandle function, to close the handle of the CreateFile method we will be using. CODE Private Declare Auto Function CloseHandle Lib "kernel32" _ (ByVal hObject As IntPtr) As Boolean
Now that we have all our declarations, we can now use the function to set the brightness. CODE ''' <summary> ''' Set the brightness of the screen. ''' </summary> ''' <param name="Brightness">The Screen Brightness</param> ''' <remarks></remarks> Private Sub SetBrightness(ByRef Brightness As BRIGHTNESS) ' Get the display Dim pDisplay As IntPtr = _ CreateFile("\\.\LCD", GENERIC_READ, SHARE_ALL, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
' Set the brightness. DeviceIoControl(pDisplay, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, _ Brightness, Marshal.SizeOf(Brightness), IntPtr.Zero, 0, Nothing, IntPtr.Zero) ' Close the handle. CloseHandle(pDisplay) End Sub
We can now call the function like so (put this code in a subroutine): CODE Dim brightness As BRIGHTNESS
' The brightness on DC Power brightness.ucDCBrightness = 100 ' The Brightness on AC Power brightness.ucACBrightness = 100
' Call the function. SetBrightness(brightness)
And now you should be able to set the screen brightness! Be aware that you won't be able to make the screen completely dark (well, in Vista anyway, I haven't tried it on any other machine), it will only go as low as it will in the Power Options settings. Experiment with the values a little bit to obtain different results. Thanks for reading, and I hope you learnt something new about screen brightness!
| |
|
|
|
Entries on Thursday 24th January 2008
|
|
|
|
24 Jan, 2008 - 08:26 PM
|
Welcome to this entry on Getting / Setting window text on external applications using the Windows API, in C#. I have recently been playing with the WINAPI functions, and found them quite intriguing, and thought I'd share my experiences with you, as everyone likes a good hax every now and then just to satisfy curiosity. I apologise for this not being totally related to .NET as such, but this really does fill the non-existant bridge to setting window text on external applications; something .NET can't do afaik. Getting and Setting window names are both relatively easy tasks, and are easy to grap, thankfully. Lets start by setting the window name of "Solitaire" to "Care Bear" (it kinda rhymes doesn't it?). First we will need to create a declaration to the SetWindowText function in user32.dll. There are two parameters, the first being the window handle to the application, and the second being the text you'd like to replace the current text with. CODE [DllImport("user32.dll", EntryPoint = "SetWindowText", CharSet = CharSet.Ansi)] public static extern bool SetWindowText(IntPtr hWnd, String strNewWindowName);
Here is how we can call the function to replace the text of our own Form. CODE SetWindowText(this.Handle, "Hello World"); This will access the Handle property of the form, and use it to replace the Window's text. Now lets take a look at the FindWindow function. The FindWindow function will search through all the windows, and return the handle of the best suited window name you searched for, defined as a string parameter. We can alternatively search through the class names, but that won't be needed here. Lets take a look at how we can declare it. CODE [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)] public static extern IntPtr FindWindow(string className, string windowName);
The first parameter being the name of a class to search for the Window responsible for it, and the second parameter being the Window Name. Seeing as we don't want to use the class name, we will just give it a value of null. Now here is how we can set the window text of solitaire, using both the SetWindowText and FindWindow functions. CODE SetWindowText(FindWindow(null, "Solitaire"), "Care Bear"); Now lets have a look at using the GetWindowText function, to get the window text of the newly changed window name. But however, we have an issue. How are we supposed to keep track of the Window name it is supposed to be; the window text could be dynamic. Using Spy++ (a tool that comes with visual studio), I was able to find Solitaire has its own class named (who would have thought), "Solitaire". This is static, so we can be sure to know we are getting the correct window handle regardless of the window name getting changed. Hence we would be using the FindWindow function like so: CODE FindWindow("Solitaire", null) Now we need to make our declaration of the GetWindowText function. This function will get the text of the window according to the handle and store it in a buffer. CODE [DllImport("user32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Ansi)] public static extern bool GetWindowText(IntPtr hWnd, [OutAttribute()] StringBuilder strNewWindowName, Int32 maxCharCount);
You might want to take note of the OutAttribute modifier, it is a member of the System.Runtime.InteropServices namespace, and is the kindof the equivilant of a pointer variable. Lets see how we can put this to use. First we will need a StringBuilder variable to store the buffer, then we will need to call the function. Here is an example: CODE StringBuilder sbWinText = new StringBuilder(256); GetWindowText(FindWindow("Solitaire", null), sbWinText, 256);
Note the 256, this is the equivilant of one byte (0-255), and is ample amount of charactors for the window name. The first GetWindowText parameter is the handle, the second is the buffer to store the window name and the third is the maximum count of charactors the function will be using. We can now get a string value of the window text by using the ToString() method, and for the purpose of this example we will use a MessageBox. CODE MessageBox.Show(sbWinText.ToString()) I hope you learnt something about getting and setting window text from the WINAPI, and thanks for viewing.
| |
|
|
|
Entries on Thursday 17th January 2008
|
|
|
|
17 Jan, 2008 - 04:37 AM
|
Welcome to this entry of Satan Eats Cheese Whiz! This is a relaunch of my old blog, with the purpose of being more purposeful. But with that aside, lets delve into the magical world of disassembling and decompiling. Ever programmed a C#, VB.NET or MC++ application, where it's contents needed to be kept absolutely secret, as its code had various passwords to SQL databases and other servers, and you thought you were safe by defining it private? How wrong you were. Disassembling gives the hacker the ability to look through the assembly of the code, and null references to calls, such as anti-cheat solutions etc. Decompiling practically gives the hacker the ability to view your source completely, in the same high-level way you would be used to programming it in. A free solution to decompiling can be found here ( click). Now, I bet after disassembling the application, you are probably panicing. You have given thousands of script-kiddies the ability to look through your code. You have reason to have a poo about this if you are illiterate, or just fail at life; thats what the helpless people would do. If you are actually willing to help yourself instead of scream and shout (I know of the kind, just taking precautions..  ), please continue reading. An excellent solution for this problem is a tool that came shipped with Visual Studio 2005 (earlier or later, I haven't a clue) named Dotfuscator. Simply run the application and set it running on an assembly (*.exe or *.dll), and let it do its job. It does its job speedy and effectively, removing the ability to both decompile, and disassemble. Strings are encrypted in the process, but of course don't affect the application's ability to function. I am estatic about this tool, as now I can feel slightly safer from cheaters; I plan on developing a multiplayer game for kicks as a little challenge for myself down te track. I hope this shed some light as to how you can obfuscate your applications!
| |
|
|
|
| |