August 27, 2012

Named pipes in .Net


Recently I did a job involving sending SMS messages from a web service. The message application app is SMS Studio, from CodeSegment. This application is used for some time by our client and in the past we've only used VB Script to interface with it.
Using VB Script, the easy way to send an SMS was to make SMS Studio create a named pipe to receive incoming messages and 'place' the SMS content in that pipe by our script.


My problem

Seems straight forward. In vb script we create an fso object and write to the pipe. Should be as easy -if not easier- in .Net right? Wrong! The Filestream object in System.IO does not allow your to write to a 'device'. Anything named "\\.\[device]\[name]" is not allowed. Apparently "pipe" is considered a device... "\\.\pipe\SendSMS" dit not work..

Problem Solved?!

Making a long story short: What I was looking for was here: System.IO.Pipes. It is available as of  framework 3.5.

This is how we did it in vb script:
 Set fso = Server.CreateObject("Scripting.FileSystemObject")  
 Set pipe = fso.CreateTextFile("\\.\pipe\SendSMS")  
 pipe.Write(msgText)  
 pipe.Close   

And the .Net way:
 Try  
    Using pipe As New NamedPipeClientStream(".", "SendSMS", PipeDirection.Out, PipeOptions.Asynchronous)  
      pipe.Connect(2000)  
      Using sw As New StreamWriter(pipe)  
        sw.AutoFlush = True  
        sw.Write(msgText)  
      End Using  
    End Using  
 Catch ex As Exception  
    Messagebox.Show(ex.Message & Environment.Newline & "SMS Studio offline?", "SMS not send")  
 End Try  

Notice that the pipe name in .Net is only the name "SendSMS", not the 'full' name ("\\.\pipe\SendSMS") like in vb script! Also we use NamedPipeClientStream, because we 'connect' to an existing pipe. It turns out to be this simple.


August 01, 2012

Visual Studio; The Document outline window

I really love Telerik controls especially in a winforms and asp.net environment. In a windows form solution it gives you this visual edge where your apps just looks a little more professional and appealing. RadDock is by far my most favorite control, closely followed by RadCommandBar.


My problem


Okay, so Telerik did a great job with those winform controls. But the problem is the order I drag my controls from the toolbox to my form. Usually I add a RadCommandBar first docking it to the top of the form, followed by a RadDock docking the form. Occasionally I first add the dock then the bar, resulting in the bar overlaying the dock.

Not the desired end result...



Problem solved?


Sort of... Usually I went to the forms designer code behind and changed the Me.Controls.Add order.



Problem Solved!


But by chance I stumbled into a Visual Studio feature which is far more easy: The document outline window! A -almost "hidden"- window showing all controls on your form. Rearranging controls is reduced to drag'n'drop and clicking arrows. No more need to edit designer code behind.



To show this window from the main menu, select View -> Other Windows -> Document Outline