03 Jun 2017 @ 3:00 AM 

One of the fun parts of personal projects is, well, you can do whatever you want. Come up with a silly or even dumb idea and you can implement it if you want. That is effectively how I’ve approached BASeBlock. Iit’s sort of depressing to play- held back by older technologies like WindowsForms and GDI+, and higher resolution screens make it look quite awful too. Even so, when I fire it up I can’t help but be happy with what I did. Anyway, I had a lot of pretty crazy ideas for things to add into BASeBlock, some fit and were even rather fun to play- like adding a “Snake” boss that was effectively made out of bricks- others were sort of- well, strange, like my Pac man boss which attempts to eat the ball. At some point, I decided that the paddle being able to shoot lightning Palpatine-style wasn’t totally ridiculous.

Which naturally led to the question- how can we implement lightning in a way that sort of kind of looks believable in a mostly low-resolution way such that if you squint at the right angle you go “yeah I can sort of see that possible being lightning?” For that, I basically considered the recursive “tree drawing” concept. One of the common examples of recursionm is drawing a tree; f irst you draw the trunk, then you draw some branches coming out of the trunk, and then branches from those branches, and so on. For lightning, I adopted the same idea. The eesential algorithm I came up with was thus:

  1. From the starting point, Draw a line in the specified direction in that direction at the specified “velocity”
  2. From that end point, choose a random number of forks. For each fork, pick an angle up to 45 degrees of difference from the angle between the starting point and the second point, and take the specified velocity and randomly add or subtract up to a maximum of 25% of it.
  3. If any of the generated forks now have a velocity of 0 or less, ignore them
  4. otherwise, recursively call this same routine and start another “lightning” from this position at the specified velocity from the fork position.
  5. Proceed until there are no forks to draw or a specified maximum number of recursions has been reached

of course as I mentioned this is a very crude approximation; lightning doesn’t just randomly strike and stop short of the ground and this doesn’t really seek out a path to ground or anything along those lines. Again, crude approximation to at least mimic lightning. The result in BASeBlock looked something like this:


Now, there are a number of other details in the actual implementation- first it is written against the game engine so it “draws” using the game’s particle system, and also uses other engine features to for example stop short on blocks and do “damage” to blocks that are impacted, and there are short delays between each fork (which again is totally not how lightning works but I’m taking creative license). The result does look far more like a tree when you look at it but the animation and how quickly it disappears (paired with the sound effect) is enough, I think, to at least make it “passably” lightning.

But All this talk, and no code, huh? Well, since this starts from the somewhat typical “Draw a shrub” concept applied recursively and with some randomization, let’s just build that- the rest, as they say, will come on their own. And by that, I suppose they mean you can adjust and tweak it as needed until it gets the desired effect. Or maybe you want to draw a shrubbery, I’m not judging. With that in mind here’s a quick little method that does this against a GDI+ Graphics object. Why a GDI+ Graphics Object? Well, there isn’t really any other way of doing standard Bitmap drawing on a Canvas type object as far as I know. Also as usual I just sort of threw this together so I didn’t have time to paint it and it might not be to scale or whatever.

What amazing beautiful output do we get from this? Whatever this is supposed to be:


It does sort of look like a shrubbery I suppose. I mean, aside from it being blue, that is. It looks nothing like lightning, mind you. Though in my defense if electricity tunnels through certain things it often leaves tree-like patterns like this. Yeah, so it’s totally electricity related.


This is all rather unfulfilling, so as a bonus- how about making lightning in Photoshop:


Step 1: Start with a Gradient like so

Next, Apply the “Difference Clouds” filter with White and Black selected as Main and Secondary Colours.

Invert the image colours, then Adjust the levels to get a more pronounced “Beam” as shown.

Finally, add a layer on top and use the Overlay filter to add a vibrant hue- Yellow, Red, Orange, Pink, whatever. I’m not your mom. I made it cyan for some reason here.

Posted By: BC_Programming
Last Edit: 03 Jun 2017 @ 03:00 AM

EmailPermalinkComments (0)
Tags: , , ,
Categories: .NET, C#, Programming
 27 Oct 2016 @ 12:39 PM 

This is part of a series of posts covering new C# 6 features. Currently there are posts covering the following:
String Interpolation
Expression-bodied Members
Improved Overload resolution
The Null Conditional operator
Auto-Property Initializers

Yet another new feature introduced into C# 6 are a feature called Dictionary Initializers. These are another “syntax sugar” feature that shortens code and makes it more readable- or, arguably, less readable if you aren’t familiar with the feature.

Let’s say we have a Dictionary of countries, indexed by an abbreviation. We might create it like so:

This is the standard approach to initializing dictionaries as used in previous versions, at least, when you want to initialize them at compile time. C#6 adds “dictionary initializers” which attempt to simplify this:

Here we see what is effectively a series of assignments to the standard this[] operator. It’s usually called a Dictionary Initializer, but realistically it can be used to initialize any class that has a indexed property like this. For example, it can be used to construct “sparse” lists which have many empty entries without a bunch of commas:

The “Dictionary Initializer” which seems more aptly referred to as the Indexing initializer, is a very useful and delicious syntax sugar that can help make code easier to understand and read.

Posted By: BC_Programming
Last Edit: 27 Oct 2016 @ 12:40 PM

EmailPermalinkComments (0)
Tags: , ,
Categories: .NET, C#, Programming
 22 Nov 2015 @ 10:13 PM 

It was some time ago, and repeatedly, that I’ve mentioned my desire to rework some parts of the site. In particular, the “Downloads” page currently goes to a “custom” CMS of sorts which uses a Database that I have to manipulate manually to add items, as well as using my older site design. (I still like it, but apparently everybody calls it “retro 1996” so I don’t think it really works for what I was going for). I’ve considered a redesign of that main site, since I really don’t want to scrap it, but for some time now I’ve merely redirected the main website straight to this wordpress blog.

As I was writing this, I intended to and had even started to design a new database table to attempt “another go” at a download CMS. My entire plan was to effectively “blog out” each step along the way- since such a task would involve not only writing PHP, but would also involve reworking my existing, older “Update” Library that provides update functionality (or, it is supposed to, at any rate) to some of my existing programs, so it would be quite code intensive. It suddenly dawned on me that there may very well be a WordPress plugin for that, so I’ll be taking a look into that shortly (It looks like some of those available may meet my needs). ‘Thankfully’, even if I take that route I will need to make modifications to my client-side library to appropriately interface with my wordpress site to retrieve downloads, so it won’t be a completely solved case. For now, I’ll share my initial design phase.

The first step to a new design is to evaluate the existing design and determine where I went wrong. My existing database suffers from several problems. First, there is no consistent casing in the field names; second, the field values are sometimes repurposed- some of them are supposed to be URLs, but if the URL isn’t valid, they are supposed to be treated as something else. There are magic number fields (What is a download type? I don’t even know and I wrote the bloody thing, apparently). So with all these issues in mind, I decided to redo the design work.

Do one thing

The existing design does too much. I was going for some weird over-arching CMS, with Links, Articles, Downloads, and flash movies and the like all being available in one database and from one section of my site. This made sense before I had a wordpress blog here but now some of that is irrelevant; rather than store links there, I can put them in a standard blogroll. Furthermore, I would ideally be able to have some sort of integration with wordpress (this why I’m leaning towards plugins as I mentioned) rather than have the downloads seem to be hosted “somewhere else”. of course, with that original design, it was simply trying to do too much, and since I didn’t design it for that originally, it started adding magic “dltype” fields to indicate what kind of an item it was, and repurposing fields. “If it is this type, these fields mean this, otherwise, they mean this other thing” which was a complete mess. The solution, of course, is to simply do one thing. In this case, I only want it for downloads, and that is it. So we can pare off a lot of excess fat from the rump of the problem just with that.

One annoyance in the original table was that I named fields stupidly, and they use inconsistent casing. When you have to repeatedly look up the schema to figure out if you put a field name in properly, that is a problem. the solution I devised for this was simple- make them all lowercase field names. No more question of “Was that uppercase? was it Pascal Case? etc. since that becomes a non-starter question since it is all lowercase.

I redesigned the schema, and came up with the following table layout:

Field Name Type Description
dlid AUTO_INCREMENT (PKEY)unique id for this download item.
icon text url of image to display to represent this item where appropriate (lists, etc.)
name text Name of this item
description text short blurb for this item
version varchar(32) X.X.X.X Version value for this item
datecreated timestamp timestamp for when this item was created. (probably can just have a default of CURRENT_TIMESTAMP and not be referenced for inserts)
downloadcount int count of times this item has been downloaded
hidden bool whether this item is hidden or not
url text download URL for this item
parentid int id of the item to which this one is attached. -1 for top-level (default -1)

This is much simpler than the existing schema, which should, in turn, simplify implementations dealing with it. The next step, then, would be coming up with a design for the PHP pages for displaying and managing the database table information.

However, for simplicity’s sake I rather hope WordPress plugins will do the job- then it becomes a question of determining how to retrieve that information client side- hopefully, a SOAP or other remoting API will allow pulling down information about downloads, but that remains to be seen.

Posted By: BC_Programming
Last Edit: 22 Nov 2015 @ 10:13 PM

EmailPermalinkComments (0)
 02 Aug 2015 @ 8:55 AM 

I’ve got a problem.

Computer/Programming books. I have a shelf full of them. When I was a teenager my computer time was limited, so I would often read computer books I had or borrowed. Recently I decided to expand my collection and add classics. I bought ‘Clean Code’ and “Code Complete”. Both well-regarded books about software development practices. I’ve barely cracked open either of them, sadly. The only time I can think of doing so was when I had a power outage!

Following this proud tradition, I decided to add more books to my collection. In particular- the biggest one was the entire four-volume set of Donald Knuth’s The Art of Computer Programming. I also got “Programming F# 3.0” and “Programming C# 5.0”. And the overwhelming question to me is “why”…. When will I read them? I’ve hardly even touched the two I bought over 2 years ago!

Books versus Internet

Programming/Computer Books do have a rather hefty competition in the form of the Internet. I think there is an argument to be made for books, however, even compared to eBook devices such as Kindles. There is an ineffable quality to a good book, and I think it being physical makes reading it more “personal” in some way.

The aforementioned titles have since arrived, and I’ve tried to get started with them. I’ve pushed through The first volume of the “Art of Computer Programming” as best I can. I can see why it has become something of a Programmer’s Bible of sorts, and why Knuth is so well-regarded in the industry.

One of the biggest advantages of Donald Knuth’s “Art of Computer Programming” is that it’s content is effectively timeless- he mentions therein that it is effectively designed to be accessible, and from what I’ve read so far (not much, admittedly!) he holds true to that

very well. This is in contrast to what could be called more timely titles. For example, “Programming F# 3.0” will eventually be “outdated” in the sense that there will be future versions of F# released; same with “Programming C# 5.0”. Both of these are excellent titles but will they still be useful in fifty years? Arguably, it doesn’t really matter since there will be updated versions, and a good software developer tries to keep up to date on the most recent technologies and platforms. But there is something to me -unsavoury- about content, like books that eventually becomes out of date.

On the other hand, an argument could be made that, regardless of content, books have a “timeless” quality to them. for example I still have books covering Windows 3.1 and Visual Basic 2.0, as well as an ancient college textbook covering BASIC. I find that these books are far more valuable for information about the topics they cover than even the Internet, so while what they actually cover may be somewhat out-dated, the information and topics they cover particularly with regards to older software is much more difficult to find online than more up to date information, so there is certainly some value there.

Posted By: BC_Programming
Last Edit: 07 Aug 2015 @ 08:44 PM

EmailPermalinkComments (0)
 20 Jun 2015 @ 12:07 AM 

More babbling about BASeBlock. It’s actually rather sad in a way because I would actually prefer to work on my work projects than on my own, simply because there is so much code involved in the changes I want to make to BASeBlock.

My biggest mistake when originally writing BASeBlock was in using the built-in .NET Serialization capabilities. The biggest problem with doing so is that the data stream is not very accessible, data errors are difficult to localize, and it is basically a huge nightmare- not to mention that there is no guarantee of data being portable from one build to the next. This was actually why I created my XML Serialization library a few months ago, “Elementizer”. The aim of Elementizer is to provide XML save/load functionality and have it implementable in a very similar fashion to ISerializable. This is in contrast to the existing XML Serialization solutions I found which tend to rely on saving and restoring public properties, and I do not want to change my object heirarchy and how values are exposed simply so they can be saved/loaded by a XML library. Right now my task is quite monumental- I need to go through and add support for my IXmlPersistable interface to every serializable class in the project. I’m trying to do it peacemeal and do a bit of testing as I go (so I don’t have to just press F5 later and hope it all works!) and while I’ve only got a rather basic amount working it works alright- I’m able to save and load a few block types to and from XML. The real question is going to be what it looks like with an entire LevelSet, which naturally requires quite a lot of other classes to support the feature. The biggest issue was adding support for some less-than-standard data types to Elementizer, such as Bitmaps and certain data types like DateTime and TimeSpans, which themselves implement ISerializable but for obvious reasons can’t implement IXmlPersistable. My solution consisted of using Elementizer’s ability to add deserializers and serializers for specific data types and I just slapped those into the Standard Helper.

It’s a daunting task but it is very sad seeing something that I used to take so much pride it be sort of abandoned in an unfinished state. And I think it is the Serialization that really keeps me from getting motivated- if I can fix that to use XML, I might be more inclined to fix other issues with the program, much like how I started to fix the GameState implementation. And, it will serve as a great example for Elementizer, since the serialization that BASeBlock needs is quite complicated- it needs to save an EditorSet which has lists of sounds and images that are added on and optional music, as well as a LevelSet which has sets of Levels which in addition to their own properties such as music and themes also have a set of various blocks and block types as well as Balls that the level starts with- so I won’t be able to really test saving very well until later.

The one great advantage of the ISerializable Interface implementation is how I was able to utilize it for the Copy-Paste feature of the Level Editor. I actually think the level editor is probably my favourite part of the program, actually.

Posted By: BC_Programming
Last Edit: 20 Jun 2015 @ 12:07 AM

EmailPermalinkComments (0)
 18 May 2015 @ 11:07 AM 

I’ve been, as I mentioned previously, fiddling with BASeBlock recently and I’ve find a few little gems that are worth sharing discussing here.

One of the effects of using GDI+ itself for drawing the game is that all physics and game logic has to be handled by the game. Unlike modern game development frameworks, we don’t get to take shortcuts for things like physics.

This made the creation of the “Polygon” Block a rather tall order, primarily because the math involved is rather advanced, In my opinion. But, I managed to get it working. I also make use of polygons for the purpose of block debris- when a block is destroyed, a number of random polygon debris “breaks” apart the block. I had some fun with that and some blocks break apart differently, such as the “Brick” block which attempts to mimick the classic Super Mario Brothers Brick breaking.

But that isn’t what this is about. One of the requirements of the standard debris (as I call it) is the creation of randomized polygons within a given range of sizes and number of points. These are randomized to prevent them appearing to “regular” since they are used for debris particles.

For generating the polygons themselves, I take a rather straightforward approach. Effectively with a chosen number of points, I use the same logic that is used to generate a regular polygon but I also vary the radius used for each point, which creates a polygon that looks very much arbitrary.

Relatively straightforward, to be honest. It does go to show that trigonometry certainly has uses.

Posted By: BC_Programming
Last Edit: 18 May 2015 @ 11:07 AM

EmailPermalinkComments (0)
Tags: , ,
Categories: .NET, C#
 11 May 2015 @ 11:50 AM 

A bit of a shorter post. Sort of a “progress” report on some of the personal stuff I’ve worked on recently.


I’ve come to form a love/hate relationship with BASeBlock. This is mostly because there are a lot of things I like about it’s design, and a lot of things I hate, and the things I dislike tend to be difficult to change. Some of the basic dislikes includes a lack of DPI support and how it won’t actually scale to larger sizes. On my monitor it is now tiny which is annoying and pretty much trash in the form of an action game. I’ve brainstormed a few solutions. The simplest would be to simply scale up the bitmap that get’s drawn to the screen. That is still a pain but is doable. Another would be to go a step further and actually scale everything in the game itself to larger sizes. That would be a rather large undertaking, to the point that I’m not even sure it would be worth the effort. I made a few minor revisions to try to get it to scale using the first method but ended up shelving that work for the moment. It’s rather disappointing to find such glaring problems with your old projects that you put so much time into, and almost painful to even consider just shelving the project entirely and just moving on. I certainly made a lot of mistakes with BASeBlock but I think it does well for a game using such basic capabilities (GDI+ drawing for a Game is rather unorthodox!).


3-D programming is incredibly annoying and uses math that is far beyond my abilities to actually comprehend. Viewmatrices, rotation matrices, dot products. It’s basically a case of Googling to find out how to do things, then hoping I can figure out how to get the math to work with OpenTK. Nonetheless, I have managed to make a bit of progress.

As with BASeBlock and realistically any game I make going forward most likely, it’s primary purpose is for learning stuff. BASeBlock is at this point “learning” how to refactor an old codebase to improve it, whereas originally it was for learning C#. Prehender is going to be both applying the design techniques I learned since creating BASeBlock as well as being my first 3-D game. With that in mind, it is a rather simple concept.

Originally, I was going to just create some sort of 3-D Block breaker. I have a rather unhealthy fetish with them or something. But I decided to change it up a bit. I decided to “steal” a bit of the design of the 2-D Game, “Spring-up Harmony” which effectively uses a physics engine, and you shoot coloured balls at coloured blocks. If you hit a matching block it will “loosen” from the static background and will interact with other blocks. Then you can catch them with your “bucket” at the bottom of the screen. I haven’t worked out all the details but right now I have it so you shoot coloured balls at an arrangement of cubes, and when a coloured ball touches a coloured block, the block loosens and will fall. I haven’t actually figured out the gameplay specifics, though. That does bring me to the title, though- 3-D programming is quite difficult. I haven’t used Unity before, I may give it a go at some point, however my interest in creating games is typically in what I can learn about actually making them- Unity seems to be more for people interested in making games, as it abstracts some of the parts I find interesting. But in my case, I’m using C# and OpenTK. Unfortunately this means I get the fun of dealing with concepts such as Projection and View Matrices, Dot Products, cross products, and the like. My math also fails me as I’m not able to determine the Camera position from the projection and view matrix, which is a bit goofy when I want to shoot the balls from the position of the camera.

On the bright side, this does make it (IMO) a more useful learning experience. I find it rather strange that I’ve had to resort to third party libraries (OpenTK and BASS.NET) for providing 3-D display and compressed audio capabilities into my C# Program. XNA has been rather left behind (Though still works) and it has a few omissions that I found frustrating when I was working on BCDodger. I would hope that .NET is given first-party support for creating games in the future that makes the task much easier but allows us to use the full power of .NET and C#. Sort of an XNA successor allowing us to also publish to XBox One. (Heck if such a library was made available even at cost I think I could justify an XBox One.)

BCSearch .NET

BCSearch, my VB6 program works, but working on it is pretty much a non-starter these days. I am impressed with the patience I used to have working with Visual Basic 6 only 7 short years ago. Some features of the program will simply not be brought to completion.

Instead, I would like to create a modern WPF Windows Application that uses modern programming (and async await and such) for the same purpose. The effective goal is to create a rather straightforward on-demand search program. This differs from standard Start->Search and the search tool of Windows Explorer in that it is a full application oriented around searches and dealing with the results of those searches. I often find myself trying to find files based on rather specific criteria, and in that context I can see myself using an imagined BCSearch.NET that allows me to write a short C# method in a textbox for filtering each file. This would also allow me to rethink some of the weird architecture decisions I made with BCSearch, while also allowing me to work with WPF again (my work is almost exclusive Windows Forms, and the last time I really worked with WPF was with BCJobClock).

Posted By: BC_Programming
Last Edit: 11 May 2015 @ 11:50 AM

EmailPermalinkComments (0)
 24 Apr 2015 @ 7:21 PM 

BASeBlock was one of my first projects that I wrote in C#. I wrote a program that sort of worked like HijackThis and a INI file reader class- which I Discussed 5 years ago (That long? Amazing), But BASeBlock was my first attempt at creating a project using a language that wasn’t horribly lobotomized. (I refer to Visual Basic 6).

I’ve found myself avoiding it somewhat for quite a long time. It is using code that I have since extracted, repurposed, and improved (such as my update library) for other projects. Another problem is that there are some glaring architecture issues in it’s design (when isn’t there) which were because I was unfamiliar with certain design patterns. For example, my game state management is a switch in the drawing and game tick routines on a game state enumeration, whereas a better design is to use a gamestate interface of some sort and have your different game states compartmentalized better.

However I’ve more recently come to rather like one aspect of BASeBlock. I designed it. I wrote it. While I imagine there are some helper classes and such which I may have acquired elsewhere, the bulk of the implementation is me. There is an aspect of that that I find particularly tantalizing, especially compared to my work. Don’t get me wrong I do love what I do and I have a lot of input in engineering decisions that affect my work, but like any software it is designed to a certain requirement, and with a certain timeline, and a lot of that simply isn’t mutable.

This brought my personal projects into new light for me somewhat when I think of it this way. For some time I’ve only thought of them in the back of my mind as a burden- after working 10 hours dealing with invoice printing considerations, I’ll remember my personal projects and feel bad that they have fallen so far by the wayside they’re stuck in the radiator. But I also forget that, unlike my work- my personal projects have no external director. I can add, change, remove, refactor, and redesign anything I want, and I have to answer to nobody- I’m “in charge” of what they become and how they get there.

Which that in mind I’ve taken it upon myself to actually start progress on making BASeBlock more palatable for me to actually work on. The GameState oddity is like an unsightly mole or other minor annoyance- you don’t notice until you have it pointed out to you (or in my case, until I learned of the pattern, and used it successfully in my poorly named Prehender and BCDodger). It is a major change/refactoring that I wasn’t really keen on doing even when I was working on the game almost daily and had a fairly good understanding of it’s architecture and design, so trying it now is perhaps putting too much faith in not only my abilities but also in my abilities when I was writing it, but if I want to improve it, I need to eliminate that unsightly implementation or I’ll just avoid it until I do.

What is this “GameState” implementation?

In my haste, I did rather forget to explain the particulars of what I was talking about when I talk about “Game States”. Game states are basically what state a game is in and are rather commonplace. A Game will have a different “state” for being paused, showing a menu, showing an introduction sequence, etc. an “Enumeration-based” approach like the one I described might implement things like so (halfpsuedocode)

The basic architecture is to have a switch or similar style of conditional processing within any of the routines which will act differently based on the games current state. This does the task and works as intended, but it tends to lead to long rambly routines, and then all your different states get mixed up in one function. The interface-based approach creates an interface and then each State will be a class that implements that interface. This can be implemented in a number of ways, but the effect is to separate the information and implementation required for each state to a separate class. This allows you to add new features to a state without affecting the “namespace” of other states or interfering with it’s variables, which is sort of the entire point of having separate scope abilities to begin with. It also allows some interesting Chaining abilities. If the Running State stores all the information about game objects, for example, and knows how to draw or tick those objects, a “Slow Motion” state could simply delegate to a provided standard State but only call the tick once for every two calls; a Pause screen could not call the Tick function of a provided composite class, and instead call the draw method and then render a 25% shaded box atop it, or apply a color transformation to wash out the result, or a pause logo of some sort on top to provide the “Pause” effect. An “introductionImage” type state may have a single purpose- you give it an image and a GameState, and it will fade the image in, wait a few seconds, fade it out, and then set the current state to the provided state. Games often show various startup logos, and this can be used to easily fade in and fade out through numerous images and then go to the main menu (presumably a Menu state), and so on and so forth. This can all be implemented, of course, with the enumeration method but it requires a lot of plumbing which also tends to get in the way of everyday development and iterative design.

Posted By: BC_Programming
Last Edit: 24 Apr 2015 @ 07:21 PM

EmailPermalinkComments (0)
Tags: , ,
Categories: Programming
 16 Oct 2014 @ 7:30 AM 

Console applications are fairly common for handling simple, routine tasks; sometimes the task is straightforward and otherwise runs on it’s own with little to no user input. Sometimes it has to generate a lot of output that isn’t really necessary. But inevitably, we find we want to launch one of those Console applications from a Windows program, and in order to integrate well into your program you figure you should do something other than just have the program appear in a console window. Enter Standard handle redirection.

Launching a Process in C# is fairly straightforward:

This is just one way to do it; another common approach is to create the ProcessStartInfo structure and use that as part of Process.Start(); this won’t give you the process object until it launches though- that can be troublesome, particularly if you decide you want to standard output, input, or error streams.

There are a few different approaches to redirecting the standard handles from within the .NET Framework. You can set the RedirectStandardOutput, RedirectStandardInput, and RedirectStandardError properties of the ProcessStartInfo to true, and then manipulate the StandardOutput, StandardInput, and StandardError Streams yourself. This can make for very complex code if you want a responsive design. A better alternative is actually built into the Process class itself- and that is to actually use an event-based approach and have an event fire when and only when one of your redirected output streams has output. There are a few gotchas in this approach that can trip you up if you aren’t careful though. the appropriate ProcessStartInfo setting, RedirectStandard* Property should be set to true; UseShellExecute should be false, and “EnableRaisingEvents” on the process should be set to true. Connect event handlers to OutputDataReceived or ErrorDataReceived, Start the program, and then Begin asynchronous reading of the Output and Error streams with BeginOutputReadLine and BeginErrorReadLine. The events you setup will fire, and e.Data will, for example, contain the line of text that was output. Here is a quick example:

With this you can fairly easily redirect the output and error from a command line (or any other) program, and do with it what you like. You could even parse each output line and present standard input to “emulate” a user running the program. This could be useful for test harnesses as well as for when you use a command line tool but would rather that not be made prevalent- the command line program can be run hidden and you can parse and interpret the standard output as needed to update your UI. (this is in some part how many Linux GUI applications function)

Posted By: BC_Programming
Last Edit: 16 Oct 2014 @ 07:30 AM

EmailPermalinkComments (0)
Tags: , ,
Categories: .NET, C#, Programming
 30 Jul 2014 @ 8:56 PM 

This post is inspired mostly by an interesting post that was posted to the Computer Hope Forum. To summarise, this individual is looking to effectively re-enable Window’s ability to run USB flash drives automatically. There is quite a bit of griping about change, about how the user should be in control, etc. Fairly understandable.

While There is certainly some merit to the idea that a user should be able to configure their system how they want, to implicate that it is not possible is rather silly. Though the software as provided has had that feature removed, there is always the option to write your own solution. Of course without programming expertise, you may be lead to ask for such a program on a forum. In fact, that is exactly what the post is about- It’s asking for a free segment of batch script based on slightly vague but mostly understandable requirements which are unfortunately interspersed in some personal, contrived rant about how things just aren’t what they used to.

Security is not something that should only concern people in top secret facilities, working for governments, etc. It should concern any person that uses a PC. While the ability to toggle an option in order to force USB Flash Drives to autorun (via autorun.inf) like CD-ROM drives when they are detected certainly makes sense from the standpoint of somebody who requires or wants that feature in their everyday computing, it does not make sense at the grander scale. An option exposed to the user is an option any program can change at will.

That is less of a concern, when you think about it- if a program has already run and is able to change those settings, than it doesn’t really need to change those settings- since you are already compromised. And this is true. However software has a habit of interacting in unexpected ways. In fact the very feature being discussed was never explicitly added; the feature was for CD-ROM installations, which in those days were stamped and always provided by the manufacturer. There was little reason to ever suspect such a disc may contain malware, and in those days, making things smoother and easier to use was considered fairly important. With Burned discs, there were further concerns- this is why Windows XP and Vista Display a prompt. The addition of USB Flash drives were the same deal. The biggest issue is simply attack surface. There are a number of attacks which exploit the autorun capabilities that are vulnerable by default. For example, Conficker was one such program. Autorun would automatically install and infect any system that the flash drive was plugged in to. It would then reach across the network and infect further systems, which would infect any flash drives plugged into them, etc etc. Even wit hteh addition of a ‘Default to not enabled, but allow people to disable it” feature only goes so far. OS Security is about reducing attack surface; various exploits could easily trick the system into running the autorun program. A prompt or dialog of options is not a security solution by any stretch of the imagination, particularly if it isn’t running from a secure desktop.

That is, at least, why they have been so far unable to create the functionality they desire, which, as I understand, is through simple modification of a batch script. Eve nso, there are some ways to provide this feature. For one, while it is not possible to enable autorun functionality for anything other than CD-ROMs or DVD-ROMs, this doesn’t prevent Emulated CD-ROM and DVD-ROM Drives from working in that manner. This is how U3 Smart drives work, for example- unfortunately, U3 Smart enabled drives don’t work as-is and would require modifying the protected areas of the Flash Memory. I am unsure if that is possible.

In their postings we see they found and used one alternative, AP USB 47. The claim, however, is that it is very limited for what they are looking for. This seems- odd. It can be configured to automatically launch and it can be configured on all their systems, so it would run just fine for exactly what they want- it supports the same autorun.inf features as well. IIf I was to reply I might inquire as to how it falls short of their requirements, because they didn’t specify how it did so- since it does exactly everything they described.

The best I can gather is that they think the capability should not require running an additional program. This is a rather shallow, and perhaps even ignorant perspective because the capabilities of an OS are usually better kept to a minimum. When Microsoft was loading Windows with bells and whistles, that was, security wise, the worst time for them. And we aren’t just talking about corporations; the fact that any XP machine can be infected, instantly, by plugging in a USB Flash drive is exactly that sort of problem. By removing that capability entirely, it reduces the attack surface by removing the surface entirely rather than trying to coat it with armour-all and hoping the water keeps beading. That is the reason. Whether they want to accept it is their problem, But suggesting that your issue with some very specific requirement involving a side-hobby that has nothing to do with actually running a business is suddenly the problem fo a vendor who’s software is designed for solving real-world problems is somehow of paramount importance and that not being able to do it “makes you sick” is perhaps astigmatic.

We see their requirement, somewhat more pronounced- and particularly why they don’t want to run the program in question, here:

i can save time not clicking buttons. And limiting how much software is installed and starting up on my little duo.

As such we see their requirement is that they don’t want to click buttons (and yet they don’t want to simply launch a program when the drive is inserted, so I’m not sure what they want here) and more importantly they “want to limit how much software is installed and starting”).

That second requirement is always baffling. the Program in question perfectly duplicates the older Autorun.inf behavior- I am unsure why they keep referring to it as autorun.ini, I hope is because they are misspelling in posts and not trying to create the wrong file this entire time. The only reason they don’t want to use it is because they don’t want to use disk space- a full- what, 100KB? ANd they don’t want to use processor power on the extra program running in the background. The first is invalid- disk space of such small amounts is simply inconsequential when you have this sort of issue. And the later is not really valid either. They are working, I believe, on the assumption that more processes means a slower system, which they cannot support with any sort of evidence because no such evidence even exists, because it is not true. Particularly in this case, a program is going to spending almost all of it’s time sleeping- in fact, almost all of it. It is just listening for the same system events that the OS listens for for it’s own autorun features, but instead of ignoring attachments of Fixed disk type drives, it will emulate the standard autorun.inf capabilities and attempt to autorun them.

Thus I am left to conclude that this individual is either a troll- supported somewhat by the fact that their first post is practically the furthest thing from actually making people want to help them, all the while effectively feeling that they are entitled to that assistance because hey it’s the internet, as well as making grandiose false analogies to support their ill constructed perception of “how it should work”, or is simply a poor communicator, because based on what they’ve written I have no idea what tehy want- particularly given that the aforementioned program they already tried, hits every single one of their points; save the one, implied point that is found between the lines.

The problem, in this case, is not with the software they are using. It is, In my opinion, with their approach to what they think is the problem. In their eyes, the problem is clearly the autorun. But why can the problem not be something further back that has led them to somehow require that they be able to arbitrarily plug in USB drives in order to use and run software. This is particularly true since they cannot seem to tell the difference between autorun, the features of AutoRuns (which has nothing to do with autorun at all) and features like the local registry options to auto-start applications- something which ironically is used specifically by the program they reject for specifically the purpose they claim to require. From my gathering, they just want a USB flash drive that triggers an action, and they really don’t need any software on said drive at all. That software is easy to write. I could write it now; just have it register a listener to watch for Device added and device removed events and parse and operate on the autorun.inf contents if present respectively, and irrespective of whether that drive is an optical drive or not.

However, I’m not going to, because that isn’t going to solve the problem, which is a rather loose and I might even say poor understanding of the underlying technologies involved such that their question doesn’t really make completely sense particularly given the rejected options.

Posted By: BC_Programming
Last Edit: 30 Jul 2014 @ 08:56 PM

EmailPermalinkComments (0)

 Last 50 Posts
Change Theme...
  • Users » 46088
  • Posts/Pages » 376
  • Comments » 105
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight


    No Child Pages.

Windows optimization tips

    No Child Pages.

Soft. Picks

    No Child Pages.

VS Fixes

    No Child Pages.