Delphi loadlibrary() 中的错误
我给了我的软件用户一个从 openfile 对话框中选择 dll 的机会。(这样我的用户就可以从我的网站下载 dll 并将其与主项目一起使用)。一切工作正常,它甚至可以发现 dll 是由我提供的或选择了无效的 dll。但是如果用户选择重命名的文件(例如: apple.txt 文件重命名为 apple.dll ),则会出现问题。我像这样输入代码
尝试 dllHandle := LoadLibrary( pwidechar(openfiledialog1.filename)) ;
抓住 { 如果它不是 dll,则显示消息(但它可以是任何 dll,稍后它会检查这是我的 dll 还是第 3 方)}
end;
delphi 显示的错误消息是“选择了错误的库图像”,
但如果用户选择无效的 dll,则 try catch 不起作用,它会显示自己的错误消息并启动。
谁能帮助我,我正在使用delphi 2009
i have given a chance to my software user to select dll from openfile dialog.(so my user can download dlls form my website and use it with the main project ). everything is working fine and it can even find that dlls is provided by me or selected an invalid dll.but the problem raises if the user selects a renamed file(eg : apple.txt file renamed to apple.dll ). i typed the code like this
try
dllHandle := LoadLibrary( pwidechar(openfiledialog1.filename)) ;
catch
{ showmessage if it is not a dll (but it can be any dll, it checks this is my dll or 3rd party later )}
end;
error message shown by delphi is 'bad library image selected'
but try catch is not working if the user selects invalid dll it is showing its own error message and struck up.
can anyone help me,i am using delphi 2009
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有要捕获的异常,因为 < 时不会引发异常代码>LoadLibrary失败;它只是返回“0”。
您应该检查“dllHandle”是否为0,如果是,则使用
GetLastError
如文档所述。或者,您可以使用Win32Check
函数将引发异常并显示相应的错误消息:(
编辑:“LoadLibrary”文档指出:
要启用或禁用由DLL 加载期间加载程序,请使用 SetErrorMode 函数。
因此,如果您不希望操作系统显示额外的对话框,您可以在调用 LoadLibrary 之前设置错误模式。)There's no exception to catch because an exception is not raised when
LoadLibrary
fails; it just returns '0'.You should check if 'dllHandle' is 0 or not, if it is, show the error information to the user by using
GetLastError
as documented. Alternatively you can use theWin32Check
function in the RTL which will raise an exception with the appropriate error message:(edit: Documentation of 'LoadLibrary' states that:
To enable or disable error messages displayed by the loader during DLL loads, use the SetErrorMode function.
So if you don't want the OS to show an additional dialog you'd set the error mode before calling LoadLibrary.)