真是很奇怪的问题:
这时mfc上的例子
全部代码如下://----------------------------------------------------------------
// 文件名:generic.h
//----------------------------------------------------------------BOOL InitApplication(HANDLE);
BOOL InitInstance(HANDLE,int);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK About(HWND,UINT,WPARAM,LPARAM);
//-----------------------------------
//文件名:resource.h
//-----------------------------------
#ifndef __RESOURCE_H__#define __RESOURCE_H__#define IDM_NEW 102
#define IDM_SAVE 103
#define IDM_EXIT 104
#define IDM_ABOUT 105#endif//----------------------------------------
//文件名:Generic.rc
//-----------------------------------------
#include "windows.h"
#include "resource.h"jjhouricon ICON "jjhour.ico";GenericMenu MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "&New",IDM_NEW,GRAYED
MENUITEM "Save",IDM_SAVE,GRAYED
MENUITEM "E&xit",IDM_EXIT
END
POPUP "&Help"
BEGIN
MENUITEM "ABout...",IDM_ABOUT
END
ENDAboutBox DIALOG 22,17,144,75
STYLE DS_MODALFRAME|WS_CAPTION|WS_SYSMENU
CAPTION "About generic"
BEGIN
CTEXT "Window2000",-1,0,5,144,8
CTEXT "Generic Application",-1,0,14,144,8
CTEXT "Version 1.0",-1,0,34,144,8
DEFPUSHBUTTON "OK",IDOK,53,59,32,14,WS_GROUP
END//----------------------------------------------------------------
// GENERIC--WIN32程序的基础写法
//文件名:GENERIC.CPP
//
//----------------------------------------------------------------#include <windows.h>
#include "resource.h"
#include "generic.h"HINSTANCE _hInst;
HWND _hWnd;
char _szAppName[]="Generic";
char _szTitle[]="Generic Sample Application";
//----------------------------------------------------------------
// WinMain---程序进入点
//
//----------------------------------------------------------------int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
if (!hInstance)
if (!InitApplication(hInstance))
return FALSE;
//判断初始化是否成功
if (!InitInstance(hInstance,nCmdShow))
return FALSE;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}//----------------------------------------------------------------
//InitInstance--注册窗口类别
//
//----------------------------------------------------------------BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wc;
wc.style =CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc =(WNDPROC)WndProc;
wc.cbClsExtra =0;
wc.cbWndExtra =0;
wc.hInstance =hInstance;
wc.hIcon =LoadIcon(hInstance,IDI_WINLOGO);
wc.hCursor =LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName =NULL;
wc.lpszClassName =_szAppName;

//注册创建窗体的资源
return RegisterClass(&wc);
}
//----------------------------------------------------------------
// InitInstance---产生窗体
//
//
//----------------------------------------------------------------BOOL InitInstance(HINSTANCE hInstance,int nCmdShow)
{
_hInst=hInstance;
_hWnd=CreateWindow(_szAppName,_szTitle,                    WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (!_hWnd)
return FALSE; ShowWindow(_hWnd,nCmdShow);
UpdateWindow(_hWnd);
return TRUE;}
//----------------------------------------------------------------
// WndProc--窗体函数
//
//
//----------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
int wmId,wmEvent; switch(message)
{
case WM_COMMAND:
wmId =LOWORD(wParam);
wmEvent =HIWORD(wParam);
switch(wmId)
{
case IDM_ABOUT:
DialogBox(_hInst,
"AboutBox",
hWnd,
(DLGPROC)About
);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
break; case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
//----------------------------------------------------------------
// About--弹出框函数
//
//
//----------------------------------------------------------------
LRESULT CALLBACK About(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam)==IDOK || LOWORD(wParam)==IDCANCEL)
{
EndDialog(hDlg,TRUE);
return TRUE;
}
break;
}
return FALSE;
}以上代码不能通过编译

new_1 error LNK2019: 无法解析的外部符号 "int __cdecl InitInstance(void *,int)" (?InitInstance@@YAHPAXH@Z) ,该符号在函数 _WinMain@16 中被引用
new_1 error LNK2019: 无法解析的外部符号 "int __cdecl InitApplication(void *)" (?InitApplication@@YAHPAX@Z) ,该符号在函数 _WinMain@16 中被引用
new_1 fatal error LNK1120: 2 个无法解析的外部命令而如果把InitApplication和InitInstance这两个基本点函数的内容直接写到winmain
中,就可以正常通过编译并执行.
如:int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
/*if (!hInstance)
if (!InitApplication(hInstance))
return FALSE;*/

WNDCLASS wc;
wc.style =CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc =(WNDPROC)WndProc;
wc.cbClsExtra =0;
wc.cbWndExtra =0;
wc.hInstance =hInstance;
wc.hIcon =LoadIcon(hInstance,IDI_WINLOGO);
wc.hCursor =LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName ="GenericMenu";
wc.lpszClassName =_szAppName;


RegisterClass(&wc);
_hInst=hInstance; _hWnd=CreateWindow(_szAppName,
_szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
//if (!_hWnd)
//return FALSE; ShowWindow(_hWnd,nCmdShow);
UpdateWindow(_hWnd);
//判断初始化是否成功
/*if (!InitInstance(hInstance,nCmdShow))
return FALSE;*/
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}这是为什么呢?如果写到函数里的话应该怎么写才正确
我的环境是vs2003
请各位帮忙。~~~~~~~~~

解决方案 »

  1.   

    是很奇怪,你试试将这两个函数改个名字,如将 InitInstance改成 InitInstance1试试
      

  2.   

    new_1 error LNK2019: 无法解析的外部符号 "int __cdecl InitInstance(void *,int)" (?InitInstance@@YAHPAXH@Z) ,该符号在函数 _WinMain@16 中被引用没有BOOL的定义?
      

  3.   

    BOOL?
    这个还需要定义?
    怎么定义?
    我查找定义能找到阿
    typedef int                 BOOL;
    在windef.h中
      

  4.   

    new_1 error LNK2019: 无法解析的外部符号 "int __cdecl InitInstance(void *,int)" (?InitInstance@@YAHPAXH@Z) ,该符号在函数 _WinMain@16 中被引用
    new_1 error LNK2019: 无法解析的外部符号 "int __cdecl InitApplication(void *)" (?InitApplication@@YAHPAX@Z) ,该符号在函数 _WinMain@16 中被引用
    new_1 fatal error LNK1120: 2 个无法解析的外部命令似乎是你改了编译器的默认设置
      

  5.   

    BOOL InitApplication(HANDLE);
    BOOL InitInstance(HANDLE,int);
    ------------------  上面 HANDLE  ----------------
    ------------------  下面 HINSTANCE --------------
    BOOL InitInstance(HINSTANCE hInstance,int nCmdShow)
    {
    }
    BOOL InitApplication(HINSTANCE hInstance)
    {
    }
      

  6.   

    这个问题太简单了, 在 generic.cpp 文件开始的位置添加那两个函数的声名好可:
    BOOL InitInstance(HINSTANCE hInstance,int nCmdShow);
    BOOL InitApplication(HINSTANCE hInstance);
      

  7.   

    所以, generic.cpp 的全文是:
    //----------------------------------------------------------------
    //GENERIC--WIN32程序的基础写法
    //文件名:GENERIC.CPP
    //
    //----------------------------------------------------------------#include <windows.h>
    #include "resource.h"
    #include "generic.h"HINSTANCE _hInst;
    HWND _hWnd;
    char _szAppName[]="Generic";
    char _szTitle[]="Generic Sample Application";
    BOOL InitInstance(HINSTANCE hInstance,int nCmdShow);
    BOOL InitApplication(HINSTANCE hInstance);
    //----------------------------------------------------------------
    //WinMain---程序进入点
    //
    //----------------------------------------------------------------int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
    MSG msg;
    if (!hInstance)
    if (!InitApplication(hInstance))
    return FALSE;
    //判断初始化是否成功
    if (!InitInstance(hInstance,nCmdShow))
    return FALSE;
    while(GetMessage(&msg,NULL,0,0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return (msg.wParam);
    }//----------------------------------------------------------------
    //InitInstance--注册窗口类别
    //
    //----------------------------------------------------------------BOOL InitApplication(HINSTANCE hInstance)
    {
    WNDCLASS wc;
    wc.style=CS_HREDRAW|CS_VREDRAW;
    wc.lpfnWndProc=(WNDPROC)WndProc;
    wc.cbClsExtra=0;
    wc.cbWndExtra=0;
    wc.hInstance=hInstance;
    wc.hIcon=LoadIcon(hInstance,IDI_WINLOGO);
    wc.hCursor=LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName=NULL;
    wc.lpszClassName=_szAppName;//注册创建窗体的资源
    return RegisterClass(&wc);
    }
    //----------------------------------------------------------------
    //InitInstance---产生窗体
    //
    //
    //----------------------------------------------------------------BOOL InitInstance(HINSTANCE hInstance,int nCmdShow)
    {
    _hInst=hInstance;
    _hWnd=CreateWindow(_szAppName,_szTitle,                   WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    NULL,
    NULL,
    hInstance,
    NULL
    );
    if (!_hWnd)
    return FALSE;ShowWindow(_hWnd,nCmdShow);
    UpdateWindow(_hWnd);
    return TRUE;}
    //----------------------------------------------------------------
    //WndProc--窗体函数
    //
    //
    //----------------------------------------------------------------
    LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
    int wmId,wmEvent;switch(message)
    {
    case WM_COMMAND:
    wmId=LOWORD(wParam);
    wmEvent=HIWORD(wParam);
    switch(wmId)
    {
    case IDM_ABOUT:
    DialogBox(_hInst,
    "AboutBox",
    hWnd,
    (DLGPROC)About
    );
    break;
    case IDM_EXIT:
    DestroyWindow(hWnd);
    break;
    default:
    return DefWindowProc(hWnd,message,wParam,lParam);
    }
    break;case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd,message,wParam,lParam);
    }
    return 0;
    }
    //----------------------------------------------------------------
    //About--弹出框函数
    //
    //
    //----------------------------------------------------------------
    LRESULT CALLBACK About(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
    {
    switch(message)
    {
    case WM_INITDIALOG:
    return TRUE;
    case WM_COMMAND:
    if (LOWORD(wParam)==IDOK || LOWORD(wParam)==IDCANCEL)
    {
    EndDialog(hDlg,TRUE);
    return TRUE;
    }
    break;
    }
    return FALSE;
    }
      

  8.   

    原来是这样...HINSTANCE一般是一个程序实例句柄,HANDLE呢?他一般是代表什么句柄?
      

  9.   

    在WinDef.h有这么一句:typedef HANDLE HINSTANCE; 
    数据上两者是一样的,本质上没什么区别。 HANDLE是用来标记资源的,也就是handle to an object。 
    HINSTANCE是Handle to an instance, 是HANDLE的一种特殊情况,常用来标记 
    App实例。 用HINSTANCE而不是HANDLE只是给用者一种说明的作用。