Retrieving the COM class factory for component with CLSID

We have a C# application which built in Windows XP using Visual Studio 2010, when we install this application to a 64-bit Windows 7 PC, we got the following error message in a popup windows:

Retreving the COM class factory for component with CLSID
{0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC} failed due to the following error:
80040154 Class not registered (Exception from HRESULT: 0x80040154
(REGDB_E_CLASSNOTREG)).

More info please read CodeEase.com


About Memory Management in .NET

Today I give a topic which maybe most of us are interested in it,  no matter you are a C++ or C# programmer, or even you are not a programmer, it is from the following questions which most of us always asked:

1: Why theres is no more Pointer in C#?
2: How to release memory in C# if no Pointer ?
3: Can we force to release memory in C# ?

I am not a good article writer and can not speak too much in English. So I try to give you guys simple concepts and simple content:

There is really big differences between C# and C++ about Memory Management.

What C++ did:

In C++, when we allocate a memory to a object or class, we normally have to free it (release) otherwise this memory will NOT released and can not be used by others applications, this is called “Memory Leak”, a professional C++ programmer should always manage his memory related code otherwise your application will “Down” and it might “Down” your windows system also. So Pointer in C++ is powerful and flexible but you have to accept it is also very dangerous .

Manged code and Unmanaged code

In .NET (or Java) , pointer has been removed (you still can use Pointer in C#, actually it is that you embedded your C++ code in C# and indicate C++ code using “Unsafe” word , they are still C++ code );
People call C# and Java Managed Code,  which means some important things have been “Managed” by .NET framework (Java has its framework too), then you know now we call C++ is Unmanaged code.

The one of “Managed” thing is Memory management.

the programming machination changed a lots in C#, The big changes in C# (here we should call .NET, here I use C# instead of .NET ) is that programmer does NOT need to manage memory which he used, well, actually you can say you CAN NOT manage it .  Because the memory allocate and memory release are Managed by .NET .

Garbage Collection:

.NET framework uses AppDomain to give each application, different applications has different AppDomain, .NET manage AppDomain to guarantee each applications can not occupy memory which other applications are using. All memory is managed by a “Model” which is called Garbage Collection  (we can GC) in .NET.

GC has its mechanism to collect “Garbage Memory” (Garbage Memory is the memories unit are no more used by all applications), programmer can load GC.Collect() method to try to “force release memory” but the force will not work as your understanding, and load GC.Collect() is not recommended because it leads unexpected issues.

GC is always running in .NET, it ONLY collect garbages memory under some conditions, for example, here is a condition: when it found the available memory is not enough,  it will try to collection all garbage memory and let them to be reused.

Here we have to know:

1: No matter we use some code to try to force release memory (for example, dispose or even load GC.Collect() method), The GC will NOT work at once, your memory will NOT released at once either, ONLY GC found the “Garbage Collection Time ” comes, then it works to collect garbage memory;

2: Do not worry about GC occupy system time, this is totally a wrong understanding. how can you control GC to stop working ? and you never know what time GC will work.
 (btw: there is no more destruct concept in .NET even you can use it in C#, you only can use Finalize , but Finalize is not Release Memory)

Summary

1: Clarify concept again: Dispose NOT equals Release memory, Finalize NOT equals memory either, there is not a Right Away way to release memory in C#, and actually we DO Not need to concern it because GC is there;

2: ONLY GC can release your memory in C# ; (sure you still can use C++ code to release memory right away)

3: GC is always there, it will NOT effect your application time because it tries to avoid your application time, ONLY it found it has to release memory, or when it found system resource is not enough, or other conditions due to GC’s mechanism;

4: When you use “new” to alloc a local object in a method, when this method end, the memory of the object will be set to wait to be garbage collect, but maybe a good coding habit is setting this object = null or you can load dispose it, actually due to 1., you can not release the memory at once;

About more detail GC, you guys can search on Google, Here is a article from Microsoft below:
 http://msdn.microsoft.com/en-us/magazine/bb985010.aspx

How to display East Asian character in Visual Studio 2008 / 2005

Sometimes I downloaded some C# code from internet but there are East Asian characters involved such as Japanese or Chinese comments. When I tried to open the code, Normally the  Visual Studio 2008 / 2005 IDE did not show correct characters.

I searched out several solutions for this but only the following solution worked for me:

In my Visual Studio 2008:
using “File -> Open -> File… -> Select a file to Open -> (Click the black triangle on the Open button) Open With… ->  CSharp Editor whith Encoding or Source Code (Text) Editor With Encoding” -> try various encodings. such as Chinese Simplified (GB2312) – Codepage 936..

Then I saw correct Chinese characters .

Microsoft Mobile Device Browse File and dotMobi DeviceAtlas

I got a open source project which I think it is supporting by Microsoft and / or dotMobi. the project name is Mobile Device Browse File or Mobile Browser Definition File. Actually I knew dotMobi Inc. has a similar project named dotMobiā„¢ DeviceAtlasā„¢, I don’t know what the differences between these 2 projects, or they are the same ? I am not sure, but I really see there are some differences from their introductions:

1: Mobile Device Browse File:

Project Description
The Mobile Browser Definition File contains definitions for individual mobile devices and browsers. At run time, ASP.NET uses the information in the request header to determine what type of device/browser has made the request.

This project provides a data file that when used with ASP.NET will detect the incoming mobile device and present you as the web developer with a set of 67 capabilities or properties describing the requesting device. These capabilities range from screen size to cookie support and provide all the information you need to adaptively render content for mobile phones and devices.

What is the Mobile Device Browser Definition File?
The Mobile Device Browser Definition File contains capability definitions for individual mobile devices and browsers. At run time, ASP.NET uses this .browser file, along with the information in the HTTP request header, to determine what type of device/browser has made the request and what the capabilities of that device are. This information is exposed to the developer through the Request.Browser property and allows them to tailor the presentation of their web page to suit the capabilities of the target device.

2: DotMobi DeviceAtlas

DeviceAtlas is the world’s most comprehensive database of mobile device information and features…
– Multi-sourced data, for maximum reliability and scope.
– Frequently Updated.
– Market leading performance, with small footprint.
– Transparent, Supported, and Future Proof.

DeviceAtlas for Microsoft ASP.NET BETA consists of a module which will integrate seamlessly with ASP.NET 2.0 and above – requiring just a single configuration line and installation of a DLL. Once installed, the module allows developers to use existing mobile controls with the benefit of new fresh data from DeviceAtlas which currently includes data on over 4,500 mobile devices.

I will get more info about them some day.

Mobile Web Forms in Visual Studio 2008 and 2005

After you install SP1 for Visual Studio 2005 or you are using Visual Studio 2008, you will see that you can not find Mobile Web Forms which you can see in Visual Studio 2003.

The reason is from Microsoft development team. They made the Mobile Web Form Templates a separate package and you need to download the package.

Also, you need to read here to know how to install the package, I copied part of content to the following:

One limitation users should be aware of is the Design View in Visual Studio 2008, which is based on a completely new archtecure, does not display ASP.NET mobile controls.  However, using these templates you should be able to develop ASP.NET Mobile applicaitons using the Source View just fine.

Once installed, these templates enable users to create and edit new ASP.NET Mobile Web Forms in ASP.NET Web Site and ASP.NET Web Applicaiton project types.

To install these templates download the attachment included in this post, extract the ZIP file, and follow the instructions in the included readme.txt files.

And, the most important is HOW TO SEE the Mobile Form Template after you install the package, see the comment below:

When looking into the issue Ahmad reported, we discovered he was looking at the “Add New Item” dialog in a “Website” project, not a “Web Application Project”.  Website projects already have mobile form templates, so these do not apply to that project type. These templates are only for the “Web Application Project” type.  To do a Web Application Project you should select ‘File > New Project’ from the menu and then select “ASP.NET Web Application” as the project type.  In that project selecting “Add New Item” hopefully should show the templates as pictured in the post.  Email me at omark-at-microsoft-dot-com if that doesn’t work for you.

How to add a Uninstall option in Visual Studio Setup project without writing code

Using Visual Studio 2005/2008, you don’t need to write any code to add a uninstall option for a Setup project (Yes I know some people can write code to do it)

1) In the Setup Project –> File System windows –> Right Click “File System on Target machine” –> add a Special Folder, select System Folder;

2) Into this system folder Add a file. Browse for msiexec.exe from local System32 folder and add it. Override default properties of this file as follows:

Condition:=Not Installed (make sure you put ‘Not Installed’ exactly like that, same case and everything),
Permanent:=True,
System:=True,
Transitive:=True,
Vital:=False.

3) Create a new shortcut under the ‘Users Program Menu’, Set Target to the System Folder which you created in the step 1. and point it’s at the msiexec.exe. Rename the shortcut to ‘Uninstall Your Application’. Set the Arguments property to /x{space}[ProductCode].

5) Build the project, ignore warning about the fact that msiexec should be excluded, DONT exclude it or the setup project wont build.

The ‘Not Installed’ condition and Permananet:=True ensure that the msiexec.exe is only placed into the system folder as part of the install IF it doesn’t aready exist, and it is not removed on an uninstall – therefore it;s pretty safe to ignore that warning and just go for it.

(Based on the description from SlapHead)

Fill a pair of data (text and value) to ComboBox .NET 2.0 Windows Application

In .NET 2.0 Windows Application, It is not easy to fill a pair of data (text, value) to a ComboBox, Most of examples just introduced the Display member and value member:

comboBox1.DisplayMember = “Desc”;
comboBox1.ValueMember = “Code”;
comboBox1.DataSource = dt;

Now let us don’t use DataSource and DisplayMember way:

class CmdPairClass
{
private string _cmdLongStr;
public string CommandLongString
{
get { return _cmdLongStr; }
set { _cmdLongStr = value; }
}

private string _cmdShortStr;
public string CommandShortString
{
get { return _cmdShortStr; }
set { _cmdShortStr = value; }
}

public CmdPairClass(string s1, string s2)
{
this.CommandLongString = s1;
this.CommandShortString = s2;

}

public override string ToString()
{

return CommandLongString;
}
}

—————————————————————————-
List<CmdPairClass> aCmdPairList = new List<CmdPairClass>();

public MForm()
{
InitializeComponent();

aCmdPairList.AddRange( new CmdPairClass[] {
new CmdPairClass( “Power”, “PW” ),
new CmdPairClass( “Remote”, “RM”)
});
}

….

for (int i = 0; i < aCmdPairList.Count; i++)
{

this.cbCmdType.Items.Add(new CmdPairClass(aCmdPairList[i].CommandLongString, aCmdPairList[i].CommandShortString));

}

private void cbCmdType_SelectedIndexChanged(object sender, EventArgs e)
{
this.lbMessage.Items.Add(((CmdPairClass)this.cbCmdType.SelectedItem).CommandShortString + “\r\n”);

this.lbMessage.Update();

}