使用extern关键字的问题 [英] problem using extern keyword

查看:58
本文介绍了使用extern关键字的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是Win32 API的初学者 我在最近开始的一个小项目中遇到了 extern 关键字的问题。


这是我的一个例子尝试做,错误正在使用 一个简单的代码。

 RESOURCE.H 

extern HWND hwnd; //一切正常,没有"extern"
WNDCLASSEX wc;
MSG消息;




 LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam) 
{
switch(msg)
{

case WM_DESTROY:
{
if(hwnd ==(HWND)GetModuleHandle(NULL) )
PostQuitMessage(0);
其他
休息;
}
休息;

默认值:
返回DefWindowProc(hwnd,msg,wParam,lParam);
}
返回0;
}



int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow)
{


wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =(HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MAINMENU);
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

if(!RegisterClassEx(& wc))
{
MessageBox(NULL," Window Registration Failed!"," Error!",
MB_ICONEXCLAMATION | MB_OK);
返回0;
}


hwnd = CreateWindowEx(
0,
g_szClassName,
" theForger's Tutorial Application",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT,CW_USEDEFAULT,480,320,
NULL,NULL,hInstance,NULL);

if(hwnd == NULL)
{
MessageBox(NULL,"Window Creation Failed!","Error!",
MB_ICONEXCLAMATION | MB_OK);
返回0;
}

g_hMainWindow = hwnd;

ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);

while(GetMessage(& Msg,NULL,0,0)> 0)
{
TranslateMessage(& Msg);
DispatchMessage(& Msg);
}
返回Msg.wParam;
}

我得到一个"对hwnd的未确定引用"即使我在main.c中包含resource.h


在我的项目中,我实际上想要在另一个模块中访问此句柄,这就是我将其声明为extern的原因


对不起,如果这是错误的地方


感谢您的帮助



解决方案

我在http://www.gamedev.net/topic/366118-file-scope-extern/找到答案

Hi,

I am a beginner in Win32 API  and I am having a problem with extern keyword in a small project I recently started.

Here is an example of what I am trying to do and the error am getting using  a simple code.

RESOURCE.H

extern HWND hwnd; // Everything works fine without "extern"
WNDCLASSEX wc;
MSG Msg;


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{

		case WM_DESTROY:
		{
          if(hwnd == (HWND)GetModuleHandle(NULL))
		  PostQuitMessage(0);
		  else
		     break;
		}
		break;

		default:
			return DefWindowProc(hwnd , msg , wParam, lParam);
	}
	return 0;
}



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, int nCmdShow)
{


	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style		 = 0;
	wc.lpfnWndProc	 = WndProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MAINMENU);
	wc.lpszClassName = g_szClassName;
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}


	hwnd = CreateWindowEx(
		0,
		g_szClassName,
		"theForger's Tutorial Application",
		WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
		CW_USEDEFAULT, CW_USEDEFAULT, 480, 320,
		NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	g_hMainWindow = hwnd;

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

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

I get an "Undifined reference to hwnd" even though I included resource.h in my main.c

In my project i actually want to access this handle in another module and that's the reason I declared it as extern

Sorry if this is the wrong place to put this

Thanks for your help

解决方案

I found an answer at : http://www.gamedev.net/topic/366118-file-scope-extern/


这篇关于使用extern关键字的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆