公告

<2019年2月>
272829303112
3456789
10111213141516
17181920212223
242526272812
3456789

统计

  • 随笔 - 9
  • 文章 - 13
  • 评论 - 3
  • 引用 - 0

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

2007年10月23日

使用标准C++的类型转换符:static_cast、dynamic_cast、reinterdivt_cast、和const_cast

使用标准C++的类型转换符:static_cast、dynamic_cast、reinterdivt_cast、和const_cast。

3.1 static_cast
用法:static_cast < type-id > ( exdivssion )
该运算符把exdivssion转换为type-id类型,但没有运行时类型检查来保证转换的安全性。它主要有如下几种用法:
①用于类层次结构中基类和子类之间指针或引用的转换。
  进行上行转换(把子类的指针或引用转换成基类表示)是安全的;
  进行下行转换(把基类指针或引用转换成子类表示)时,由于没有动态类型检查,所以是不安全的。
②用于基本数据类型之间的转换,如把int转换成char,把int转换成enum。这种转换的安全性也要开发人员来保证。
③把空指针转换成目标类型的空指针。
④把任何类型的表达式转换成void类型。

注意:static_cast不能转换掉exdivssion的const、volitale、或者__unaligned属性。


3.2 dynamic_cast
用法:dynamic_cast < type-id > ( exdivssion )
该运算符把exdivssion转换成type-id类型的对象。Type-id必须是类的指针、类的引用或者void *;
如果type-id是类指针类型,那么exdivssion也必须是一个指针,如果type-id是一个引用,那么exdivssion也必须是一个引用。

dynamic_cast主要用于类层次间的上行转换和下行转换,还可以用于类之间的交叉转换。
在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的;
在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。
class B{
public:
int m_iNum;
virtual void foo();
};

class D:public B{
public:
char *m_szName[100];
};

void func(B *pb){
D *pd1 = static_cast(pb);
D *pd2 = dynamic_cast(pb);
}

在上面的代码段中,如果pb指向一个D类型的对象,pd1和pd2是一样的,并且对这两个指针执行D类型的任何操作都是安全的;
但是,如果pb指向的是一个B类型的对象,那么pd1将是一个指向该对象的指针,对它进行D类型的操作将是不安全的(如访问m_szName),
而pd2将是一个空指针。

另外要注意:B要有虚函数,否则会编译出错;static_cast则没有这个限制。
这是由于运行时类型检查需要运行时类型信息,而这个信息存储在类的虚函数表(
关于虚函数表的概念,详细可见)中,只有定义了虚函数的类才有虚函数表,
没有定义虚函数的类是没有虚函数表的。

另外,dynamic_cast还支持交叉转换(cross cast)。如下代码所示。
class A{
public:
int m_iNum;
virtual void f(){}
};

class B:public A{
};

class D:public A{
};

void foo(){
B *pb = new B;
pb->m_iNum = 100;

D *pd1 = static_cast(pb); //compile error
D *pd2 = dynamic_cast(pb); //pd2 is NULL
delete pb;
}

在函数foo中,使用static_cast进行转换是不被允许的,将在编译时出错;而使用 dynamic_cast的转换则是允许的,结果是空指针。


3.3 reindivter_cast
用法:reindivter_cast (exdivssion)
type-id必须是一个指针、引用、算术类型、函数指针或者成员指针。
它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针(先把一个指针转换成一个整数,
在把该整数转换成原类型的指针,还可以得到原先的指针值)。

该运算符的用法比较多。

3.4 const_cast
用法:const_cast (exdivssion)
该运算符用来修改类型的const或volatile属性。除了const 或volatile修饰之外, type_id和exdivssion的类型是一样的。
常量指针被转化成非常量指针,并且仍然指向原来的对象;
常量引用被转换成非常量引用,并且仍然指向原来的对象;常量对象被转换成非常量对象。

Voiatile和const类试。举如下一例:
class B{
public:
int m_iNum;
}
void foo(){
const B b1;
b1.m_iNum = 100; //comile error
B b2 = const_cast(b1);
b2. m_iNum = 200; //fine
}
上面的代码编译时会报错,因为b1是一个常量对象,不能对它进行改变;
使用const_cast把它转换成一个常量对象,就可以对它的数据成员任意改变。注意:b1和b2是两个不同的对象。
 

== ===========================================

== dynamic_cast .vs. static_cast
== ===========================================

class B { ... };
class D : public B { ... };

void f(B* pb)
{

D* pd1 = dynamic_cast(pb);

D* pd2 = static_cast(pb);
}

If pb really points to an object of type D, then pd1 and pd2 will get the same value. They will also get the same value if pb == 0.

If pb points to an object of type B and not to the complete D class, then dynamic_cast will know enough to return zero. However, static_cast relies on the programmer’s assertion that pb points to an object of type D and simply returns a pointer to that supposed D object.

dynamic_cast可用于继承体系中的向下转型,即将基类指针转换为派生类指针,比static_cast更严格更安全。dynamic_cast在执行效率上比static_cast要差一些,但static_cast在更宽上范围内可以完成映射,这种不加限制的映射伴随着不安全性。static_cast覆盖的变换类型除类层次的静态导航以外,还包括无映射变换、窄化变换(这种变换会导致对象切片,丢失信息)、用VOID*的强制变换、隐式类型变换等...


== ===========================================
== static_cast .vs. reinterdivt_cast
== ================================================

reinterdivt_cast是为了映射到一个完全不同类型的意思,这个关键词在我们需要把类型映射回原有类型时用到它。我们映射到的类型仅仅是为了故弄玄虚和其他目的,这是所有映射中最危险的。(这句话是C++编程思想中的原话)

static_cast reinterdivt_cast 操作符修改了操作数类型。它们不是互逆的; static_cast 在编译时使用类型信息执行转换,在转换执行必要的检测(诸如指针越界计算, 类型检查). 其操作数相对是安全的。另一方面;reinterdivt_cast 仅仅是重新解释了给出的对象的比特模型而没有进行二进制转换, 例子如下:

int n=9; double d=static_cast < double > (n);

上面的例子中, 我们将一个变量从 int 转换到 double 这些类型的二进制表达式是不同的。 要将整数 9 转换到 双精度整数 9static_cast 需要正确地为双精度整数 d 补足比特位。其结果为 9.0。而reinterdivt_cast 的行为却不同:

int n=9;

double d=reinterdivt_cast (n);

这次, 结果有所不同. 在进行计算以后, d 包含无用值. 这是因为 reinterdivt_cast 仅仅是复制 n 的比特位到 d, 没有进行必要的分析.

因此, 你需要谨慎使用 reinterdivt_cast.

posted @ 2007-10-23 20:28 blues 阅读(2955) | 评论 (0)编辑 收藏

2007年10月22日

offsetof 解析

 1offsetof(s,m)解析 offsetof(s,m)解析
 2 
 3今天看代码时,发现一个有用的东东,offsetof(s,m),这是一个宏,定义如下:
 4
 5 #define offsetof(s,m) (size_t)&(((s *)0)->m)
 6 
 7 然后到网上查了一下,发现还真的是很有用,附带一位大侠的解说:
 8
 9  struct   AAA   
10  {   
11  int   i;   
12  int   j;   
13  }
;   
14    
15  struct AAA *pAAA;   
16  pAAA=new AAA;   
17  这时,pAAA实际上是一个Pointer, 指向某一确定的内存地址,比如0x1234;   
18  而 pAAA->i 整体是一个int型变量,其地址是&(pAAA->i) ,'&'为取址运算符;   
19  那么&(pAAA->i)一定等于0x1234,因为i是结构体AAA的第一个元素。   
20  而&(pAAA->j)一定是0x1234 + 0x4 = 0x1238; 因为sizeof(int= 4;
21    
22  这个做法的巧妙之处就是:它把“0”作为上例中的pAAA,那么 &(pAAA->j)就是j的   
23  offset啦
24
25  解析结果是:   
26  (s   *)0 ,将 0 强制转换为Pointer to   "s"     
27  可以记 pS = (s *)0 ,pS是指向s的指针,它的值是0;   
28  那么pS->m就是m这个元素了,而&(pS->m)就是m的地址,而在本例中就是offset啦   
29    
30  再把结果强制转换为size_t型的就OK 了,size_t其实也就是int啦!!    
31 
32 
33

posted @ 2007-10-22 16:02 blues 阅读(3989) | 评论 (2)编辑 收藏

2007年1月26日

2006年世界顶级杀毒软件排名

http://blog.cnetnews.com.cn/hand/keji/3658/

posted @ 2007-01-26 14:55 blues 阅读(130) | 评论 (0)编辑 收藏

2006年12月11日

com中以结构体作为接口参数

 1 通过VARIANT;   
 2   VARIANT   varData;(出参)   
 3     
 4   MYSTRUCT    * pMyData    =    NULL;   
 5     
 6   pMyData    =    (MYSTRUCT * )CoTaskMemAlloc( sizeof (MYSTRUCT));   
 7   .   
 8     
 9   varData.byref    =    (LPVOID)pMyData;   
10     
11   在调用文件里,同样定义   
12   VARIANT   varData(入参)   
13     
14   MYSTRUCT    * pMyData    =    NULL;   
15     
16   pMyData    =    (MYSTRUCT * )varData.byref;   
17     
18   CoTaskMemFree((LPVOID)pMyData);

posted @ 2006-12-11 15:39 blues 阅读(710) | 评论 (0)编辑 收藏

2006年11月7日

Getting an (ATL) ActiveX control to print from Office Applications

13 votes for this article.
Popularity: 4.73. Rating: 4.25 out of 5.

Introduction

Seeing as this is my first ever post to CodeProject, let me do a quick introduction as to who I am and what I do. I have been working in one form of C and C++ or another for as long as I can remember (among the other myriad of languages that I've run into). Nowadays, most of my development is focused on Microsoft Windows platforms, and is done in VC6, VC2002.NET. I am heavily entrenched in BI (Business Intelligence) development, and in my spare time develop little ActiveX controls and games etc...

The plot

Having learnt COM a while ago, I made the obvious progression to ATL to ease the development of boilerplate code, and to leverage off Microsoft's template library. As my experience grew, I ventured into creating ActiveX controls using the ATL framework ... and life was good. I could spew out a fairly useful (albeit not overly complex) control within a short period of time. Recently, I was asked to create a KPI (Key Performance Indicator) control that could be embedded in a web page and an Excel document. Obviously based on my experience (which was obviously not vast) I thought that this would be no problem and off I went, creating code that would meet the functional spec (we all work to these don't we :)).

A couple of days later the control was finished and the final tests were being run when someone asked me to print a hardcopy of an example spreadsheet with the embedded control. This is where my nightmares began. Not only did my control not print, but there was no clear indication as to why it didn't print. And so my exploration into this apparent mystery began.

Have you ever tried to include 3rd party ActiveX controls into an Office document? They sure seem to work fine, but most (apart from the Microsoft controls) don't seem to render themselves when you request a Print Preview or a simple Print of the worksheet or document. So, if any of you have ever had this problem, or have never dabbled with this, but think that you may be heading this way, take note of this, cos it might save you hours of frustration and frantic searching on MSDN and Google.

So what now?

The first thing one needs to realize is that even though we have been blessed with Office 2000 and Office XP, the printing architecture still uses the old Windows-format metafile for its printing operations. This metafile format was used in 16-bit Windows-based applications (thinks back to Win3.1). Now, this becomes a major problem for ActiveX developers who wish their controls to be printable from within Office applications, because this old metafile format only supported a limited set of GDI functionality. The list of supported GDI functions can be found here.

Now that you are armed with your limited function set, you cringe with the realization that you can no longer create memory DC's, you can no longer use your lovely DrawText() functions and you can definitely no longer call GetTextExtentPoint32() function. However, those realizations only hold true for the instance of when your control is being rendered to an old format metafile. So how do we empower our control to know that its being rendered to an old format metafile? Simple, we use the GetObjectType() function and check if the result is equal to OBJ_METADC (old metafile format):

HRESULT Cxxxxx::OnDraw(ATL_DRAWINFO& di)
{
    HDC hdc = di.hdcDraw;
    bool bMetaFile = false;

    //// lets check if we're drawing to an old// metafile format.. (like Office printing)//if ( GetObjectType(hdc) == OBJ_METADC )
        bOldMetaFile = true;

    //// the rest of your code...//
}

For interest, the opposite of OBJ_METADC is OBJ_ENHMETADC (refer to this MSDN document).

Now that we know if we're drawing to an old metafile format or not, we can write adaptive code to cater for each instance or we can just write all our drawing logic using the limited set of functionality that is supported by the old metafile DC.

What about fonts and text extents?

As any ATL ActiveX developer knows, using fonts in AX controls provides for limited amount of fun. The typical piece of code would probably look something like this:

																//
																// ... some code
																//
    CComQIPtr<IFont, &IID_IFont> pFont(m_pFont);
    TEXTMETRICOLE tm;if ( pFont != NULL )
    {
        pFont->get_hFont(&newFont);
        pFont->AddRefHfont(newFont);
        pFont->QueryTextMetrics(&tm);
        oldFont = (HFONT) SelectObject(dc, newFont);
    }

The Bolded lines of code are ones that I didn't use regularly, due to the fact that I didn't really need to know about the breakdown of my font's details because I had access to GetTextExtentPoint32() function. Unfortunately, in this scenario, we don't have access to that function to determine how wide (in pixels) our text is going to be. But there is another way to calculate this fairly accurately, as is demonstrated in the code below:

																//
																// assume that we have called QueryTextMetrics() and
																// have a filled TEXTMETRICOLE structure called tm
																//
CComBSTR strText(_T("Hello, world"));
SIZE sz;

sz.cx = strText.Length() * tm.tmAveCharWidth;
sz.cy = tm.tmHeight;

Having said this, there are many other functions that I use a lot that I can't use if I want my ActiveX control to be printable by Office, but as with GetTextExtentPoint32() and its respective replacement, there is always a way to replace these functions using Old-Metafile-Safe-Drawing-Code (OMSDC). *maybe that acronym will catch on*

Conclusion

When creating an ActiveX control that you know will be used inside Office applications, and will most probably be printed, remember to stick to these guidelines when developing your drawing logic. I was fairly shocked by how little information was available in the MSDN and online in general, while I was searching for information on how to enable my ActiveX control to print from within an Office application. There are hundreds of documents on ActiveX controls being printed from within Internet Explorer, but none address this particular issue. Perhaps I was looking in the wrong places. Hopefully this article will help one or more of you one day ;)

Acknowledgment

Many thanks to Igor Tandetnik for pointing me in the right direction on this.

About Peter Mares

posted @ 2006-11-07 17:38 blues 阅读(354) | 评论 (0)编辑 收藏
转===如何用ATL创建ActiveX控件(牛人翻译的)

http://www.czvc.com/down.asp?id=105

posted @ 2006-11-07 16:53 blues 阅读(389) | 评论 (0)编辑 收藏

2006年9月12日

DB2免费版

http://www.ibm.com/developerworks/cn/downloads/im/udbexp/

posted @ 2006-09-12 14:08 blues 阅读(288) | 评论 (0)编辑 收藏

2006年8月25日

美国让人喷饭的法律

 

这美国还有这等喷饭的法律,实属大开眼界,看来有机会要好好善加利用了!!现与各位一起分享: 
美国联邦法律规定: 

1)不得与豪猪发生性关系。(*,谁敢呀) 
2)每周四晚6:00以后不得放P。(以后还真要小心了,别一不留神坐牢了还不知为 
啥) 
3)任何人不得销售其子女。(好象中国也不许吧) 

阿拉巴马州: 
无论任何时候,将冰激淋卷放在口袋里是违法的。(有病丫的) 

阿肯色州: 
男性可以合法殴打其配偶,但每月最多一次。(估计很多东北的兄弟知道了一定想 
移民阿肯色了,可也有例外呀,克林顿就是阿肯色的前州长,咋老被喜莱莉扁呀) 

亚利桑纳州: 
任何房间中不得有两根以上的假****。(估计那州的最高法官丫是个变态狂!) 

夏威夷州: 
不得将谷物放在耳朵里。(神经病,以为偷太空种子呀) 

印弟安纳州: 
1)任何年满18岁的男性,若与17岁以下的女性发生性关系,而且当时她又没穿鞋 
袜,那将课重罪。(兄弟们千万注意了呀!别全脱了) 
2)圆周率在该州法定为4。(活活气死咱祖冲之前辈呀!) 

爱荷华州: 
1)任何只有一只上臂的钢琴演奏者必须免费演奏。(严重歧视残疾艺术表演家) 
2)任何有胃病的男性不得在公共场所与女性接吻。(接吻和胃有关系吗?男性胃癌 
晚期患者的福音) 

纽约州: 
1)不得仅为娱乐而将球砸向他人脑袋。(谋杀可以不?真的脑子进水了) 
2)10:00以后不得穿拖鞋。(光脚吧) 

新泽西州: 
凡谋杀时不得穿防弹背心。(管得着吗,警察这么没自信!) 

北卡州: 
任何一位未婚男性与一为未婚女性,如果在任何旅馆或汽车旅馆登记为已婚,那么 
他们即算合法夫妻了。(想带小蜜开房的兄弟们千万别去那州呀!) 

宾西法尼亚州: 
不得在浴室唱歌。(难怪在宾大商学院的同胞都不会K歌) 

南卡州: 
仅在每周六,男性被允许在法院的门前台阶上合法殴打其配偶。(这是啥规定,郁 
闷ING) 

犹他州: 
1)不喝牛奶违法。(喝不完援助非洲难民呀,干么为难自己!难怪俺一只要喝牛 
奶就拉肚子的朋友从犹大转到纽约了,保命要紧呀。) 
2)不得在正在执行急救任务的救护车后座上Make Love。(这好理解,怕病人看见血管 
爆裂么!哈哈) 

posted @ 2006-08-25 17:53 blues 阅读(113) | 评论 (0)编辑 收藏

2006年8月23日

上海印象

posted @ 2006-08-23 15:21 blues 阅读(208) | 评论 (1)编辑 收藏
仅列出标题