源代码是别人发给我的,我看了看表头也没有错啊,是不是代码这边有错。本人是小白,求各位大佬告知一下,谢谢了。
下面是代码:
#include "stdafx.h"
#include "ConsoleApplication1.h"
#include <stdio.h>
#include <windows.h>
#include <string.h>
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#pragma comment(lib, "lua5.1.lib")
lua_State *L;
HWND HMainwnd = NULL;
HWND hwndButton = NULL;
int run();
bool InitWindowsApp(HINSTANCE instanceHandle, int show);
LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp);
//main函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
if (!InitWindowsApp(hInstance, nShowCmd))
{
return 0;
}
run();
return 0;
}
bool InitWindowsApp(HINSTANCE stanceHandle, int show)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = stanceHandle;
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "BaseWndClass";
if (!RegisterClass(&wc))
{
MessageBox(0, "注册失败", 0, 0);
return false;
}
HMainwnd = CreateWindow("BaseWndClass","xxx(12345)",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,300,300,NULL,NULL,stanceHandle,NULL);
hwndButton = CreateWindow("BUTTON", "Operate", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 100, 100, 100, 60, HMainwnd, (HMENU)520, (HINSTANCE)GetWindowLong(HMainwnd, GWL_HINSTANCE), NULL);
if (HMainwnd == 0)
{
MessageBox(0, "创建窗体失败", 0, 0);
return false;
}
ShowWindow(HMainwnd, SW_SHOW);
UpdateWindow(HMainwnd);
return true;
}
HWND hButton;
HDC hdc;
PAINTSTRUCT ps;
RECT rc;
LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg)
{
case WM_PAINT:
hdc = BeginPaint(wnd, &ps);
SetRect(&rc, 10, 10, 500, 30);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_COMMAND:
if (LOWORD(wp) == 520 && HIWORD(wp) == BN_CLICKED)
{
//1.创建Lua状态
lua_State *L = luaL_newstate();
if (L == NULL)
{
MessageBox(0, TEXT("lua创建失败"), TEXT("提示"), MB_OK);
}
//2.加载Lua文件
int bRet = luaL_loadfile(L, "op.lua");
if (bRet)
{
MessageBox(0, TEXT("加载文件错误"), TEXT("提示"), MB_OK);
}
//3.运行Lua文件
bRet = lua_pcall(L, 0, 0, 0);
if (bRet)
{
MessageBox(0, TEXT("pcall错误"), TEXT("提示"), MB_OK);
}
//6.读取函数
lua_getglobal(L, "as1231d");
lua_pushnumber(L, 40);
lua_pushnumber(L, 20);
int iRet = lua_pcall(L, 2, 1, 0);
if (iRet)
{
const char *pErrorMsg = lua_tostring(L, -1);
MessageBox(0, TEXT(pErrorMsg), TEXT("提示"), MB_OK);
lua_close(L);
}
if (lua_isnumber(L, -1)) //取值输出
{
double fValue = lua_tonumber(L, -1);
FrameRect(hdc, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));
DrawText(hdc,
TEXT(buf),-1, &rc, DT_LEFT);
EndPaint(wnd, &ps);
}
lua_close(L);
}
return 0;
}
return DefWindowProc(wnd, msg, wp, lp);
}
int run()
{
MSG msg = { 0 };
BOOL bRet = 1;
while ((bRet = GetMessage(&msg, 0, 0, 0)) != 0)
{
if (bRet == -1)
{
MessageBox(0, "接受消息失败", 0, 0);
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}