2013-05-08 7 views
7

मैंने एक साधारण Win32 कंसोल एप्लिकेशन बनाया है जो एक छिपी हुई संदेश-केवल विंडो बनाता है और संदेशों के लिए प्रतीक्षा करता है, पूर्ण कोड नीचे है।कंसोल एप्लिकेशन में केवल संदेश विंडो का उपयोग करके संदेश कैसे प्राप्त करें?

#include <iostream> 
#include <Windows.h> 

namespace { 
    LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
    { 
    if (uMsg == WM_COPYDATA) 
     std::cout << "Got a message!" << std::endl; 
    return DefWindowProc(hWnd, uMsg, wParam, lParam); 
    } 
} 

int main() 
{ 
    WNDCLASS windowClass = {}; 
    windowClass.lpfnWndProc = WindowProcedure; 
    LPCWSTR windowClassName = L"FoobarMessageOnlyWindow"; 
    windowClass.lpszClassName = windowClassName; 
    if (!RegisterClass(&windowClass)) { 
    std::cout << "Failed to register window class" << std::endl; 
    return 1; 
    } 
    HWND messageWindow = CreateWindow(windowClassName, 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, 0); 
    if (!messageWindow) { 
    std::cout << "Failed to create message-only window" << std::endl; 
    return 1; 
    } 

    MSG msg; 
    while (GetMessage(&msg, 0, 0, 0) > 0) { 
    TranslateMessage(&msg); 
    DispatchMessage(&msg); 
    } 
    return msg.wParam; 
} 

हालांकि, मुझे किसी अन्य एप्लिकेशन से कोई संदेश नहीं मिल रहा है। GetMessage() बस ब्लॉक और कभी वापस नहीं आते हैं। मैं FindWindowEx() का उपयोग उसी संदेश नाम के साथ करता हूं जो एक संदेश भेजता है, और यह विंडो पाता है। बस संदेश स्पष्ट रूप से प्राप्त नहीं किया जा रहा है।

क्या मैं यहां कुछ गलत कर रहा हूं? विंडो संदेश प्राप्त करने वाले सबसे कम एप्लिकेशन क्या हैं?

+0

वहाँ किसी कारण आप कर रहे है वहाँ खिड़की प्रसंस्करण कास्टिंग? ऐसा लगता है कि यह पहले से ही एक WNDPROC है। – HerrJoebob

+0

@HerrJoebob आप सही हैं, आवश्यक नहीं लगता है। – futlib

उत्तर

5

आपके संदेश User Interface Privilege Isolation द्वारा अवरुद्ध किए जा सकते हैं। उस स्थिति में आप WM_COPYDATA संदेश को अनुमति देने के लिए ChangeWindowMessageFilterEx() फ़ंक्शन का उपयोग कर सकते हैं।

+0

यहां समस्या नहीं प्रतीत होती है, निष्क्रिय यूएसी और संदेशों को अभी भी नहीं मिलता है। – futlib

+0

कभी नहीं, मुझे नहीं पता था कि मुझे रीबूट करना था। यह वास्तव में यूएसी था। इसे निष्क्रिय करने के बाद, यह एक कंसोल और गुई अनुप्रयोग दोनों में काम करता है। यूएसी सक्षम के साथ, यह न तो काम करता है। – futlib

+0

@ फ़ुटलिब यूएसी को अक्षम करने की अनुशंसा नहीं की जाती है क्योंकि यह विंडोज सुरक्षा अवधारणा –

2

iam सुनिश्चित नहीं है कि आपके प्रोग्राम में गलत है, लेकिन मैं हमेशा विपरीत दृष्टिकोण का उपयोग करता हूं: मैं एक Win32 ऐप बनाता हूं, इसे नीचे दबाता हूं और कंसोल विंडो जोड़ता हूं।

छीन सभी युक्त winapp संस्करण (भी निकाल देता है सब afx-कबाड़ फ़ाइलें):

// Modify the following defines if you have to target a platform prior to the ones specified below. 
// Refer to MSDN for the latest info on corresponding values for different platforms. 
#ifndef WINVER    // Allow use of features specific to Windows XP or later. 
#define WINVER 0x0501  // Change this to the appropriate value to target other versions of Windows. 
#endif 

#ifndef _WIN32_WINNT  // Allow use of features specific to Windows XP or later.     
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows. 
#endif      

#ifndef _WIN32_WINDOWS  // Allow use of features specific to Windows 98 or later. 
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later. 
#endif 

#define WIN32_LEAN_AND_MEAN  // Exclude rarely-used stuff from Windows headers 



// MinWInApp.cpp : Defines the entry point for the application. 
// 
#include <windows.h> 

// C RunTime Header Files 
#include <stdlib.h> 
#include <malloc.h> 
#include <memory.h> 
#include <tchar.h> 
#include <strsafe.h> 




#define MAX_LOADSTRING 100 
#define SZ_TITLE "MinWinApp" 
#define SZ_WND_CLASS "MINWINAPP" 


// Global Variables: 
HINSTANCE g_hInst;    // current instance 
HWND g_hWnd; 
HWND g_hwndNextViewer; 

TCHAR* szWndClass = TEXT(SZ_WND_CLASS); 


LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 


int APIENTRY _tWinMain(HINSTANCE hInstance, 
         HINSTANCE hPrevInstance, 
         LPTSTR lpCmdLine, 
         int  nCmdShow) 
{ 
    UNREFERENCED_PARAMETER(hPrevInstance); 
    UNREFERENCED_PARAMETER(lpCmdLine); 

    // TODO: Place code here. 



    MSG msg; 

    WNDCLASSEX wcex; 

    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style  = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc = WndProc; 
    wcex.cbClsExtra = 0; 
    wcex.cbWndExtra = 0; 
    wcex.hInstance = hInstance; 
    wcex.hIcon  = NULL; 
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1); 
    wcex.lpszMenuName = NULL; 
    wcex.lpszClassName = szWndClass; 
    wcex.hIconSm = NULL; 
    RegisterClassEx(&wcex); 


    g_hInst = hInstance; // Store instance handle in our global variable 
    g_hWnd = CreateWindow(szWndClass, TEXT(SZ_TITLE), WS_OVERLAPPEDWINDOW, 
    CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 

    if (!g_hWnd) 
    { 
    return FALSE; 
    } 

    ShowWindow(g_hWnd, SW_SHOW); 

    // Main message loop: 
    while (GetMessage(&msg, NULL, 0, 0)) 
    { 

    TranslateMessage(&msg); 
    DispatchMessage(&msg); 
    } 

    return (int) msg.wParam; 
} 


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    int wmId, wmEvent; 

    switch (message) 
    { 
    case WM_CREATE: 
    break; 
    case WM_COMMAND: 
    wmId = LOWORD(wParam); 
    wmEvent = HIWORD(wParam); 
    // Parse the menu selections: 
    switch (wmId) 
    { 
    case 0: 
    default: 
     return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
    break; 
    case WM_DESTROY: 
    PostQuitMessage(0); 
    break; 
    default: 
    return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
    return 0; 
} 
यहाँ

कंसोल विंडो जोड़ने के लिए कोड पाया जा सकता है: http://justcheckingonall.wordpress.com/2008/08/29/console-window-win32-app/

संबंधित मुद्दे

 संबंधित मुद्दे