python 非托管 dll 调用在 win32 FindWindow api 上失败并出现访问冲突
我在 DLL 中有以下最基本的函数,
#define INTELHOOK_API extern "C" __declspec(dllexport)
INTELHOOK_API BOOL testFunc(void) {
BOOL success = false;
HWND parent = NULL;
parent = FindWindow("Arium.SourcePont", NULL);
if (parent != NULL) {
success = true;
}
return success;
}
如果我从 DLL 中的主函数调用它,它可以正常工作。如果我从 python 调用它,我会得到以下内容:
WindowsError: exception: access violation reading 0x00439508
我的 python 脚本如下所示:
from ctypes import *
dll = cdll.hook
print dll.testFunc()
我在 win7,64 位上运行,但 dll 和 python 都是 32 位:
c:\Projects\hg\hooklib>dumpbin /headers hook.dll
Microsoft (R) COFF/PE Dumper Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file hook.dll
PE signature found
File Type: EXECUTABLE IMAGE
FILE HEADER VALUES
14C machine (x86)
'
<块引用> <块引用>打印系统版本 2.7.2(默认,2011 年 6 月 12 日,15:08:59)[MSC v.1500 32 位(Intel)]
我附加了一个调试器,但似乎无法解决它。
'python.exe': Loaded 'C:\Python27\python.exe'
'python.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\python27.dll'
'python.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\shell32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\shlwapi.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_50934f2ebcb7eb57\msvcr90.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\PGPmapih.dll'
'python.exe': Loaded 'C:\Python27\DLLs\_ctypes.pyd'
'python.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Projects\hg\hooklib\hook.dll', Symbols loaded.
The thread 'Win32 Thread' (0x10b0) has exited with code 0 (0x0).
First-chance exception at 0x01d428ae (hook.dll) in python.exe: 0xC0000005: Access violation reading location 0x00439508.
The program '[3836] python.exe: Native' has exited with code 1 (0x1).
当我通过 JNA 使用 Java 访问该函数时,也会发生同样的情况。 鉴于该函数在从 dll 中的 main 调用时起作用,让我相信这是一些访问限制,但这没有意义。我不可能是第一个在 dll 中间接调用 win32 函数的人...
提前感谢您花时间阅读所有这些!
干杯
I have the following bare minimum function in a DLL
#define INTELHOOK_API extern "C" __declspec(dllexport)
INTELHOOK_API BOOL testFunc(void) {
BOOL success = false;
HWND parent = NULL;
parent = FindWindow("Arium.SourcePont", NULL);
if (parent != NULL) {
success = true;
}
return success;
}
If I call this from the main function in the DLL it works fine. If I call it from python I get the following:
WindowsError: exception: access violation reading 0x00439508
My python script looks like this:
from ctypes import *
dll = cdll.hook
print dll.testFunc()
I'm running on win7, 64-bit but both the dll and python are 32-bit:
c:\Projects\hg\hooklib>dumpbin /headers hook.dll
Microsoft (R) COFF/PE Dumper Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file hook.dll
PE signature found
File Type: EXECUTABLE IMAGE
FILE HEADER VALUES
14C machine (x86)
'
print sys.version
2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]
I attached a debugger but can't seem to solve it.
'python.exe': Loaded 'C:\Python27\python.exe'
'python.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\python27.dll'
'python.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\shell32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\shlwapi.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_50934f2ebcb7eb57\msvcr90.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\PGPmapih.dll'
'python.exe': Loaded 'C:\Python27\DLLs\_ctypes.pyd'
'python.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Symbols loaded (source information stripped).
'python.exe': Loaded 'C:\Projects\hg\hooklib\hook.dll', Symbols loaded.
The thread 'Win32 Thread' (0x10b0) has exited with code 0 (0x0).
First-chance exception at 0x01d428ae (hook.dll) in python.exe: 0xC0000005: Access violation reading location 0x00439508.
The program '[3836] python.exe: Native' has exited with code 1 (0x1).
The same happens when I access the function with Java through JNA.
Given that the function works while calling from the main in the dll makes me believe it's some access restriction, but that doesn't make sense. I can't be the first one calling win32 functions indirectly in dll's...
Thank you in advance for taking the time to read all this!
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就其价值而言,这对我来说在使用 Python 3.2.1 和 2.7.2 的 32 位 Windows XP 中工作得很好,使用 i686-w64-mingw32-g++.exe 版本 4.5.3 编译:
c/test.cpp:
test。 py:
如果记事本窗口打开,则打印 1,否则打印 0。
For what it's worth, this works fine for me in 32-bit Windows XP with Python 3.2.1 and 2.7.2, compiled with i686-w64-mingw32-g++.exe version 4.5.3:
c/test.cpp:
test.py:
It prints 1 if a Notepad window is open, else 0.
@eryksun
谢谢你的尝试。你的回答引导我找到解决方案。
我在编译时缺少相当于 GCC 的 -shared (即 /LD)的 MSVC。
我最初从 C 语言开发和测试了 DLL 函数,然后添加了导出,但忘记添加 /LD 选项。
我会将这个问题标记为已解决,但我仍然想知道如果我没有适当的 DllMain,为什么它会导致访问冲突。
@eryksun
Thank you for trying. Your answer lead me to find the solution.
I was missing the MSVC equivalent to GCC's -shared (which is /LD) while compiling.
I initially developed and tested the DLL functions from C, then added exports but forgot to add the /LD option.
I'll mark this question as solved, but I still wonder why it causes an access violation if I don't have a proper DllMain.