Tuesday, December 05, 2006

 

Some notes on Visual Studio 2005 -1

正在Porting原来的项目到Visual Studio 2005,遇到了很多奇怪的Bug,这里记载一些

1.
From http://blog.dragon2.net/archives/2006/03/08/300.php

Compiler Warning C4819 (C++)

用了 Microsoft Visual Studio 2005 之後本來還覺得不錯,沒想到這幾天為了要安裝 Boost library,終於出問題了。

首先照著說明文件打指令,bjam 要先安裝就是了。當然這些都不是什麼大問題,不過裝著裝著問題就來了,怎麼會有一堆 warning C4819 咧?本來是不以為意啦(誰會想要去理 warning 啊),不過美好的一切就在 Visual Studio 2005 告訴你找不到 regex library 時破滅了!!!

追根究底,這樣出現 C4819 warning 的檔案,如果直接去開,那麼會發現裡面有亂碼。亂碼問題不大,問題是有些會影響到 compiler 的判斷。也就是原來放在註解的某些字元一下子就變成 code 了,所以 boost 在安裝的時候就少了幾個 library 沒放上去,也造成了 regex 沒法用(其實我只要用 regex 而已,其他的我不清楚)!

因為字碼(codepage)都亂了,本來用 vim/notepad 開啟後另存為 Unicode 模式也沒用(因為打開時就爛了)。現在有想到一個白痴的方法,就是找台 FreeBSD 機器,用它來幫我轉換(用 vi/vim 開檔雖然也有亂碼,不過按 ga 時 hex 值還是對的)。正在試….

Updated: 上面的方法還是不行,現在用鋸箭法解決了,把有問題的 / ? / 砍掉,反正註解我也看不到,也不用懂了。

Reference
The new compiler error C4819
http://blogs.msdn.com/michkap/archive/2005/01/05/347394.aspx

More on the C4819 error
http://blogs.msdn.com/michkap/archive/2005/12/09/502290.aspx

Compiler Warning (level 1) C4819 (C++)
http://msdn2.microsoft.com/en-us/library(d=robot)/ms173715.aspx

luoge Says:
十月 3rd, 2006 at 12:25 pm
I just found there is a software “FAR”, which can convert ansi files to unicode files. I have compiled the library with vc++ 2005 successfully after doing the conversion.

http://www.helpware.net/FAR/help/Unicode.htm

2.
From http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=220821&SiteID=1

Rob Caldecott 25 Jan 2006, 11:46 PM UTC

I have found a serious problem with the Visual Studio 2005 Professional resource editor. I am based in the UK (this is crucial), and when I add a new resource to an MFC project, it is assigned the English (United Kingdom) language by default. Unfortunately, the following line is added to the .rc file, which will not compile:

LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_NEUTRAL

The resource compiler will produce the following:

error RC2147 : SUBLANGUAGE ID not a number

It *should* be using SUBLANG_ENGLISH_UK instead.

If I add a new resource and then attempt to change the language to English (US), then it fails and the error persists. This means that every time I add a new resource, I am forced to edit the .rc file by hand in order for it to compile, which is very time-consuming and prone to error.

Does anyone have a workaround for this? I guess I could define
SUBLANG_ENGLISH_NEUTRAL as SUBLANG_ENGLISH_UK somewhere. Note also that SUBLANG_ENGLISH_NEUTRAL is nowhere to be found in the Visual C++ headers.

Ted. 26 Jan 2006, 12:25 AM UTC

There's already a bug for this

http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=3b49aa31-fa2b-419e-927a-0b16ccc0e18b

the people at Microsoft claim it's not reproduceable in builds after Beta 2. Which build are you running?

Rob Caldecott 26 Jan 2006, 8:19 AM UTC

I am running the official build:
8.0.50727.42 (RTM.050727-4200)

Ted. 26 Jan 2006, 1:27 PM UTC

The bug has now been reopened (see above link) - please feel free to add any more information you have to it.

3.
发信人: cygwin (空), 信区: MSDN
标 题: 这时我理解错误还是vc Bug?
发信站: 水木社区 (Sat Nov 4 14:08:44 2006), 站内

#include
#include

int main(int argc, char* argv[])
{
setlocale(LC_ALL, "chs");
wprintf(L"测试1\n");

std::locale loc("chs");
std::wcout.imbue(loc);
std::wcout << L"测试2" << std::endl;

return 0;
}

main函数体中前两行,使wprintf能输出中文,
后三行使wcout输出中文,这两个单独执行时都是好的,

但是现在放在一起,后面的那个“测试2”出不来,
是我傻了还是vc傻了啊?


编译器为vs2005

--

※ 修改:·cygwin 于 Nov 4 14:09:17 修改本文·[FROM: 219.134.113.*]
※ 来源:·水木社区 http://newsmth.net·[FROM: 219.134.113.*]

发信人: Quaful (夸父|要找工作咯), 信区: MSDN
标 题: Re: 这时我理解错误还是vc Bug?
发信站: 水木社区 (Sat Nov 4 16:07:47 2006), 站内

呵呵,通常我们写程序的时候,都有一条注意事项,就是 setlocale 和 std::locale 不能混用。至于为什么的话以前我没有细究过,今天你提了这个问题,本来我想就此琢磨一下到底是什么地方引起了两者的不兼容,不过翻了半天 CRT 和 iostream 的代码,有点乱,呵呵。简单的想,可能是这样一个原因:wprintf 并不是一个 unicode 的输出函数,它实际上是根据 setlocale 设置的 locale 信息把参数里面的 wchar_t 转换为对应的 MBCS;wcout 也是一样。如果混用了 setlocale 和 std::locale,转换的过程中可能就会出问题。

【 在 cygwin (空) 的大作中提到: 】
: #include
: #include
: int main(int argc, char* argv[])
: ...................

--

※ 来源:·水木社区 newsmth.net·[FROM: 221.219.7.*]

发信人: fireseed (昏睡), 信区: MSDN
标 题: Re: 这时我理解错误还是vc Bug?
发信站: 水木社区 (Sat Nov 4 23:48:07 2006), 站内

中间加一句:
setlocale(LC_ALL, "C");
就可以了

【 在 cygwin (空) 的大作中提到: 】
: #include
: #include
: int main(int argc, char* argv[])
: ...................

--

※ 来源:·水木社区 newsmth.net·[FROM: 222.240.210.*]

发信人: Quaful (夸父|毕业毕业), 信区: MSDN
标 题: VS2005中fstream不支持中文文件名的BUG的原因及简单workaround
发信站: 水木社区 (Mon Dec 4 10:31:13 2006), 站内

VS2005里面的fstream在打开文件的时候,如果传入的文件名中有中文字符,则打开常常会失败。查代码,原来是因为fstream打开文件调用的是Fiopen函数:
(VC\crt\src\fiopen.cpp, line 9)
_MRTIMP2_NCEEPURE FILE *__CLRCALL_PURE_OR_CDECL _Fiopen(const char *filename,
ios_base::openmode mode, int prot)
{ // open wide-named file with byte name
wchar_t wc_name[FILENAME_MAX];

// 在这里出错
if (mbstowcs_s(NULL, wc_name, FILENAME_MAX, filename, FILENAME_MAX - 1) != 0)
return (0);
return _Fiopen(wc_name, mode, prot);
}
那么就可以知道是locale的原因了,简单的workaround就是调用一个setlocale
例:
setlocale(LC_ALL, "chs");
std::ifstream test("你好.txt");
if(file)
{
// do sth.
}

--

※ 来源:·水木社区 newsmth.net·[FROM: 124.42.96.*]

发信人: flybb (zz), 信区: MSDN
标 题: Re: VS2005中fstream不支持中文文件名的BUG的原因及简单workaro
发信站: 水木社区 (Mon Dec 4 10:41:53 2006), 站内

然后cout就不能输出中文了

【 在 Quaful (夸父|毕业毕业) 的大作中提到: 】
: VS2005里面的fstream在打开文件的时候,如果传入的文件名中有中文字符,则打开常常会失败。查代码,原来是因为fstream打开文件调用的是Fiopen函数:
: (VC\crt\src\fiopen.cpp, line 9)
: _MRTIMP2_NCEEPURE FILE *__CLRCALL_PURE_OR_CDECL _Fiopen(const char *filename,
: ...................

--

※ 来源:·水木社区 newsmth.net·[FROM: 218.104.71.*]

发信人: Quaful (夸父|毕业毕业), 信区: MSDN
标 题: Re: VS2005中fstream不支持中文文件名的BUG的原因及简单workaround
发信站: 水木社区 (Mon Dec 4 10:56:06 2006), 站内

就像前段时间讨论过的,还得用
setlocale(LC_ALL, "C");
改回来

vs2005的stl确实很bt啊

【 在 flybb (zz) 的大作中提到: 】
: 然后cout就不能输出中文了


--

※ 来源:·水木社区 newsmth.net·[FROM: 124.42.96.*]

4.
http://blog.joycode.com/jiangsheng/archive/2006/12/19/89320.aspx

VC++ 2003 Managed Extensions to VC++2005 C++/CLI Conversion Tool,Visual C++的方向
Stan Lippman在C++/CLI Migration Primer中提到的mscfront已经以VC++ 2003 Managed Extensions to VC++2005 C++/CLI Conversion Tool的名字发布,下载地址在http://www.microsoft.com/downloads/details.aspx?FamilyID=a3581805-1af2-4c34-9d0e-6029cc078441&displaylang=en。这个工具看起来并不完善,但是好在有源代码,可以自行修改来适应个人的代码。

另外,在MSDN论坛的一个帖子中提到,Visual C++产品组在对产品的评估中决定将工作的重点转移回非托管代码。Visual C++产品组会加强对Windows非托管API的支持,以及提供托管和非托管代码的互操作性,但是在Orcas不会提供对所有.Net新特性的支持,例如LINQ和WPF。Orcas中确定的新特性只有STL/CLR(以前叫STL.Net)和对C++标准的进一步支持。虽然Windows Vista上有Desktop Window Manager API,但是在Windows XP上要用WPF的话还是得和其他语言的代码互操作。

看起来Visual C++产品组还是没有解决支持partial class的问题,C++的编译模式使得在类编译成obj之后动态扩展比较困难。C++程序员只能继续在创建Form之后手动把设计器生成的函数实现搬到cpp文件里面去。

(按:C++ is still alive)

5.
Upgrade Issue: Compile error C2327 with import dll under VC8/VS2005

http://blog.csdn.net/carolbaby/archive/2006/12/22/1452489.aspx

Issue:
When I upgrade an exe project from VC7.1 to VC8, it says upgrade
succeed. But When I try to compile the project, it outputs a lot of errors.

..\debug\msado15.tlh(1024) : error C2327: 'CDO::_ADO::Properties' : is not a type name, static, or enumerator
..\debug\msado15.tlh(2086) : error C2327: 'CDO::Command15::Parameters' : is not a type name, static, or enumerator
..\debug\msado15.tlh(2285) : error C2327: 'CDO::Connection15::Errors' : is not a type name, static, or enumerator
..\debug\msado15.tlh(2525) : error C2327: 'CDO::Recordset15::Fields' : is not a type name, static, or enumerator
..\debug\msado15.tlh(3124) : error C2327: 'CDO::_Record::Fields' : is not a type name, static, or enumerator
..\debug\msado15.tli(109) : error C2065: '_result' : undeclared identifier
..\debug\cdosys.tlh(628) : error C2327: 'CDO::IConfiguration::Fields' : is not a type name, static, or enumerator
..\debug\cdosys.tlh(814) : error C2327: 'CDO::IBodyPart::Fields' : is not a type name, static, or enumerator
..\debug\cdosys.tlh(1090) : error C2327: 'CDO::IMessage::Fields' : is not a type name, static, or enumerator
..\debug\cdosys.tlh(1100) : error C2327: 'CDO::IMessage::Fields' : is not a type name, static, or enumerator

In my project, the import sentences are written like follows:

#include "stdafx.h"
#include "Resource.h"
#include "ProcessorThread.h"
#include
#import "c:\program files\common files\system\ado\msado15.dll"
rename("ADODB","CDO") rename( "EOF", "adoEOF" )
#import


Root Reason:
The msado15.tlh and cdosys.tlh are not generated correctly. When I compare the .tlh file generated by 2003 and 2005, I noticed the keyword “struct” is missed in some method definitions in the tlh file generated by 2005.
For example: in msado15.tlh
virtual HRESULT __stdcall get_Item (
/*[in]*/ VARIANT Index,
/*[out,retval]*/ struct Property * * ppvObject ) = 0; // this struct is missed in the tlh generated under VS2005

virtual HRESULT __stdcall get_Item (
/*[in]*/ VARIANT Index,
/*[out,retval]*/ struct Error * * ppvObject ) = 0; // this struct is missed in the tlh generated under VS2005


The generation of tlh files is affected by some codes in this project, which is compiled earlier than the import sentences.

Solution:
Move the import sentences to the very beginning of stdafx.h. Maybe move to other positions could also make things work. The only thing I did is to make the import sentences compiled earlier than other sentences, by moving their position.



<< Home

This page is powered by Blogger. Isn't yours?