GithubHelp home page GithubHelp logo

ynyyn / miniblink-python-simpledemo Goto Github PK

View Code? Open in Web Editor NEW
12.0 12.0 4.0 9 KB

A very simple Miniblink and Python 3 binding example via ctypes. Sketchy and rough prototype as it is, it might, someday in the future, become a not bad candidate to get a quick view of Miniblink. | Miniblink 的 Python 3 ctypes 绑定例子,目前只是一个非常简陋的原型,但也许未来某日能成为快速领略 Miniblink 特性的不错之选。

License: MIT License

Python 100.00%
desktop-app miniblink python webkit webui webview windows

miniblink-python-simpledemo's People

Contributors

ynyyn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

miniblink-python-simpledemo's Issues

使用最新版node.dll报错

使用的miniblink版本为 2019.11.22版

  1. 使用 node.dll 时报错如下:
λ python miniblink.py                                                              
Exception in thread Thread-1:                                                      
Traceback (most recent call last):                                                 
  File "D:\Anaconda3\lib\threading.py", line 926, in _bootstrap_inner              
    self.run()                                                                     
  File "miniblink.py", line 16, in run                                             
    mb = ctypes.cdll.LoadLibrary("node.dll")                                       
  File "D:\Anaconda3\lib\ctypes\__init__.py", line 442, in LoadLibrary             
    return self._dlltype(name)                                                     
  File "D:\Anaconda3\lib\ctypes\__init__.py", line 364, in __init__                
    self._handle = _dlopen(self._name, mode)                                       
OSError: [WinError 193] %1 不是有效的 Win32 应用程序。                                       
  1. 使用 miniblink_x64.dll 报错如下
λ python miniblink.py                                                              
Exception in thread Thread-1:                                                      
Traceback (most recent call last):                                                 
  File "D:\Anaconda3\lib\threading.py", line 926, in _bootstrap_inner              
    self.run()                                                                     
  File "miniblink.py", line 24, in run                                             
    mb.wkeMoveToCenter(webview)                                                    
OSError: exception: access violation reading 0x000000007A54EE08                    

请问该如何解决?

在 Python 3.8 上抛出异常 FileNotFoundError: Could not find module 'node.dll'

使用 Python 3.8 运行,启动时抛出 FileNotFoundError 异常,提示 Could not find module 'node.dll'. Try using the full path with constructor syntax.

Exception in thread Thread-1:
Traceback (most recent call last):
  File "...\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File ".\main.py", line 16, in run
    mb = ctypes.cdll.LoadLibrary("node.dll")
  File "...\Python38\lib\ctypes\__init__.py", line 451, in LoadLibrary
    return self._dlltype(name)
  File "...\Python38\lib\ctypes\__init__.py", line 373, in __init__
    self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'node.dll'. Try using the full path with constructor syntax.

64 位 Python + 64 位库并不能将目前的 Demo 跑起来(无法直接迁移使用 64 位)

背景

在本 Demo 在诞生之时及随后较长的一段时间内,Miniblink 都仅官方支持 32 位,也就是仅发布 32 位动态链接库,任何 64 位程序都无法直接加载使用,这也意味着只有使用 32 位 Python 才能运行本 Demo。

虽然没有官方发布的 x64 版,但非官方的 x64 编译版还是有的。

因此笔者在当时也做过使用 64 位库的技术验证。接下来将介绍的内容,也正是当时所得到的。但是由于当时无官方 x64 版,所以未公开相关技术内容和调整。

但是由于近期 Miniblink 增加了官方 x64 版本的支持,故现在有必要将该问题拿出来进行简单的说明。

64 位 Python + 64 位库并不能将目前的 Demo 跑起来(无法直接迁移使用 64 位)

受制于 ctypes 库的一些固有特性,目前本 Demo 需要略作修改才能切换到 64 位使用,无法直接迁移运行,即便使用 64 位的 Python 和配套的 64 位的动态链接库(miniblink_x64.dll)。

具体技术原因

ctypes 库对于任何未指定返回值类型(restype)的函数调用,默认认为其返回的是 int 类型

By default functions are assumed to return the C int type.

然而 64 位库函数的指针类型为 64 位。当 64 位指针返回时,如若先前未告知 ctytes 返回类型,则ctypes 遵循上面的默认规则,视作 int 类型,导致返回到 Python 侧的是 32 位截断值。

事实上,不仅返回值有该特性,参数值也有类似的特性。如使用 ctypes 调用库函数时未指定参数类型,当传入 Python 原生的 integer 类型时,会截断成 C 的 int 类型后传入。

Python integers are passed as the platforms default C int type, their value is masked to fit into the C type.

说了这么多,在本 Demo 中的一种反映就是:Miniblink 创建的 webview 很不巧地就是指针类型,而本 Demo 也很不巧地(作为一个很粗糙的原型)没有在使用 ctypes 函数调用前预先指定被调函数的参数列表和返回值。

        mb = ctypes.cdll.LoadLibrary("miniblink_x64.dll")
        webview = mb.wkeCreateWebWindow(
            0, # WKE_WINDOW_TYPE_POPUP
            None, 0, 0,
            1024, 768 # width, height
        )
        mb.wkeMoveToCenter(webview)

所以在 64 位 Miniblink 下,webview 的返回值被截断,在 Python 侧得到的 webview 值并不正确,并且后续调用中把错误的 webview 传给库函数,库函数引用了错误的内存而导致内存非法访问错误。

这就是目前 Demo 代码无法直接迁移到 64 位运行的最主要原因。欲修复该问题,需要预先为库函数指定参数和返回值类型(至少为不得不指定类型的函数指定)。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.