HWND  hwnd = FindWindow ((LPCWSTR)"#32770",(LPCWSTR)"对对碰角色版"); //获取QQ对对碰游戏的窗口句柄
       if(hwnd!=NULL)
          this->label2->Text ="游戏 已运行";
       else this->label2 ->Text ="游戏 未运行";//判断窗口是否存在小弟初学C++的Windows编程,对这段代码中有两处不懂的地方:
1.((LPCWSTR)"#32770"这里为什么要强制转换成LPCWSTR类型才能编译通过?而去掉强制类型转换符就不行了,VB中却可以直接调用"#32770","对对碰角色版"这样的参数
2.判断hwnd是否为NULL的时候,不管我开没开QQ对对碰游戏,最后的运行结果都是hwnd为NULL。当我设置断点调试的时候,hwnd显示是未定义的值,这是怎么回事?

解决方案 »

  1.   

    1.((LPCWSTR)"#32770"这里为什么要强制转换成LPCWSTR类型才能编译通过?而去掉强制类型转换符就不行了,VB中却可以直接调用"#32770","对对碰角色版"这样的参数 
    ==============
    VB我不知道,不过C对类型要求严格一点而已。2.判断hwnd是否为NULL的时候,不管我开没开QQ对对碰游戏,最后的运行结果都是hwnd为NULL。当我设置断点调试的时候,hwnd显示是未定义的值,这是怎么回事? 
    ==========
    说明FindWindow这个函数没有成功,你调用一下LastError看一下出错的原因是什么
      

  2.   

    FindWindow这个函数一般两个参数只用一个就行了,知道窗口类的就用前一个,知道窗口标题的就用后一个
      

  3.   

    HWND  hwnd = ::FindWindow (_T("#32770"),_T("对对碰角色版")); 
      

  4.   

    HWND hWnd = FindWindow(NULL,TEXT("对对碰角色版"));
      

  5.   

    1、如果不进行强制类型转换,系统会默认为字符串变量,是LPCSTR类型,通过强制转换LPCWSTR,代表如下:
    lp -> long pointer
    c -> const
    w -> unicode
    str -> string 
    可见,多了个W,即unicode编码的问题。
    2、你的FindWindow使用方法上有点小问题,就如2楼所说,常用一个参数就够了,用不着两个参数都写进去。
      

  6.   

    FindWindow (_T("#32770"),_T("对对碰角色版"));
      

  7.   

    FindWindow函数两个参数中只传递一个就OK了 
     你用FindWindow (_T("#32770"),NULL);
    或者FindWindow (NULL,_T("对对碰角色版"));试试
      

  8.   

    使用FindWindow (NULL,_T("对对碰角色版")); 的时候编译不通过
      

  9.   

    刚没说清楚
    1.使用_T("#32770")或者_T("对对碰角色版")的时候 编译提示无法找到_T标识符。
      使用这个的时候需要做什么声明不?这也是一个强制转换符么?功能是什么?能介绍下么
    2.TEXT(
      LPTSTR string  
    );
    Parameters
    string 
    Pointer to the string to interpret as UTF-16 or ANSI. 
    Res
    This macro interprets an ANSI string at runtime according to the current Windows ANSI code page. Literal ANSI strings that are not strictly ASCII are interpreted differently when processed with different Windows ANSI code pages. For example, '\0xC4' in code page 1252 (Latin-1) represents Upper Case A with Dieresis ('Ä'). However, in code page 1253 (Greek), the string represents Upper Case Delta ('Δ'). These different interpretations lead to development and maintenance issues. For example, a developer might correct a string when using a different system code page from the page used by the original developer; or a build computer might use a different code page. The different interpretations also pose runtime issues, for example, when the end user computer uses a different code page to interpret a string from that used by the build computer
    TEXT这个函数转换成指针指向字符串类型,还是只是字符串类型?
      

  10.   

    TEXT表示字符串是unicode的,否则就是ansi的。
      

  11.   

    用:HWND  hwnd = FindWindow (NULL,"对对碰角色版"); //获取QQ对对碰游戏的窗口句柄就可以......