Showing posts with label Tao of Programming. Show all posts
Showing posts with label Tao of Programming. Show all posts

2/02/2010

One or more projects in the solution could not be loaded

You will see a dialog box just like the following when you try to open a .sln file if you using Visual Studio and TFS:


"One or more projects in the solution could not be loaded for the following reason(s):
The project file or web has been moved, renamed or is not on your computer.
These project will be labeled as unavailable in Solution Explorer. Expand the project node to 
show the reason the project could not be loaded."

Whatever you did before, if you can't get your job done, try this way and you'll be lucky:  
1. Check out the .sln file and open it with any your favored text editor.
2. Adjust the following sections in the .sln file to the correct paths and names:
...
Project("{dummy-solution-guid}") = "dummy-project-name", "dummy-project-path\dummy-project-name.csproj", "{dummy-project-guid)}"
EndProject
...
Global
    SccProjectUniqueNameN = dummy-project-path\\dummy-project-name.csproj
    SccProjectNameN = dummy-project-name
    SccLocalPathN = dummy-project-path
EndGlobalSection
...
3. Delete the .vssscc files under the .sln folder and dummy projects folders.
4. Open the .sln file and save it then close it.
5. Re-Open the .sln file and check it in TFS.


6/03/2009

How to re-add database objects into EDMX

When you working on ADO.NET Entity Framework in VS2008, may be there are some things wrong or other else you'll need to re-add database objects into your EDMX. 

You can try the following steps:
1. Delete the Entity from the EDMX Designer's Diagram. Because that is the only way to delete an Entity from EDMX, I don't know why not MS add this function into Model Browser in VS2008.
2. Now, you can see the Entity that had been deleted exists in Model.Store yet. So, you cannot re-add it via 'Update from Database...' .
3. Open EDMX through XML Editor (Right-Click EDMX item in Solution Explorer>Open With...>XML Editor), find the Entity in <edmx:StorageModels> sections, then delete it.

Now, you can re-add database objects into your EDMX.

8/17/2008

How to reference WPF types in XAML

1. If the custom WPF types had been defined in your WPF application, you can reference it in your XAML by adding the BOLD line append to XAML root element:
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:WpfApplication1"
    Title="Window1">
...
</Window>

2. If the WPF types had been defined outside your WPF application, you can append "assembly" clause to "clr-namespace:xxx" and spearate "clr-namespace" and "assembly" clauses by semicolon:
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom2="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1"
    Title="Window1">
...
</Window>

3. The <?Mapping XmlNamespace="http://your-domain/your-type" ClrNamespace="your-wfp-app-namespace" ?> does not work again that
Ian Griffiths and Chris Sells introduced it in [Programming Windows Presentation Foundation ].

5/03/2008

How to partition a USB Flash Drive

Goals:
Partitioning a USB flash drive into 2 partitions. One used to general storage task and it will be supported on both the Windows and Linux; And another partition will be only supported on Linux, then you can use this partition to install a USB-bootable Linux.

Requirements:
1. A runnable Linux operating system. At here, I used Ubuntu 8.04.
2. fdisk: a partition table manipulator for Linux that existing on general Linux system.
2. A USB flash drive.

Step1. Find the USB flash drive device.
sudo fdisk -l
On my machine, I had the following output like this:
Disk /dev/sdb: 523 MB, 523091968 bytes
17 heads, 59 sectors/track, 1018 cylinders
Units = cylinders of 1003 * 512 = 513536 bytes
Disk identifier: 0x6f20736b

So, I assume you had same the device with my which is the /dev/sdb.

Step2. Create the one partition for general storage.
sudo fdisk /dev/sdb
2.1 Use "d" command to delete all existing paritions. First, use "p" command to print the partition table, get the partition number, then repeatedly use "d" command to delete if you have more than one existing partitions. My partition table look like the following output from "p" command:
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         718      360047+   6  FAT16
/dev/sdb2   *         719        1018      150450   83  Linux

Then use "d" command to delete:
Command (m for help): d
Partition number (1-4): 2
Command (m for help): d

2.2 Use "n" command to create the partition.
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1

First cylinder (1-1018, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1018, default 1018): 509

After above commands had been done, I got a new partition:
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         509      255234   83  Linux

2.3 Use "t" command to change the partition's system id.
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): b

Finally, the partition has been done, but at here this partition is not compatible with Windows yet.
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         509      255234    b  W95 FAT32


Step3. Create a bootable Linux partition.
Reference Step-2 to create a Linux partition, but use "a" command instead of "t" command that introduced in Step2-3. The "a" command used to toggle partition is bootable.
Command (m for help): a
Partition number (1-4): 2

The final output like this:
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         509      255234    b  W95 FAT32
/dev/sdb2   *         510        1018      255263+  83  Linux


Step4. Make the general storage partition is compatible with the Windows.
Command (m for help): c
DOS Compatibility flag is not set

Command (m for help): c
DOS Compatibility flag is set


Step5. Commit all work that you had done.
Command (m for help): w

If you do something wrong, you can rollback all your work via "q" command.

Step6. Format the Linux partition to ext3.
This step is optional if you like ext3 file system.
sudo mke2fs -j /dev/sdb2

3/02/2008

An easy way to add Chinese language support for Ubuntu 7.10 English local

1. Add Chinese language support:
sudo apt-get install language-pack-zh language-support-zh language-pack-gnome-zh
2. Install Arphic fonts:
sudo apt-get install ttf-arphic-uming ttf-arphic-ukai ttf-arphic-gbsn00lp ttf-arphic-gkai00mp ttf-arphic-bsmi00lp ttf-arphic-bkai00mp
3. Make a file named local.fonts in /etc/fonts/
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<!-- /etc/fonts.conf file to configure system font access -->
<fontconfig>
 <!--  Enable sub-pixel rendering
       <match target="font">
               <test qual="all" name="rgba">
                       <const>unknown</const>
               </test>
               <edit name="rgba" mode="assign"><const>rgb</const></edit>
       </match>
        -->
 <alias>
   <family>serif</family>
   <prefer>
     <family>Bitstream Vera Serif</family>
     <family>AR PL ZenKai Uni</family>
     <family>AR PL ShanHeiSun Uni</family>
     <family>AR PL SungtiL</family>
     <family>AR PL KaitiM GB</family>
     <family>AR PL Mingti2L Big5</family>
   </prefer>
 </alias>
 <alias>
   <family>sans-serif</family>
   <prefer>
     <family>Bitstream Vera Sans</family>
     <family>AR PL ZenKai Uni</family>
     <family>AR PL ShanHeiSun Uni</family>
     <family>AR PL SungtiL</family>
     <family>AR PL KaitiM GB</family>
     <family>AR PL Mingti2L Big5</family>
   </prefer>
 </alias>
 <alias>
   <family>monospace</family>
   <prefer>
     <family>Bitstream Vera Sans Mono</family>
     <family>AR PL ZenKai Uni</family>
     <family>AR PL ShanHeiSun Uni</family>
     <family>AR PL SungtiL</family>
     <family>AR PL KaitiM GB</family>
     <family>AR PL Mingti2L Big5</family>
   </prefer>
 </alias>
</fontconfig>

4. Restart computer.

2/15/2008

Remove crashed SQL Server 2005 Express installation

If u cannot uninstall the instance of SQL Server 2000 Express installation by general methods, u can try the following steps:
1. Backup all ur data.
2. Stop and then delete all SQL Server services.
sc delete mssqlserver
sc delete mssql$sqlexpress
sc delete mssqlserveradhelper
sc delete sqlbrowser
sc delete sqlwriter

3. Delete the local security groups for the SQL Server 2005 componments.
4. Uninstall VSS Writer, Native Client componment, and Setup Support Files componment.
5. Renamed/Removed the installation folder.
6. Run Install Cleanup utility and delete all SQL Server 2005 list items.
7. Restart computer.

2/08/2008

How to disable Windows Live OneCare?

1. Disable Windows Live OneCare, run as administrator to run the following commands:
sc config msfwhlpr start= disabled
sc config msfwdrv start= disabled

sc config msfwsvc start= disabled

sc config mpfilter start= disabled
sc config onecaremp start= disabled
sc config winss start= disabled

then restart the computer.

2. Enable Windows Live OneCare, run as administrator to run the following commands:
sc config msfwhlpr start= system
sc config msfwdrv start= auto

sc config msfwsvc start= auto
sc config mpfilter start= auto
sc config onecaremp start= auto
sc config winss start= auto
then restart the computer.

1/26/2008

Remove list item from Vista Programs and Features

After uninstalled VS2005, I try to uninstall Microsoft Silverlight 1.0
SDK Visual Studio 2005 Template via Vista Programs and Features. But,
the uninstalling process indicates me that requires VS2005 has been
installed. It's simple to remove the associated files with Microsoft
Silverlight 1.0 SDK Visual Studio 2005 Template, but how to remove the
list from Vista Programs and Features and Windows Registry.

There are two ways to resolve this:
1.Utilize the Windows Install Clean Up utility.
But be care for, the Windows Install Clean Up utility does not remove
the files of any programs that are installed by Windows Installer.
2.Remove the Windows Registry items by hand.
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{AFD5020D-65CE-4C4D-B6D8-6AAC29A4A95C}]

12/26/2007

VS2005 and VS2008 .sln file icon incorrect

I had installed VS2005 and VS2008 on Vista Business, both work fine. But today, I found .sln file icon losted and cannot open it with double-click. So I associate .sln with Visual Studio Version Selector, it works again, but the file icon is incorret.

I spent all the afternoon to make a ton of mistakes, the final solution is: associate .sln with <osroot>\Program Files\Common Files\microsoft shared\MSEnv\VSLauncher.exe.

The headache had been resolved, but I cannot find the reality. The conclusion is more bigger and more danger.

12/25/2007

Pseudorandom number generation enhanced in VS2005

1. Define the _CRT_RAND_S macro before the #include <stdlib.h> .
2. Does not need call srand function to generate the seed.
3. rans_s now has returned value which is type of errno_t, zero if successful.
4. Use this formula: dst_num = (unsigned int)((double)rand_num / (double)UINT_MAX * rand_max + rand_min) to specify the range[rand_min, rand_max] .


12/16/2007

A small example of compute Fibonacci's formula statically in C++

static unsigned int const value = Fibonacci<N-1>::value + Fibonacci<N-2>::value;
};

template<>
struct Fibonacci<1> {
static unsigned int const value = 1u;
};

template<>
struct Fibonacci<2> {
static unsigned int const value = 2u;
};

int main(int argc, char *argv[]) {
unsigned int v3 = Fibonacci<3>::value;
unsigned int v9 = Fibonacci<9>::value;

return (0);
}


12/11/2007

A sealed class in C++

I want a 'sealed' class in native C++ just like 'sealed' in C++/CLI and C# or 'final' in Java. I googled it then got following solution.

template<typename T>
class DeriveLock {
private:

DeriveLock() {}
~DeriveLock() {}
friend T;
};

class SealedClass : virtual public DeriveLock<SealedClass> {
};

You can found above sample code at Bjarne Stroustrup's article.

10/31/2007

LNK2028, LNK2019 without extern "C"

I wrote a Win32 dll in VS2005 and a CLR-Console client, the CLR-Console client is the caller of Win32 dll. In the Win32 dll, using /TC compiler option to build it and using /clr option to build the CLR-Console client. The all things is fine, such as 'dllexport/dllimport', 'Additional Include Deirectories' and 'Additional Dependencies'. But I got LNK2028 unresolved token and LNK2019 unresolved external symbol, the reason is I forgot using the extern "C" to include all dllexport functions.

10/23/2007

Reversing string in C

There is a example for reversing string in the Section 3.5 of K&R's 'The C Programming Language' . The example code look like this:
void reverse(char s[]) {

char c;
int i, j;

for (i = 0, j = strlen(s) - 1; i < j; ++i, --j) {
c = s[i], s[i] = s[j], s[j] = c;
}
}

But the above code can be improved. There is 'Pointer Version':
void reverse(char *s) {
char *l = s;
char c;

while (*s++)
;
s -= 2;

while (l < s) {
c = *l;
*l++ = *s;
*s-- = c;
}
}

The *strrev* in C standard library implements the same functionality, but generally implements in assembly code.

10/20/2007

Item not found: Could not find this item! on Vista

When u try to remove a directory under the Vista GUI, u'll get 'Item not found: Could not find this item'. Or, u'll get 'The system cannot find the file specified' under the Vista command prompt.

Try the following:
1. Open the command prompt.
2. Execute *dir /x* to display 8.3 file name
3. Execute *rd filename* the filename 8.3 format that u got it in step 2.

10/18/2007

The #end joke

I found a joke when I publish my blog through the gmail. Wrote a blog which includes a piece of C/C++ code. Within that, the *# e n d i f* C/C++ directive had been used. So after send my blog, I got truncated blog content, Ha.

Because, u can use the *# e n d* flag in ur gmail in order to indicate blogger to truncate the content after *# e n d* flag. It's a joke.

Organize IA-32 machine code in C++ class

First, look the following code:

#include <iostream>

#pragma pack(push, 1)
class MachineCode {
public:
unsigned char jmp_;
unsigned long proc_;
};
#pragma pack(pop, 1)

typedef void (__stdcall *ProcCall)(void);

class Adapter {
public:
int Init(void* proc, void* pthis) {
thunk_.jmp_ = 0xE9;
thunk_.proc_ = (unsigned long)proc - ((unsigned long)pthis + sizeof(thunk_));

::FlushInstructionCache(::GetCurrentProcess(), pthis, sizeof(thunk_));

return (true);
}

ProcCall GetProcCaller(void) {
return (reinterpret_cast<ProcCall>(&thunk_));
}

static void DefProcCall(void) {
std::cout<<"in Adapter::DefProcCall.\n"<<std::endl;
}

private:
MachineCode thunk_;
};

Then, I use the following code to call X::DefProcCall function:

Adapter x;
x.Init (&Adapter::DefProcCall, &x);
ProcCall call = x.GetProcCaller();
call();

The all magic is at 'reinterpret_cast<ProcCall>(
&thunk_)' in GetProcCaller member function of Adapter class and two data members in MachineCode class. Directly, generates such IA-32 code 'call dword ptr [function-address]' when C++ compiler meet 'call()' statement. Because of the function-address exactly is the address of the thunk_ which is the first data member of Adapter class. So the machine code in thunk_ has been executed.

The jmp_ data member of MachineCode class stores 0xE9 that is 'jmp' instruction in IA-32. And the proc_ data member stores relative address since the ending address of the thunk_ which in Adapter class. So whole MachineCode class generate such IA-32 code 'jmp address'.

10/16/2007

[] Operator in IA-32 assembly code

[] bracket operator means get the content of the memory location, u can treat [location] as addressing expression.

The following C code:
int i;
i = 0x1234;

can be translate to assembly code which looks like this:
mov dword ptr [i], 1234h

the dword ptr means the content of variable i occupied 4 bytes. It is required because of IA-32 is byte addressable. And above mov instruction does not know how much storage requires for storing immediate number 1234h.

10/15/2007

Cannot install Microsoft Server 2003 SP2!

*update.inf*

1. net stop cryptsvc
2. ren %systemroot%\system32\catroot2 old-catroot2
3. net start cryptsvc

10/14/2007

Manipulate bits in C

1. Definition:
const int FIRST = 1 << 0;
const int SECOND = 1 << 1;
const int THIRD = 1 << 2;

Why defines the bit flag like this? Oh, just for clarity.

2. Setting bit flag:
int flags = 0;

flags |= FIRST;
flags |= SECOND;
flags |= THIRD;

if (flags & FIRST) {
printf("FIRST bit has been set.\n");
} else if (flags & SECOND) {
printf("SECOND bit has been set.\n");
}
...

3. Clearing bit flag:
flags &= ~FIRST;
flags &= ~SECOND;
...