Thursday, October 25, 2007

.NET DateTime Objects Does Not Compare Correctly

Environment: C#


You have parsed a date time string and made a DateTime object, then you wanted to compare that with current time using DateTime.Now, but it does not work.

DateTime t1 = DateTime.Parse("27 October 2007 10:27");

if (t1 > DateTime.Now) { /* do something */ }


The parsed DatTime object has the Kind attribute set to UNKNOWN while DateTime.Now is set to LOCAL. Comparison between the two will not work as expected.


There may be other ways of doing this, but here is the one that made my code to work.

DateTime t1 = DateTime.Parse("27 October 2007 10:27");
t1 = DateTime.SpecifyKind(t1, DateTimeKind.Local);
if (t1 > DateTime.Now) { /* do something */ }

Time Wasted:

Took me about 2 hours to figure out what is exactly going on and how to fix it.

Monday, October 15, 2007

Unable to Load DLL Error


You built an application using Visual Stuido 2005, you also have a Platform Invoke code so that your managed code can call unmanaged code stored in your platform invoke DLL, and in order to test it on another machine, you copy the bin/debug folder there. The test machine does not have the Visual Studio 2005.

As soon as you run the program, and when your code calls the platform invoke code the following error message occurs,

Unable to load DLL 'Some.DLL': This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)

Possible Cause and Fix:

This article is still being completed. But here is what I have figured out so far:

Apparently, when you build a code that includes a platform invokaction code (i.e., calling a C++ library), there is a run-time (CRT) code to be included, and if you are doing this on a debug code then there are some very complex issues.

There is one person on the web that solved this issue by using the release build. I am going to try that approach.

If that's not what you want, here is a detailed information about this.

(This article will be updated further as I find the final solution.)