ShowWindow错误,GetLastError返回400,怎么回事?

somebd 2010-11-05 01:54:46
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;
}
...全文
148 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
用户 昵称 2010-11-05
  • 打赏
  • 举报
回复
照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;
}

qsycn 2010-11-05
  • 打赏
  • 举报
回复
你并没有注册窗口类
RegisterClassEx

并且尚需判断窗口是否创建成功
IsWindow
somebd 2010-11-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jennyvenus 的回复:]

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

我按照你说的两条改了一下。但是问题是现在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;
}
dong918 2010-11-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jennyvenus 的回复:]
1)加入RegisterClassEx
2)判断hwnd,应该是非0,而不是-1。
[/Quote]
++

也需看看ShowWindow返回码的描述:
Nonzero indicates that the window was previously visible. Zero indicates that the window was previously hidden.
tigerMayDo 2010-11-05
  • 打赏
  • 举报
回复
不懂帮顶 是不是没加StdAfx.h
用户 昵称 2010-11-05
  • 打赏
  • 举报
回复
1)加入RegisterClassEx
2)判断hwnd,应该是非0,而不是-1。
a220315410 2010-11-05
  • 打赏
  • 举报
回复
项目类型是什么?
用户 昵称 2010-11-05
  • 打赏
  • 举报
回复
	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;
}
somebd 2010-11-05
  • 打赏
  • 举报
回复
就一个最简单的exe啊,别的什么也没有。
winproc是空的,这个没有什么问题吧
a220315410 2010-11-05
  • 打赏
  • 举报
回复
代码所在的线程为后台线程?可能问题不在你贴的代码,而在于你调用代码的地方。

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧