Sam Beauvois: general dev, .net and other stuff

FreeImage and x64 projects : Yes you can !

A few words about FreeImage

According to the website, FreeImage (http://freeimage.sourceforge.net) is an Open Source library project for developers who would like to support popular graphics image formats like PNG, BMP, JPEG, TIFF and others as needed by today’s multimedia applications.

FreeImage is easy to use, fast, multithreading safe, compatible with all 32-bit versions of Windows, and cross-platform (works both with Linux and Mac OS X).

FreeImage is written in C++ but can be used in .NET by using a wrapper (which is provided on the site in the download section)

Using FreeImage in a .NET project

To use FreeImage with a .NET project, you have to include the native C++ library (FreeImage.dll) in the same folder as the executable or in a folder contained in the PATH variable (“%WINDIR%\System32” can be a good choice).
A good way to include the FreeImage dll in the executable’s folder is to add it to your visual studio projet, and set the Build Action property to “none” and the Copy to Output Directory property to “Copy always”

image

You have to add a reference to the wrapper library too, which is “FreeImageNET.dll”.

Now, using the library is easy.

Add an using directive:

using FreeImageAPI;

Check if the library is available :

if (!FreeImage.IsAvailable())
{
   Console.WriteLine("FreeImage.dll seems to be missing. Aborting");
   return;
}

Do something with your pictures:


FIBITMAP myImage= new FIBITMAP();
myImage= FreeImage.Load( [parameters] );

// some smart actions

Don’t forget to dispose your images when finished to use them

if (!myImage.IsNull
{
   FreeImage.Unload(myImage);
}
myImage.SetNull();

Well, this is nice but it still has a problem with that library (there is always one):

And with a x64 project ?

The executable you build has to be the same architecture as the library. The library is x86 so you can use it for win32 applications. What if you have (for some reasons) to build an x64 application?

In that case, you have to rebuild the library to target X64 architectures.

Here is how to do that with Visual Studio 2010 (I assume that it’s more or less the same procedure with other visual studio versions).

Grab the latest FreeImage sources on  the download site (go to the http://freeimage.sourceforge.net/download.html page and click to the link in the Source Distribution section. Once you have the sources, unzip the archive (obvious but…)

In the FreeImage folder there is a visual studio 2005 and a visual studio 2003 solutions, pick the 2005.

image

Convert the solution

image
image
image
image
image

Notice the compilation configuration

image

By default, Win32 is set : we don’t want that !

Go to the Configuration Manager

image
image

As you can see, all FreeImage libs are set to target the Win32 platform, we want 64 bits !

Add a new solution platform

image

Choose to target the x64 platform

image

Just select x64 then click OK

image

Close the manager

Build the solution (keyboard shortcut : ctrl-shift-B).

image

And at this point you will see that there is a problem, yes, you don’t really think it would be that easy ;-)

image

It seems that the compilation encounters a problem especially with the “LibOpenJpeg” project

So, to be sure, try to compile the library one at the same time

image

We see that it is the LibOpenJPEG the big source of problems :

image

A quick Google search allows to find a post on the Google user group for OpenJPEG that helps:
http://groups.google.com/group/openjpeg/browse_thread/thread/64b89cdb1a44bb3b/fd4db2f74c76db87?lnk=gst&q=x64#fd4db2f74c76db87

The problematic file is the “opj_include.h” wich use a keyword non supported by the x64 architecture (the “_asm” keyword if you want to know. It’s because the c++ compiler does not support inline x64 assembly)

image

We have to change :

/* MSVC does not have lrintf */
#ifdef _MSC_VER
static INLINE long lrintf(float f){
 int i;

 _asm{
 fld f
 fistp i
 };

 return i;
}
#endif

to

/* MSVC does not have lrintf */
#ifdef _MSC_VER
static INLINE long lrintf(float f){
#ifdef _M_X64
	return (long)((f>0.0f) ? (f + 0.5f):(f -0.5f));
#else
	int i;

	_asm{
		fld f
		fistp i
	};

	return i;
#endif
}
#endif

Now compile

image

The project compiles pretty well, there is some remaining warnings . . . but it works.

Now you can use that library (mine can be downloaded at  http://www.sambeauvois.be/Demos/FreeImage/x64/FreeImageX64.zip) in your x64 applications

Attention anyway : the library will be slower !

Additional links :

FreeImage download page : http://freeimage.sourceforge.net/download.html
FreeImage API documentation: http://freeimage.sourceforge.net/fnet/Index.html

/* MSVC does not have lrintf */

#ifdef _MSC_VER

static INLINE long lrintf(float f){

#ifdef _M_X64

return (long)((f>0.0f) ? (f + 0.5f):(f -0.5f));

#else

int i;

_asm{

fld f

fistp i

};

return i;

#endif

}

#endif

Change the Office 2010 product key

A few days ago I received Office 2010 from work.

I installed it with the provided product key, and tried to activate it:

image

image

Unfortunately, the product key has been already activated for the maximum number of times permitted.

image

So I asked for a new key, I received it and I tried to change the key

image

After several researches, I couldn’t find an option to change this key.

I finally tried to repair the installation, so I start the office installer

and  the wanted option was here :

image

Choose “Enter a product key” then click on Continue

image

Enter your product key and click on “Continue”.

Then you cant choose if you want to install additional office options

Now it’s activated !

image

The IX509CertificateRequestPkcs10 InitializeFromTemplateName adventure

This week has been researches, tests and headaches to be able to make request on a Certificate Authority server from a web application.

My client has a Win server 2008 CA server for my developments

On this server I have a certificate template named “User Template”

Certificate_server_templates

My assignment was to request a certificate via an ASP.NET application.

After researches on how to do that, I found this blog post that helped me a lot : http://blogs.msdn.com/alejacma/archive/2008/09/05/how-to-create-a-certificate-request-with-certenroll-and-net-c.aspx

In my development environment, I create a request for a certificate by using  this code

 CERTENROLLLib.CX509CertificateRequestPkcs10Class request = new CERTENROLLLib.CX509CertificateRequestPkcs10Class();
 string templateName = "User Template";
 try
 {
    request.InitializeFromTemplateName(CERTENROLLLib.X509CertificateEnrollmentContext.ContextUser, templateName);
 }
 catch (Exception ex)
 {
   log.DebugFormat("Error InitializeFromTemplateName : message {0}, inner : {1}, stack : {2}, source : {3}, target : {4} ",
    ex.Message,
    ex.InnerException,
    ex.StackTrace,
    ex.Source,
    ex.TargetSite);
 }

And it worked just fine !

Then we moved the published solution to the staging environment, and the problems arrived . . .

It didn’t worked !

The error message said:

“CertEnroll::CX509CertificateRequestPkcs10::InitializeFromTemplateName: The requested certificate template is not supported by this CA. 0×80094800 (-2146875392)”

stack :

“at CERTENROLLLib.CX509CertificateRequestPkcs10Class.InitializeFromTemplateName(X509CertificateEnrollmentContext Context, String strTemplateName)”

cererror

We checked the security everywhere ( CA server, Certificate templates, IIS Account, …), give access to everyone on the CA, on the template, … without any success

Then, after days and hours of search and tests, I decided to re-read the method documentation : http://msdn.microsoft.com/en-us/library/aa377533%28v=VS.85%29.aspx

And I noticed the second parameter description:

strTemplateName [in]
Pointer to a BSTR variable that contains the Common Name (CN) of the template as it appears in Active Directory or the dotted decimal object identifier.

I had never tried using the dotted decimal object identifier, so i give it a shot.

I retrieved the object identifier on a certificate previously created with the “User Template”

On the CA server, I had a certificate request with the template “User Template”, so I right-click on it, go to All Tasks and click on the “View attributes/Extensions. . .” menu item.

image

image

A property windows opened, I go to the Extensions tab and click on the “Certificate Template Information” item.

image

In the information panel, I can see the famous dotted decimal object identifier between brackets.

I copy-paste this identifier in my code :

 CERTENROLLLib.CX509CertificateRequestPkcs10Class request = new CERTENROLLLib.CX509CertificateRequestPkcs10Class();
 string templateName = "1.3.6.1.4.1.311.21.8.3531346.8488945.6374567.164989.5001604.52.12582268.10747996";
 try
 {
    request.InitializeFromTemplateName(CERTENROLLLib.X509CertificateEnrollmentContext.ContextUser, templateName);
 }
 catch (Exception ex)
 {
    log.DebugFormat("Error InitializeFromTemplateName : message {0}, inner : {1}, stack : {2}, source : {3}, target : {4} ",
      ex.Message,
      ex.InnerException,
      ex.StackTrace,
      ex.Source,
      ex.TargetSite);
 }

I compiled, I tried in the development environment and it worked !!

(note: there is just a minimal change in my code since I put the “templateName” in a configuration file in order to be able to modify it)

So we moved the published solution to the staging environment,

retrieve the dotted decimal object identifier corresponding to the “User Template” on the staging CA server,

copy-pasted this identifier in the configuration file,

executed a “IISRESET” command and try to execute the application.

And it worked !

We can’t figured out why it worked in the development environment but not in the staging one while both are equals. So If anyone knows about it, please let me know.

So, don’t trust the template name and use the object identifier !

Visual Studio Default Key binding posters

The visual studio 2010 keybinding poster is available since a few weeks.

Visual C# 2010 default key bindings preview :

VS2010CsharpKeyBinding1

VS2010CsharpKeyBinding2

Shortcuts are grouped by category (debuging, refactoring, editing, navigation, …)

Here is the Visual Studio Keyboard shortcuts posters for C#:

Visual Studio 2010

Visual Studio  2008

Visual Studio 2005

Enjoy them !

Custom paging with Subsonic 3 and ObjectDataSource in ASP.NET

Here is how I created a paged items list in ASP.NET 3.5 using Subsonic for data access, and ASP.NET Controls for the display.

I use Subsonic 3.0.0.4 with ActiveRecord and ASP.NET 3.5 SP1 webforms.

For this example, we are creating a blog post list and that the DAL is created (the data class we use is “DAL.BlogPost”).

The DAL.BlogPost object has three properties we need : CreatedOn, Subject and Body.

First step : prepare your data manager class


namespace MyBlog.BLL
{
   public class PostManager
   {
     // ...
   }
}

(keep in mind the namespace and the class for the nexts steps)

In this class, two methods are required,

The first returns the total number of items


public int GetPostCount()
{
  return DAL.BlogPost.All().Count(x => x.IsDeleted != true);
}

The second one returns the items contained in a defined range (start index and then nomber of elements), so this method needs to know where to start and how many items to get.


public PagedList<DAL.BlogPost> GetLastestPosts(int startRowIndex, int maximumRows)
{
   if (startRowIndex != 0)
   {
     startRowIndex =(startRowIndex/maximumRows);
   }

    IOrderedQueryable<DAL.BlogPost> yourQuery = DAL.BlogPost.All().OrderByDescending(x => x.CreatedOn);
    return new PagedList<DAL.BlogPost>(yourQuery, startRowIndex, maximumRows);
}

One important part to remember when using Subsonic is


if (startRowIndex != 0)
{
   startRowIndex = startRowIndex / maximumRows;
}

This conversion is necessary because subsonic and the ObjectDataSource doesn’t share the same opinion about the paging method.
Subsonic says

“I want the third page containing five elements”

ObjectDataSource says

“I want the five elements following the third element”

So the conversion is mandatory if you want a correct paging.

Second step : create the aspx page.

Declare an ObjectDataSource:

<asp:ObjectDataSource runat="server" ID="postsDataSource"
 TypeName="Example.BLL.PostManager"
 SelectMethod="GetLastestPosts"
 EnablePaging="true"
 SelectCountMethod="GetPostCount" >
 </asp:ObjectDataSource>

Note the TypeName attribute : the value is the namespace + the class name of our datamanager class.
We set the EnablePaging attribute to true and we specify the SelectCountMethod and the SelectMethod.

We don’t need any parameters for the paging, by default the MaximumRowsParameterName and StartRowIndexParameterName are set to “maximumRows” and “startRowIndex”.
If we want other parameters names in our datamanager class, we can define them in the ObjectDataSource with these two parameters.

Add an asp ListView element

<asp:ListView runat="server" ID="postsListview"
     DataSourceID="postsDataSource">
 <LayoutTemplate>
    <asp:PlaceHolder ID="itemPlaceHolder"
         runat="server" />
 </LayoutTemplate>
 <ItemTemplate>
    <h2><%# Eval("CreatedOn","{0:d}")%> :
     <%# Eval("Subject") %></h2>
    <br />
    <p>
     <%# Eval("Body") %>
    </p>
    <hr />
 </ItemTemplate>
</asp:ListView>

Set the DataSourceID attribute to the ObjectDataSource ID parameter.

Define a LayoutTemplate and an ItemTemplate with the elements we want to display.

Then add an asp datapager control in the LayoutTemplate


<LayoutTemplate>

  <asp:DataPager ID="postsDataPager" runat="server"
       PageSize="3">
    <Fields>
      <asp:NextPreviousPagerField ButtonType="Link"
           FirstPageText="<<" PreviousPageText="<"
           ShowNextPageButton="false"
           ShowFirstPageButton="true" />
      <asp:NumericPagerField PreviousPageText="..."
           NextPageText="..." ButtonCount="10" />
      <asp:NextPreviousPagerField ButtonType="Link"
           LastPageText=">>" NextPageText=">"
           ShowPreviousPageButton="false"
           ShowLastPageButton="true" />
     </Fields>
   </asp:DataPager>

   <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
 </LayoutTemplate>

Here is the result :
blogposts

Remark about the data access method

It’s possible to avoid the start index value conversion !

Check the PagedList first constructor code (code is available on the subsonic git repository)


public PagedList(IQueryable<T> source, int index, int pageSize)
{
   int total = source.Count();
   TotalCount = total;
   TotalPages = total / pageSize;

   if(total % pageSize > 0)
     TotalPages++;

   PageSize = pageSize;
   PageIndex = index;
   AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
}

As you can see, Subsonic use the Skip and the Take methods from Linq

So, you can modify the GetLastestPosts method to return a strongly typed List in place of the PagedList:


public List<DAL.BlogPost> GetLastestPosts(int startRowIndex, int maximumRows)
{
   return DAL.BlogPost.All().
          Skip(startRowIndex).
          Take(maximumRows).
          OrderByDescending(x => x.CreatedOn).
          ToList<DAL.BlogPost>();
}

With this method, no more conversion needed