Posts in .Net Framework

Troubleshooting SGEN : error : An attempt was made to load an assembly with an incorrect format

I have a POCO entity project that must generate XML Serializers due to a memory leak bug. Xml serializer generated dynamic assemblies are not ever collected.

Turning on verbose mode in MSBuild options tells the problem.

Read more ...


Converting generic OopFactory.X12 structures to typed counterparts

OopFactory.X12 can parse EDI messages into segments and loops. However despite typed segments and loops exist, the parser does not generate them in the object model. The unit test only use them when generating EDI messages.

The typed segments and loops are aggregative objects that do not contain their own data members besides the contained untyped objects. Which means if I replace the contained object with the generic one from the parser via .Net reflection, then I can access the object model in a typed way. So I created this in a static class

Read more ...


Getting around “Strong name signature not valid for assembly” for a ClickOnce application

When building a ClickOnce application, I need to redistribute a third party dll. But it failed the strong name validation under sn-vf thus ClickOnce launcher failed with the same error.

Microsoft (R) .Net Framework Strong Name Utility Version 4.0.30319.0 Copyright (c) Microsoft Corporation. All rights reserved.

Read more ...


What part of Windows is written in .Net/WPF/Silverlight?

user 曹一聪 on zhihu is curious how widely .Net is used within Windows components. Obviously an operating system component cannot depend on something that isn’t in the operating system, so nothing uses Silverlight in the desktop version of Windows. And because XP editions mostly did not ship with any .Net Framework version, practically nothing from the XP days can use .Net. Only components newer than XP or rewrote after XP can use .Net, except those Windows Media Center and Tablet PC components that have .Net dependencies.

The first time .Net is available in almost all editions is Windows Vista/2008 (the server core editions make .Net optional). ecause a Vista Capable sticker does not guarantee a machine with user-acceptable speed, not much in Windows Vista is using .Net. OR Windows 7 on that matter if you skip Windows Powershell. The usage of .Net is more in the consumer software (e.g. Windows Live Essentials), developer tools (e.g. Visual Studio) and business tools (e.g. SQL Server). In short, .Net is for writing apps, not really for the OS itself. After Windows 8, Microsoft’s focus moved to Windows RT, now UWP, not much effort is done to improve the desktop, so practically nothing in the desktop got rewritten in .Net Framework.  Windows Help 2.0 is a notable exception however. Because more and more desktop components are moving to UWP (control panel, calculator, games etc), Windows 8 might be the peak of .Net Framework usage in Windows.

Read more ...


When you get a System.BadImageFormatException. maybe you indeed have a bad system image.

I am getting a StackOverflowException with two functions repeating on the call stack, one is the constructor of System.BadImageFormatException, another is System.Environment.GetResourceStringLocal.

Since the call that throws the stack overflow is to a web service proxy defined in the same project as the application, there isn’t a 32bit/64bit mismatch here (32bit machine with every project targeting x86), unlike almost all other discussions on the internet about this exception.

Read more ...


How to: Migrating a CLR console Visual C++ project to Windows Forms

Note: Windows Forms programming in new development is discouraged by Microsoft at this point as Microsoft wants to make full use of hardware accelerated drawing instead of using the CPU-intensive GDI. But maintaining old Windows Forms code in Visual C++ 2012 is still supported, GDI isn’t going anywhere anytime soon.

Visual C++ 2012 removed the Windows Forms project template and I see people scramble to find ways to create a Windows Forms project.  There is a walkaround that’s been around for years, that is, to convert a CLR console application to a Windows one, then add forms related code to the project.

Read more ...


What is the difference between int and System::Int32

Some may say identical, at least that’s what the Visual C++ compiler tells you at the first glance  when you turn on /oldsyntax

Okay, so if I add & to the parameter types I should get the same error right?

Read more ...


Make the webbrowser control styled the same way as the hosting application

User wtx_sonery wants to know whether it is possible to style the webbrowser control with the rest of the application.

The answer is yes, you can implement IDocHostUIHandler’s GetHostInfo method and return DOCHOSTUIFLAG_THEME in the host flags.

Read more ...


415 Unsupported Media Type when WSE is NOT configured

I have a web service that runs fine on my Windows XP. However, when I deploy to the production server, the web service returns 415 Unsupported Media Type when calling.

I have seen this error when WSE is not enabled on the client. The problem is, the web service is NOT using WSE. so I did the usual, uninstall ASP.Net, reinstalling, adding asmx extension to IIS, same error.

Now I need to fire a debugger to see what’s going on. Surprisingly, Microsoft.Web.Services3.dll is loaded even when there is no trace of it in my projects. Now I probably know what’s going on. There is another web service in a different virtual directory that uses WSE.

OK, I will isolate my web service to a new application pool. Well, that does not help. In the end I have to add WSE configuration to both my web service and my Windows client.

Read more ...


Type ‘System.Web.UI.WebControls.Parameter’ does not have a public property named ‘DbType’

In Visual C# 2005 SP1, I added an object data source to a web page that uses my business class as the select method. The method has one parameter of type Guid. The data source wizard generates code like this

<asp:Parameter DbType=”Guid” Name=”rowId” />

Read more ...


AutoComplete with DataSource

Download Sample code: jiangsheng/Samples

.Net 2.0 introduced autocompletion in TextBox and ComboBox. It is obvious that autocomplete is not very useful when the number of options is small. However, when the number of option becomes too many, pre-filling of all options to an AutoCompleteStringCollection becomes impractical, especially when the data is coming from a remote computer. An alternative is to replace the AutoCompleteCustomSource in a TextChanged event, however, users are getting random AccessViolationException when trying to replace it. In this article I will demonstrate another alternative, using a BindingSource as the data source of options, bypassing the .Net Framework and call the underline Windows API directly.

Read more ...


STL/CLR, Compiler and Marshaling

MSDN第9频道又采访了Visual C++类库组的项目经理Nikola DudarSarita Bafna,以及质量控制组的Marina Polishchuk尽管Visual C++项目组已经转移了工作重点,但是很少人注意到这一点。或许这些采访可以帮助你了解Visual C++项目组的工作。 为什么C++仍旧重要?

  • 非托管的应用程序有很大的代码积累,而这些程序的升级工作仍旧在进行

  • 性能是选择C++的重要因素。举例来说,游戏和杀毒程序更适合用非托管代码来编写。

  • 多平台支持。虽然.Net号称是跨平台的,但是如果要编写真正的跨平台程序,开发的时候遵循C++标准还是很有必要的。

为什么C++程序员仍旧重要?

  • C++程序员理解整个机器的运作,他们知道怎么写垃圾收集机制,甚至可以写机器代码

  • C++程序员可以很容易的学会其他语言——C++已经是最难学的语言之一了

  • C++程序员并不只使用一种语言。如果有必要的话,他们会选择汇编、C#或者Perl这样更适合特定任务的语言。

为什么Visual C++项目组转移了工作重点?

  • C++程序员对于转到C#没有抵触心理,所以Visual C++项目组不认为有必要尽快实现Visual C#支持的所有特性,比如LINQ和WPF设计器

  • C++程序员对于让他们的非托管程序调用其他语言的托管代码比用C++来写托管代码更有兴趣

  • 核心模块,例如IE和Windows外壳会更加频繁地更新,而会有更多的非托管代码需要调用这些新的特性,为了这些特性,有必要在MFC中引入新的封装类来节省C++程序员的时间

Orcas中Visual C++的新特性:

  • 托管代码互操作库。可扩展的托管数据类型和非托管数据类型的转换支持

  • STL/CLR。使得托管代码可以利用旧的STL编写的算法

  • Vista支持。对Vista中新的通用控件和文件对话框等界面元素的MFC封装。

  • DevExpess重构引擎——将包含DevExpess的Refactor!™ for C++

Orcas之后的考虑

  • 更新界面。有些Visual C++的代码是针对20年之前的硬件环境设计的,已经不适合现在的需要。新的Phoenix编译引擎使得重写前台变得更加容易。

  • 太多现有的代码需要重构。新的Phoenix编译引擎使得代码分析变得更加容易。

  • C++标准。新的C++标准TR1可能会在Orcas下一版本开发时成为正式标准。

  • 多核支持。需要编写可以充分利用多CPU的代码。第一个尝试是LINQ。

结论

  • MFC和非托管代码回来了

  • 性能和多平台支持的重要性越来越低,托管代码仍旧具有很大的市场。

Visual C++项目组的其他动作

  • ATL Server发布到了源代码共享站点CodePlex。这包含CAtlRegExp,在.Net和第三方类库(boost,TR1)的竞争下已经不再有必要维护一个单独的条件表达式标准了

Read more ...


Error: Unable to cast COM object of type ‘mshtml.HTMLDocumentClass’ to interface type ‘ICustomDoc’

This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{3050F3F0-98B5-11CF-BB82-00AA00BDCE0B}’ failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

My first reaction was:”What the hell? HTMLDocumentClass is the managed wrapper of MSHTML, and MSHTML is supposed to support the ICustomDoc interface!” Now I started wondering why the interfaces don’t work I created a sandbox project and tried to cast interface there, but it works smoothly. I played with strong name and found no luck. Finally, I found out that it is the frame document that does not support this interface.

Read more ...


托管C++中函数调用的双重转换(Double Thunking)

在VC.Net中使用默认设置/clr编译时,一个托管函数会产生两个入口点,一个是托管的,供托管代码调用,另外一个是非托管的,供非托管代码调用。但是函数地址,特别是虚函数指针只能有一个值,所以需要有一个默认的入口。

非托管入口点可能是所有调用的默认入口(在 Visual Studio .NET2003 中,编译器总是会选择非托管入口,但是在Visual Studio 2005中,如果参数或者返回值中包含托管类型,那么编译器会选择托管入口),而另外一个只是使用托管C++中的互操作功能对默认入口的调用。在一个托管函数被另一个托管函数调用的时候,这可能会造成不必要的托管/非托管上下文切换和参数/返回值的复制。如果函数不会被非托管代码使用指针调用,那么可以在声明函数时用VC2005新增的__clrcall修饰符阻止编译器生成两个入口。 现在用简单的冒泡排序算法来比较一下使用__clrcall之后的性能改善程度。

Read more ...


示例:在MFC程序中集成.Net中的控件

从.Net Framework 1.1开始,.Net控件可以以ActiveX的方式被集成到非托管宿主中——但是官方的支持只对于使用托管C++的MFC程序。Chris Sells在2003年3月份的MSDN杂志中描述了这样一个示例(https://web.archive.org/web/20030304083154/http://msdn.microsoft.com/msdnmag/issues/03/03/WindowsForms/default.aspx)。这个示例使用的代码稍微繁琐,而且没有描述如何处理控件的事件。MFC 8.0增加了一系列这方面的支持来把这个集成过程简单化(参考http://msdn2.microsoft.com/en-us/library/ahdd1h97.aspx)。这使得在MFC程序中使用.Net中的一些比较好用的类,例如System::Windows::Forms::PropertyGrid比以前容易多了。

举例来说,要在MFC的基于对话框的程序中使用System::Windows::Forms::PropertyGrid控件,首先创建一个基于对话框的程序,添加必要的引用:

Read more ...


WinForm

今天一口气看完了WinForm Quick Start(http://samples.gotdotnet.com/quickstart/winforms/)

发现.Net使一切变得简单,以前需要大量代码的docking现在只需要一句话

Read more ...


VC6 with .NET CLR & Managed C++

如何让你的VC6也可以写出mc++的程序是一个有挑战性的工作,不过看来MS对于解决这个问题似乎是手到擒来.通过下载一个名叫Microsoft Visual C++ Toolkit 2003 的软件,就可以实现这样的愿望。

访问这个了解更多:http://msdn.microsoft.com/visualc/vctoolkit2003/

Read more ...