Setting WPF Dialog owner from within WinForms application

I had some WPF dialog that I need to show from older WinForms application, or more precisely from some WinForms UserControl. The problem was to set Owner of that dialog, GetWindow() didn’t work (of course) and leaving dialog without an owner, made it possible to disappear when user switch from application to application. And I didn’t want to show it in task bar either.

Thankfully, there is a very easy way, which, for some reason, I didn’t find quite fast – use WindowInteropHelper class. Here is an example:

   1: MyWpfDialog dialog = new MyWpfDialog();
   2:  
   3: //remember, this is WinForms UserControl and its Handle property is
   4: //actually IntPtr containing Win32 HWND.
   5: new System.Windows.Interop.WindowInteropHelper(dialog).Owner = this.Handle;
   6: dialog.ShowDialog();
Technorati Tags: , ,

iPhone 3GS

Go ahead and read it - Stephen Fry’s review at http://www.guardian.co.uk/technology/2009/jun/19/stephen-fry-iphone-3gs-review. Pay attention to the title of the review: “iPhone, therefore I am” – brilliant :)

From my side, I’m sitting here, in Israel and waiting for iPhone to be officially supported by local operators. Seems that at least 2 of them have conformed that in a few months they are getting 3GS version, so we’ll see what the prices will be and maybe I’ll get one for myself too – getting tired of my current Sony-Ericsson P1i.

Technorati Tags: , , ,

Sending E-mail From PowerShell v1.0

While in PowerShell 2 there will be a “Send-Mail” cmdlet (as I heard), I needed something for current version. Here is the self-explaining PowerShell script for sending mails with optional attachments and multiple recipients.

Remember to change the hard-coded “mail.server.com” to your own mail server, otherwise nothing will work :).

##############################################################
# Send e-mail with optional attachments
#
function SendEmail
{
    param(
        $From,
        $To,
        $Subject,
        $Body,
        $Attach = $null
    )
    
    $msg = New-Object Net.Mail.MailMessage
    $msg.From = $From
    
    #Recipient address can be an array as well
    $addresses = $To
    if($To -isnot [Object[]])
    {
        $addresses = ([string]$To).Split(";")
    }
    
    foreach($singleAddress in $addresses)
    {
        $msg.To.Add($singleAddress)
    }
    
    $msg.Body = $Body
    $msg.Subject = $Subject
    
    if($Attach -ne $null)
    {
        if($Attach -is [String[]] -or $Attach -is [Object[]])
        {
            foreach($filePath in $Attach)
            {
                $att = New-Object Net.Mail.Attachment($filePath, "text/plain")
                $msg.Attachments.Add($att)
                Write-Output ([String]::Format("{0} attached", $filePath))
            }
        }
        elseif($Attach -is [string])
        {
            $att = New-Object Net.Mail.Attachment($Attach, "text/plain")
            $msg.Attachments.Add($att)
            Write-Output ([String]::Format("{0} attached", $Attach))
        }
        else
        {
            Write-Warning ([String]::Format("Attachment is of unknown type: {0}. Please use string or string/object arrays only", $Attach.GetType().Name))
        }
    }
    
    $client = New-Object net.Mail.SmtpClient("mail.server.com")
    $client.Send($msg)
    Write-Output "E-mail message sent"
}

Technorati Tags: , ,

Visual Studio 2008 template for Prototype console application

There have been several occurrences where I needed to have something quick UI, to check some set of classes. Building a whole Windows Forms or WPF application seemed to be overkill, specially when I will not be responsible for building UI later, or application will not need UI at all. So what we usually do? Right, create a command line utility, than another one and another one.. Most of them look exactly the same.

So some time ago I have created a Visual Studio project template, that will contain all I need in such console applications, and all I need – just put the code (or calls to another code) for your business logic and check it. Now I have decided to publish it as well, as I’ve been asked to send it few times by mail and people found it useful.

For more info and download link, see full article Prototype Project Template.

Technorati Tags: , ,

Replacing passwords on application logs

Many applications write various information into log files and sometimes this information is sensitive, e.g. contains user passwords. In my case, I knew that we are logging SQL statements with stored procedures calls and one of the parameters may contain user’s password. So I have been asked to replace such passwords with something neutral.

I’ve ended with following regular expression string (works only for stored procedures and alike call logs!):

(?<Pre>(pass(word)?|pwd)[^=']*=[^']*'?)(?<Target>[^']+)(?<Post>'?)

Technorati Tags: , ,

Everquest II News iGoogle gadget updated

I have updated my Everquest II News gadget to strip off all images that were breaking the view. Now the gadget looks more like a regular news feed – all text with a link to full article. Now I probably need to create such feed really and submit it to FeedBurner or something.

image

Update October 25: Go figure - just after I have updated the gadget, SOE made their news available via standard RSS. So I do not need the gadget anymore, woohoo! :)

Access Violation in DsBrowseForContainer on Windows Vista. The code

Since my post about DsBrowseForContainer problem in Vista, I have been asked several times to provide the code on solution. So here we go – below is the class that I use to browse for OU container, it is written to .NET 3.5, but can be easily adapted to earlier versions as well:

public sealed class OUPickerDialog
{
public OUPickerDialog()
{
Caption = "The container picker";
Title = "Select a container:";
}

#region Imports
// all strings in structure will be marshaled as LPWStr [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct DSBrowseInfo
{
public int structSize;
public IntPtr dlgOwner;

public string dlgCaption;
public string treeViewTitle;
public IntPtr rootPath;

// in/out string must be declared as String in struct/class, // not as StringBuilder public string path;
public int pathSize;

public int flags;
public IntPtr callback;
public IntPtr lParam;
public int returnFormat;

public string userName;
public string password;

public string objectClass;
public int objectClassSize;
};

[DllImport("dsuiext.dll", CharSet = CharSet.Unicode)]
private static extern int DsBrowseForContainerW(ref DSBrowseInfo info);

private const int DSBI_ENTIREDIRECTORY = 0x00090000;
private const int MAX_PATH = 256;
#endregion #region Properties
/// <summary> /// Gets or sets text to display on dialog box caption /// </summary> public string Caption { get; set; }

/// <summary> /// Gets or sets title displayed above directory tree /// </summary> public string Title { get; set; }

/// <summary> /// Gets or sets path to start browsing from /// </summary> public string RootPath { get; set; }

/// <summary> /// Gets path selected by user or empty string if nothing has been selected /// </summary> public string SelectedPath { get; private set; }

/// <summary> /// Gets name selected by user or empty string if nothing has been selected /// </summary> public string SelectedName { get; private set; }
#endregion #region Methods
/// <summary> /// Displays a modal picker dialog and returns OU path as selected by user or empty string if nothing /// has been selected /// </summary> /// <returns><c>true</c> if user selected a unit or <c>false</c> otherwise</returns> public bool? ShowDialog()
{
// initialize all members SelectedPath = String.Empty; SelectedName = String.Empty; DSBrowseInfo dsbi = new DSBrowseInfo();

dsbi.structSize = Marshal.SizeOf(dsbi);
dsbi.dlgCaption = Caption;
dsbi.treeViewTitle = Title;
if (RootPath != null)
dsbi.rootPath = Marshal.StringToHGlobalAuto(RootPath);

dsbi.path = new string(new char[MAX_PATH]);
dsbi.pathSize = MAX_PATH;
dsbi.flags = DSBI_ENTIREDIRECTORY;

int status = DsBrowseForContainerW(ref dsbi);

bool? ret = false;
if (status == 1)
{
ret = true;
SelectedPath = dsbi.path;
SelectedName = CalcOUName(SelectedPath);
RootPath = Marshal.PtrToStringAuto(dsbi.rootPath);
}
return ret;
}

private static string CalcOUName(string ou)
{
int idx = 0;
do { idx = ou.IndexOf(@",", idx + 1);
} while (idx > 0 && ou[idx - 1] == '\\');
ou = ou.Substring(0, idx);
return ou;
}
#endregion }

I have also uploaded a testing application I’ve been using. The code can be downloaded from here.

Technorati Tags: ,

Blog is back

Due to several errors during upgrade of blog software, the site was inaccessible in the last few days. After waiting some time for a fix to be available, I have decided to rollback all changes and return to previous version. Now everything should work fine, as usual :)

Technorati Tags:

Aggressive Installers

Aggressive installer is just like aggressive marketing (or guerilla marketing) - it uses inconvenient way to promote some other products. This is what I've been thinking for last few times I've been installing Java updates. When you click on that orange icon notifying you that update is available, you get an installation wizard which "will guide you though the setup process". At some point you are prompted to install OpenOffice or Google Toolbar along with Java, although I don't understand how they are related and why it is already selected by default. As I do not need nor use any of those tools, I unselect it and continue. But imaging an average user, who knows nothing about computers and only uses his own for e-mails or researching online. Speaking of such user, I have several perfect examples - one of them is my mother, who is a psychiatrist and often searching Google (or different online resources) for something she needs. Second is my girlfriend, who just uses mails, sometimes online in-browser games and chats.

Now what they would do when they are going though setup? Right, they just click Next button a couple of times and vu-a-la, the system is up-to-date. With an addition to useless (to my mom and g-friend) OpenOffice and Google toolbar, which make Internet Explorer look like garbage collection and Word documents are now opened in strange-looking application that none of my relatives know about.

What next? Maybe wizard will prompt to install Linux instead of MS Windows? Why not? What is the difference, right? Isn't that the same tactics that EU and Open Source organizations claimed to be used by Microsoft? Microsoft at least added they own software, which is an added value to operating system as a whole - Windows Media Player is an example, without it the system would not be able to play media files, but what we see here? Some organization sneaks another software which adds another piece of junk and makes my system unstable. And I do NOT want to say that OpenOffice or Google toolbar is a piece of junk as is. But in my environment - they do not add anything valuable and instead interfere with already installer programs, which makes them just a plain junk.

Technorati Tags: , , ,

Chinese Simplified is available for DBAdmin

Thanks to George J. Sun (web), Chinese Simplified language pack is available for Database Administrator. The ZIP file containing all languages is updated with the new file. I have to admit - I couldn't test it, as my system don't even display Chinese, so if anyone sees any problem in with the language file (not displayed properly and such), contact me or George to get it fixed :)

As a reminder - DBAdmin is a tool for online managing your MS Access databases, written by me some time ago and using plain ASP (not .NET).

Technorati Tags: , , ,