gpt4 book ai didi

使用 LuaJIT 调用标准打开对话框会导致访问冲突

转载 作者:太空宇宙 更新时间:2023-11-04 01:37:32 25 4
gpt4 key购买 nike

已关注 MSDN's GetOpenFileName example使用 LuaJIT 的 FFI。这两天我一直在努力让它工作,不仅对话框打不开,而且整个东西都崩溃了。

当使用 OllyDdb 进行调试时,它会在 comdlg32.dll 中的某处引发丑陋的访问冲突。

这是仅包含基本代码的概念验证。如果我指定结构大小为零,它会毫无问题地退出,GetLastError() 会抛出 87 (ERROR_INVALID_PARAMETER)

所以我认为问题与结构或 FFI 相关。

如果有人能够在这里阐明一些...谢谢。

ffi = require"ffi"
bit = require"bit"

collectgarbage("stop")

ffi.cdef[[
static const int OFN_FILEMUSTEXIST = 0x1000;
static const int OFN_NOCHANGEDIR = 8;
static const int OFN_PATHMUSTEXIST = 0x800;

typedef bool BOOL;
typedef char CHAR;

typedef unsigned short WORD;
typedef unsigned long DWORD;

typedef void *PVOID;
typedef void *LPVOID;
typedef void *LPOFNHOOKPROC;

typedef unsigned long HANDLE;
typedef HANDLE HWND;
typedef HANDLE HINSTANCE;

typedef const char *LPCSTR;
typedef const char *LPCTSTR;

typedef char *LPSTR;
typedef char *LPTSTR;

typedef unsigned long LPARAM;

typedef struct {
DWORD lStructSize;
HWND hwndOwner;
HINSTANCE hInstance;
LPCTSTR lpstrFilter;
LPTSTR lpstrCustomFilter;
DWORD nMaxCustFilter;
DWORD nFilterIndex;
LPTSTR lpstrFile;
DWORD nMaxFile;
LPTSTR lpstrFileTitle;
DWORD nMaxFileTitle;
LPCTSTR lpstrInitialDir;
LPCTSTR lpstrTitle;
DWORD flags;
WORD nFileOffset;
WORD nFileExtension;
LPCTSTR lpstrDefExt;
LPARAM lCustData;
LPOFNHOOKPROC lpfnHook;
LPCTSTR lpTemplateName;

LPVOID pvReserved;
DWORD dwReserved;
DWORD flagsEx;

}OPENFILENAME;

BOOL GetSaveFileNameA( OPENFILENAME lpofn );
BOOL GetOpenFileNameA( OPENFILENAME lpofn );
]]
com=ffi.load("comdlg32")

ffi.cdef[[
DWORD GetLastError(void);
]]
krnl=ffi.load("kernel32")

function OpenDialog()
Ofn=ffi.new("OPENFILENAME")
ffi.fill(Ofn,ffi.sizeof(Ofn)) --zero fill the structure

local szFile = ffi.new("char[260]","\0")
local hwnd = ffi.new("HWND",0)

Ofn.lStructSize = ffi.sizeof(Ofn)
Ofn.hwndOwner = hwnd

Ofn.lpstrFile = szFile
Ofn.nMaxFile = ffi.sizeof(szFile)

Ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0"
Ofn.nFilterIndex = 1

Ofn.lpstrFileTitle = nil
Ofn.nMaxFileTitle = 0

Ofn.lpstrInitialDir = nil
Ofn.flags = bit.bor(com.OFN_PATHMUSTEXIST, com.OFN_FILEMUSTEXIST, com.OFN_NOCHANGEDIR)

print("displaying...")

if com.GetOpenFileNameA(Ofn) then --luajit converts bool automatically
print("file->",ffi.string(Ofn.lpstrFile, Ofn.nMaxFile))
end

print("lasterror->",krnl.GetLastError())
end

OpenDialog()

最佳答案

这些 C 函数采用指向结构的指针。正确的声明是:GetOpenFilenameA(OPENFILENAME *lpofn)

关于使用 LuaJIT 调用标准打开对话框会导致访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11846910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com