2009年3月19日星期四

Using Resources in .NET

我有个C++.NET程序。
在修改Form自带的resx文件时会弹出 警告菜单"You are trying to edit a resources file that ..."
如果强制进行修改,当对Form进行其他修改后,resx会更新,手工进行的修改就会消失。
这是Visual Studio的一个BUG。
但是怎样添加自己想要的resources呢,如运行时载入的图片等等。
这里是一个可行的步骤:

1. 在名为MyProject的Project里添加MyResources.resx
2. 在MyResources.resx中添加你想要添加的Image如MyImage.png等resources资源文件。
注意,图片导入后,后缀就消失了。
3. 在代码中添加如下的代码进行操作:
using namespace System::Resources; // for ResourceManager and MissingManifestResourceException
using namespace System::Reflection; //for Assembly::GetExecutingAssembly()
try{
ResourceManager^ resources = gcnew ResourceManager("MyProject.MyResources", Assembly::GetExecutingAssembly());
this->toolStripMenuItem1->Image = (cli::safe_cast(resources->GetObject(L"MyImage")));

}catch(MissingManifestResourceException^ ex){
MessageBox::Show(ex->Message);
}
上述代码中的MyProject.MyResources是对应的MyProject.MyResources.resources文件名,这个文件在编译后存在于你的Debug和Release文件夹中,如果名字写错了,将会抛出MissingManifestResourceException异常。

好,祝你好运。

2009年3月14日星期六

Is it possible to deploy a .NET exe without installing .NET framework

在没有安装.NET Framework的平台上运行.NET程序,问题是可能吗?
答案是:没有不可能。
C#的出现是为了与Java抗衡,和Java一样,需要安装虚拟机平台来运行。如果没有CLR,似乎是不行的。但是如果将所有需要的dll都拷贝过来,将其他的所有设置相关的内容都拷贝过来,集成到exe中,不就可以了吗?

怎么做呢?Google Thinstall to Check the detail.
其他的包括

Static Compilation in Mono

Xenocode(Deploy your .NET app securely, with no Framework install ),
Remotesoft(Salamander .NET Linker, Native Compiler and Mini-Deployment Tool)

Thinstall软件已经被VMWare收购了。

.NET framework virtualization

Deploy and run .NET applications - with out installing the .NET framework. Run applications that require different versions of .NET, concurrently, without conflicts and protect code from dissassembly.

Challenges

Developing with the latest release of the .NET framework offers clear advantages. However, deploying an application requiring the latest .NET can present challenges due to a number issues including:

  • Other applications require an earlier version of .NET
  • Locked down desktops can not be upgraded immediately
  • Computers may need to have different versions of .NET installed concurrently

Whether a commercial application distributed to home computers or a custom application to corporate locked down computers, the correct version of the .NET framework - or any of the .NET components - may not be installed. This presents challenges in terms of updating, running, and installing applications and also problems when applications require different .NET versions to be installed.

VMware ThinApp Solution (Formerly Thinstall)

ThinApp provides essential features that make .NET deployments conflict free and secure. Benefits include:

  • Not requiring the .NET Framework to be installed. By packaging the application and linking the .NET framework with ThinApp, no installation of the .NET framework is required. The application runs without modifying the host computer.
  • Eliminating all installation errors and conflicts. ThinApp enables applications to run concurrently that each require a different version of .NET to be installed.
  • Reducing download and installation time. ThinApp packages all of the .NET framework or only the components that the program needs into a single, compressed EXE. This not only reduces the size of the components that need to be installed (and downloaded), but also makes ‘installation’ significantly faster. Nothing is installed, rather the program is run via the ThinApp Virtual Operating System.

Additionally, even applications created for internal corporate use at some point may be distributed externally. .NET applications can be easily disassembled with readily available developer tools that make inspection of source code easy, enabling hacking and reverse engineering relatively simple for any experienced programmer.

ThinApp also protects against hacking and disassembly. Rather than using obfuscation, ThinApp instead encrypts the .NET Program inside of secure loader which prevents disassembly. The resulting EXE no longer appears to be a .NET Program so codedisassemblers are unable to decrypt the program stored inside.

2009年3月4日星期三

error C2143: syntax error : missing ')' before 'constant'

翻译为汉语就是:在常数的前面缺少')'
例如,你在某处 #define A 1
但是在程序中某处又忘记了常数的定义,而使用了int A;等,就会得到这个错误。

一个可能的原因:Additional information: Cannot access a disposed object.

编译正常,但是在执行CSharp程序的时候却出现了Additional information: Cannot access a disposed object.

检查错误,是自己在Load程序中新建了一个Form对象,使用了Show(),改为ShowDialog()后正常。

Built successfully, but fail to run.
change the frmSomething.Show()
into frmSomething.ShowDialog();
Things are OK.