Friday, May 18, 2012

ClickOnce Hell

This post is just a comment on my basic impression of ClickOnce, after having lived through developing for it for several years.

If your application is relatively small and simple, ClickOnce is great.  Basically, one (maybe two or three) project in the solution, compile size relatively small (to handle slow connections if necessary).

The problems start to arise when your solution starts to get complex.  Multiple projects, with references in XAML crossing the projects, and it gets a bit tricky.  Stuff that works fine in Visual Studio tends to not work once deployed through ClickOnce, unless you are very careful in your reference definitions.  It only takes one misdefined definition, and the whole thing can break--and the error messages received will likely be too generic to easily find it.

So with a complex application, ClickOnce becomes completely unfeasible.  In my opinion, it's a piece of crap, unless you keep your application small and simple--then it's great.

I use it on my small and simple applications as it offers easy deployment.

But on the one complex application I support, originally it was deployed as ClickOnce, but at some point the ClickOnce deployment broke, probably due to an incorrectly defined reference.  The application worked fine in Visual Studio, or when installed using WiX.  But it would go so far in ClickOnce, then blow up, and I never could figure out what ClickOnce didn't like.

So it was easier to give up on ClickOnce and switch to installing with WiX and use our own home-grown mechanism for updating the application.  That home-grown mechanism had its own set of problems, but at least it was under our control.

As a final note: WiX is great.  Being driven by XML allowed automatic update of the project definition through Team Systems Build.  If  I get around to it, I might post the recipe for this.  Leave a comment if this recipe would be valuable to you (to encourage me to post it sooner).

Assembly Unloading

I needed a simple way of checking version information of an assembly that was not loaded into memory.  On System.Reflection.Assembly, there is the Load method, but unfortunately, now the assembly is in memory with no way of unloading it--eating up precious RAM.

However, .NET offers the AppDomain.  Simply create a new AppDomain and load assemblies into that for reviewing information of the assembly, then unload the domain.

Example:


System.AppDomain newdomain = System.AppDomain.CreateDomain("DataVerification");
Assembly assm = newdomain.Load("MyAssembly.dll");
MessageBox.Show("Assembly version: " + assm.GetName().Version.ToString();
System.AppDomain.Unload(newdomain);

Tuesday, April 17, 2012

Perceived Types


Never could figure this one out complete.  We have an application that looks in the ClassesRoot Registry for the extension of a file, looking for the "PerceivedType" entry, and rendering an image if the PerceivedType is "image".  The problem for this particular computer was that its initial configuration was bad, and it had no extensions configured to image.  No idea why, nor how to automatically fix it or prevent it going forward.

Simple solution was to create a Registry merge file that included all extensions and added the "PerceivedType"="image" to all of them.  However, I stumbled onto a Windows Shell API call that would first check some hard-coded extensions for their perceived type, then check the registry, so there was potential that if our application used this call, AssocGetPerceivedType, the problem may not have surfaced in the first place.

But I could not find an example of the call defined in C# anywhere.  What a pain!  You mean no one has written C# that needs this call???????

I found C declaration for the call:

HRESULT AssocGetPerceivedType(
  __in       PCWSTR pszExt,
  __out      PERCEIVED *ptype,
  __out      PERCEIVEDFLAG *pflag,
  __out_opt  PWSTR *ppszType
    );

Now the question was, how to get it to C#?


It wasn't too hard, but did take some digging.  Below is the full code I used to get the perceived type of a file.  Simply check the returned value from the GetPerceivedType function below, passing the extension:






        [DllImport("Shlwapi", EntryPoint = "AssocGetPerceivedType")]
        private static extern int AssocGetPerceivedType(IntPtr extension, out IntPtr PERCEIVED, out IntPtr flag, out IntPtr PerceivedType);
        public string GetPerceivedType(string extension)
        {
            IntPtr result = IntPtr.Zero;
            IntPtr dummy = IntPtr.Zero;
            IntPtr intPtr_aux = Marshal.StringToHGlobalUni(extension);
            AssocGetPerceivedType(intPtr_aux, out dummy, out dummy, out result);
            string s = Marshal.PtrToStringAuto(result);
            return s;
        }
        

Tuesday, January 31, 2012

Axiom of Truth


If it takes two contractors six months to drive a project to failure, four contractors will achieve that failure in half the time, but it will take twice as long for the failure to be acknowledged.

Tuesday, January 10, 2012

Filter for all supported Image file types

Nifty little trick: for the OpenFileDialog, I want to set a filter for all Image file types supported by the PC, including ones that haven’t been invented yet. My code will load it into some sort of Image control for displaying. I could manually set my filter to something like “*.jpg;*.png”, etc., but then I’d have to update it whenever a new format was introduced, and some PCs may support types not supported by others, due to what has been installed on the PC.

This code below solves this problem.  It uses the MIME types that are configured for the PC, as defined in the Windows Registry.  Nice and simple solution.




  public static string GetImageFormatsFileFilter()
        {
            string retVal= "*" + string.Join(";*", SupportedImageFormats());
            return retVal;
        }
        public static string[] SupportedImageFormats()
        {
            List retVal = new List();
            RegistryKey BaseKey = Registry.ClassesRoot.OpenSubKey("MIME").OpenSubKey("Database").OpenSubKey("Content Type");
            foreach (string subkey in BaseKey.GetSubKeyNames())
            {
                if (subkey.ToUpperInvariant().StartsWith("IMAGE/"))
                {
                    string ext = (string)BaseKey.OpenSubKey(subkey).GetValue("Extension");
                    if (ext != null && !retVal.Contains(ext.ToUpperInvariant()))
                    {
                        retVal.Add(ext.ToUpperInvariant());
                    }
                }
            }
            return retVal.ToArray();
        }