请教:
  自己写的 DLL,系统来调用,向一个窗口发消息,窗口在一个service中,通过函数FindWindow找到窗口句柄,可是总是失败,即找不到窗口句柄。如果自己写个程序调这个DLL,去可以找到窗口,不知问什么,请指教,谢谢!

解决方案 »

  1.   

    你可以参考:http://www.vckbase.net/document/viewdoc/?id=691
    中的“只能运行程序的一个实例”,使用全局消息!!
      

  2.   

    窗口隐藏不行吗?但我写的普通的程序调这个DLL却可以亚
      

  3.   

    你的service是不是可以用窗口交互的?
      

  4.   

    你的问题是:
    系统调用你的DLL,用FindWindow找不到窗口,
    而你自己调用却可以找到窗口。是这样的吗?
      

  5.   

    有个类似的帖子,是关于窗口隐藏就收不到消息的例子,你参考一下:
    http://community.csdn.net/Expert/topic/3554/3554407.xml?temp=.2940027
      

  6.   

    1 .窗口隐藏时可以用FindWindow发现窗口
    2. 服务要和桌面交互这是我实现相同功能的代码,请大家指教
    // 添加任务栏图标的接收的窗口
    BOOL AddTrayIconWindows()
    {
    HDESK   hdeskCurrent;
        HDESK   hdesk;
        HWINSTA hwinstaCurrent;
        HWINSTA hwinsta;    hwinstaCurrent = GetProcessWindowStation();
        if (hwinstaCurrent == NULL)
    {
    return FALSE;
    }    hdeskCurrent = GetThreadDesktop(GetCurrentThreadId());
        if (hdeskCurrent == NULL)
    {
    return FALSE;
    }    hwinsta = OpenWindowStation( "winsta0", FALSE,
                                      WINSTA_ACCESSCLIPBOARD   |
                                      WINSTA_ACCESSGLOBALATOMS |
                                      WINSTA_CREATEDESKTOP     |
                                      WINSTA_ENUMDESKTOPS      |
                                      WINSTA_ENUMERATE         |
                                      WINSTA_EXITWINDOWS       |
                                      WINSTA_READATTRIBUTES    |
                                      WINSTA_READSCREEN        |
                                      WINSTA_WRITEATTRIBUTES);
        if (hwinsta == NULL)
    {
    return FALSE;
    }    if (!SetProcessWindowStation(hwinsta))
    {
    return FALSE;
    }
        hdesk = OpenDesktop("default", 0, FALSE,
                                DESKTOP_CREATEMENU |
    DESKTOP_CREATEWINDOW |
                                DESKTOP_ENUMERATE    |
                                DESKTOP_HOOKCONTROL  |
                                DESKTOP_JOURNALPLAYBACK |
                                DESKTOP_JOURNALRECORD |
                                DESKTOP_READOBJECTS |
                                DESKTOP_SWITCHDESKTOP |
                                DESKTOP_WRITEOBJECTS);
    if (hdesk == NULL)
    {
               return FALSE;
    } SetThreadDesktop(hdesk);    // 创建一个窗口
        CreateTrayIconWindows()  ;


    if (!SetProcessWindowStation(hwinstaCurrent))
    {
    return FALSE;
    }
    if (!SetThreadDesktop(hdeskCurrent))
    {
    return FALSE;
    }
    if (!CloseWindowStation(hwinsta))
    {
    return FALSE;
    }
    if (!CloseDesktop(hdesk))
    {
             return FALSE;
    }  return true;
    }
    void CreateTrayIconWindows()
    {
    g_hInstance = NULL;
        g_hInstance = GetModuleHandle(NULL);
    g_iWhichIcon = 1;    if ( !hTrayIconDlghWnd )
    {
    WNDCLASS wndclass; 
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hbrBackground=(HBRUSH)::GetStockObject(WHITE_BRUSH);
    wndclass.hCursor=NULL;
    wndclass.hIcon=NULL;
    wndclass.hInstance=g_hInstance ;
    wndclass.lpfnWndProc=myboleProc;
    wndclass.lpszClassName="myclass";
    wndclass.lpszMenuName=NULL;
    wndclass.style=CS_VREDRAW;
    RegisterClass(&wndclass);

    hTrayIconDlghWnd = CreateWindow("myclass","// {4256BB56-8931-48bb-9D53-0D570A8A5A1D}static const GUID <<name>>={ 0x4256bb56, 0x8931, 0x48bb, { 0x9d, 0x53, 0xd, 0x57, 0xa, 0x8a, 0x5a, 0x1d } };",WS_OVERLAPPEDWINDOW,50,50,400,300,NULL,NULL,g_hInstance,NULL);                                           
    if(!hTrayIconDlghWnd )
    return ;
    }
    ::ShowWindow(hTrayIconDlghWnd,SW_HIDE); m_bTrayIconDlgHasCreate= true;
    }
    // 在其他的地方向窗口发送消息
        // 消除已经添加的任务栏图标
    hTrayIconDlghWnd = ::FindWindow(NULL,"// {4256BB56-8931-48bb-9D53-0D570A8A5A1D}static const GUID <<name>>={ 0x4256bb56, 0x8931, 0x48bb, { 0x9d, 0x53, 0xd, 0x57, 0xa, 0x8a, 0x5a, 0x1d } };");
    if ( hTrayIconDlghWnd )
    {
    ::PostMessage(hTrayIconDlghWnd ,WM_USER+15,NULL,NULL);
    ::PostMessage(hTrayIconDlghWnd ,WM_USER+12,NULL,NULL);
    }
      

  7.   

    服务进程和用户进程默认是在不同的桌面下的Winlogon桌面和Default桌面,这两个桌面的窗口是隔离的1,在窗口线程中先用OpenDesktop函数打开"Winsta0\\Default"用户桌面
    然后再用SetThreadDesktop,将当前线程的桌面设置为用户桌面
    2,在安装服务时设置 SERVICE_INTERACTIVE_PROCESS 参数