NetInverse Developers Blog

March 8, 2009
Category: .Net — Tags: , , , , — admin @ 1:00 am

In C++, you can use Win32 WPI FormatMessage to get the system error message. So what is the equivalent in managed code?

It is easy! you can simply use Marshal.GetExceptionForHR.

Marshal.GetExceptionForHR Method (Int32):
Converts the specified HRESULT error code to a corresponding Exception object.

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)

Sample code:

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace hr
{
    class Program
    {
        static void Main(string[] args)
        {
            Exception e = Marshal.GetExceptionForHR(-2147024894);
            System.Console.WriteLine(e.Message);
        }
    }
}

Run above program you will get following system error message from the console:

The system cannot find the file specified. (Exception from HRESULT: 0×80070002)

Please note that when you call a COM object and want to get back the original HResult like S_OK and S_FLASE, you may need to use PreserveSigAttribute Class. Since most COM member return an HRESULT, by applying the PreserveSigAttribute, you can retrieve an integer representing the success or failure HRESULT.

Category: .Net — Tags: , , , , — admin @ 12:35 am

This tool fusion log viewer, coming with Microsoft .Net Framework SDK, is a very useful, but less known. It can display details for failed assembly binds. This information helps you diagnose why the .NET Framework cannot locate an assembly at runtime.

For example, you have written a .Net application and it works on your development machine perfectly. When you deploy it on a testing machine, it throws out exception about assembly binding failure. You know there might be a dependent DLL missing. But how do you figure out which one? use Fuslogvw.exe. It is very possible that in your code you interop some COM components, which have some dependent runtime DLLs are missing on your testing machine. Is it cool?

When you run fuslogvw.exe and it displays nothing, you need to add following flag to the registry.

       HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion

       Add a new DWORD key ForceLog, value = 1

You can check out some more details from this blog: Fuslogvw.exe and diagnosing .NET assembly binding issues. Suzanne Cook’s .NET CLR Notes is also a good resource of Common Language Runtime and binding issues.

Category: .Net, SQL — Tags: , — admin @ 12:12 am

Query Notifications is a powerful new feature, built in to SQL 2005, usable from ADO.NET 2.0 and from ASP.Net 2.0 directly.

Query Notifications allows you send a query to SQL server and request that a notification be generated if any row included in the query is changed.

Commands sent to server may include a tag that requires a notification. Whe the server sees this tag, it will create a notification subscription that fires once the result set is change for the query statement.

This is extremely useful for client side caching of results from database server. With this feature, you don’t have to pulling data periodically.

Category: .Net — Tags: — admin @ 12:10 am

For example, you may have some .Net 1.1.4 COM+ serviced components running under dllhost.exe, and not be able to re-compile them using .net 2.0 for some reasons. You can forcefully run them under .Net 2.0 framework.

You just need to create a dllhost.exe.config file for the %windir%\system32\dllhost.exe. Please note the filename has to be dllhost.exe.config and be placed along with your dllhost.exe. You need this .config file to specify the runtime version you want.

   dllhost.exe.config:

	<?xml version ="1.0"?>
 	<configuration>
	    <startup>
         	       <requiredRuntime version="v2.0.50727" safemode="true"/>
         	       <supportedRuntime version="v2.0.50727" safemode="true"/>
     	    </startup>
	</configuration>
March 7, 2009
Category: .Net — Tags: , — admin @ 10:32 pm

  Can you get the address of a Managed Type? look at the code below:

    
    class Point
    {
        public int x;
        public int y;  
    }
  
    class A  
    {
        unsafe static void Main()   
        {
            Point pt = new Point();
            // Using fixed allows the address of pt members to be
            // taken, and "pins" pt so it isn't relocated.
            fixed (int* p = &pt.x) //This is okay!
            {
                *p = 1;
            }

            fixed (int* p = &pt) //This won't compile.  
            {

                //...
            }  
        }
    }

 Above code won’t compile due to following error:

Cannot take the address of, get the size of, or declare a pointer to
a managed type ('something.Point')

Conclusion: from above sample, you can see that you cannot get the address of a managed type!  But you can point to value types of a class.

By the way, you might don’t know much about fixed statement, I copied the definition of it from MSDN.

fixed Statement (C# Reference)

The fixed statement prevents the garbage collector from relocating a movable variable. The fixed statement is only permitted in an unsafe context.

March 4, 2009
Category: .Net — Tags: , , , — admin @ 11:03 pm

Please read the following sample code with Monitor.Enter and Monitor.Exit. Does the lock work?

class LockMe
{
    private static int counter = 0;
    public void Lock()
    {
       Monitor.Enter(counter);
       counter++;
       Monitor.Exit(counter);
    }

No! The code actually is totally wrong. You are not supposed to lock a value type, which doesn’t have a SyncBlock field. Monitor.Enter(counter) will cause an int(counter) to be boxed first and then you are getting the lock from a box object, not the int itself. The lock won’t work as you expected, since a different object(a different SyncBlock) will be used each time.

« Newer Posts

©2009 NetInverse. All rights reserved. Powered by WordPress