ERROR_THREAD_MODE_ALREADY_BACKGROUND
400 (0x190) The thread is already in background processing mode.怎么回有这样的错误? 我写了一个很简单的Win32程序,只是想显示一个最简单的窗口,如下代码:
#include<Windows.h>
#include<stdio.h>
const char className[]="mywndclass";
LRESULT __stdcall myproc(HWND hWnd,UINT uMsg,WPARAM wp,LPARAM lp){
return 0L;
}
int __stdcall WinMain(
HINSTANCE hInst,
HINSTANCE hPrev,
LPSTR lpCmdLine,
int nCmdShow
){
WNDCLASSEX wnd;
ZeroMemory(&wnd,sizeof(wnd));
wnd.cbSize=sizeof(wnd);
wnd.lpszClassName=className;
wnd.lpszMenuName=NULL;
wnd.hInstance=hInst;
wnd.lpfnWndProc=myproc;
wnd.hbrBackground=(HBRUSH)MAKEINTRESOURCE(GRAY_BRUSH);
HWND hWnd=CreateWindowEx(0,className,"窗口名称",0,0,0,300,200,NULL,NULL,hInst,0);
if(INVALID_HANDLE_VALUE==hWnd){
MessageBox(NULL,"Error!","CreateWindowEx失败\n",MB_OK);
return 1;
}
if(!ShowWindow(hWnd,SW_SHOW)){
char dip[200];
sprintf(dip,"ShowWindow失败:%d\n",GetLastError());
MessageBox(NULL,"Error!",dip,MB_OK);
return 1;
}
return 0;
}

解决方案 »

  1.   

    就一个最简单的exe啊,别的什么也没有。
    winproc是空的,这个没有什么问题吧
      

  2.   

    RegisterClassEx(&wnd);     HWND hWnd=CreateWindowEx(0,className,"窗口名称", WS_OVERLAPPEDWINDOW |  WS_HSCROLL |   WS_VSCROLL
    ,0,0,300,200,NULL,NULL,hInst,0);
        if(!hWnd){
            MessageBox(NULL,"Error!","CreateWindowEx失败\n",MB_OK);
            return 1;
        }
      

  3.   

    1)加入RegisterClassEx
    2)判断hwnd,应该是非0,而不是-1。
      

  4.   

    ++也需看看ShowWindow返回码的描述:
    Nonzero indicates that the window was previously visible. Zero indicates that the window was previously hidden. 
      

  5.   


    我按照你说的两条改了一下。但是问题是现在CreateWindowEx虽然成功了(GetLastError=0),但是返回hWnd=0!
    这是为什么呢? 
    现在的代码是:#include<Windows.h>
    #include<stdio.h>
    const char className[]="mywndclass";
    LRESULT __stdcall myproc(HWND hWnd,UINT uMsg,WPARAM wp,LPARAM lp){
    return 0L;
    }
    int __stdcall WinMain(
    HINSTANCE hInst,
    HINSTANCE hPrev,
    LPSTR lpCmdLine,
    int nCmdShow
    ){
    WNDCLASSEX wnd;
    ZeroMemory(&wnd,sizeof(wnd));
    wnd.cbSize=sizeof(wnd);
    wnd.lpszClassName=className;
    wnd.lpszMenuName=NULL;
    wnd.hInstance=hInst;
    wnd.lpfnWndProc=myproc;
    wnd.hbrBackground=(HBRUSH)MAKEINTRESOURCE(GRAY_BRUSH);
    ATOM reg;
    if((reg=RegisterClassEx(&wnd))==0){
    MessageBox(NULL,"RegisterClassEx失败\n","有错!",MB_OK);
    return 1;
    }
    char dip[200];
    HWND hWnd=CreateWindowEx(0,className,"窗口名称",
    WS_OVERLAPPEDWINDOW |  WS_HSCROLL |   WS_VSCROLL,0,0,300,200,NULL,NULL,hInst,0);
    if(0==hWnd){
    sprintf(dip,"CreateWindowEx失败:%d\n",GetLastError());
    MessageBox(NULL,dip,"有错r!",MB_OK);
    return 1;
    }
    if(!ShowWindow(hWnd,SW_SHOW)){
    sprintf(dip,"ShowWindow失败:%d\n",GetLastError());
    MessageBox(NULL,dip,"有错!",MB_OK);
    return 1;
    }
    return 0;
    }
      

  6.   

    你并没有注册窗口类
    RegisterClassEx并且尚需判断窗口是否创建成功
    IsWindow
      

  7.   

    照msdn的demo写就行了,俺就是比较着改的。我那窗口也没出来,懒得调了。// test.cpp : Defines the entry point for the application.
    //#include "stdafx.h"
    #include<Windows.h>
    #include<stdio.h>
    char className[]="mywndclass";
    LRESULT __stdcall myproc(HWND hWnd,UINT uMsg,WPARAM wp,LPARAM lp){
    MSG msg;
        while (GetMessage(&msg, (HWND) NULL, 0, 0) != 0 && GetMessage(&msg, (HWND) NULL, 0, 0) != -1) 
        { 
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        } return 0;
    }
    int __stdcall WinMain(
        HINSTANCE hInst,
        HINSTANCE hPrev,
        LPSTR lpCmdLine,
        int nCmdShow
    ){
        WNDCLASSEX wnd;
        ZeroMemory(&wnd,sizeof(wnd));
        wnd.cbSize=sizeof(wnd);
        wnd.lpszClassName=className;
        wnd.lpszMenuName=NULL;
        wnd.hInstance=hInst;
        wnd.lpfnWndProc=myproc;
        wnd.hbrBackground=(HBRUSH)MAKEINTRESOURCE(GRAY_BRUSH);
    RegisterClassEx(&wnd);     HWND hWnd=CreateWindowEx(0,className,"窗口名称", WS_OVERLAPPEDWINDOW |  WS_HSCROLL |   WS_VSCROLL
    ,0,0,300,200,NULL,NULL,hInst,0);
        if(!hWnd){
            MessageBox(NULL,"Error!","CreateWindowEx失败\n",MB_OK);
            return 1;
        }
        if(!ShowWindow(hWnd,SW_SHOW)){
            char dip[200];
            sprintf(dip,"ShowWindow失败:%d\n",GetLastError());
            MessageBox(NULL,"Error!",dip,MB_OK);
            return 1;
        }
        return 0;
    }