python unmanaged dll调用,在具有访问冲突的win32 FindWindow api上失败

时间:2011-09-22 20:41:42

标签: python dll unmanaged access-violation

我在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中的main函数调用它,它可以正常工作。如果我从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)

  
    
      

print sys.version           2.7.2(默认,2011年6月12日,15:08:59)[MSC v.1500 32位(英特尔)]

    
  

我附加了一个调试器但似乎无法解决它。

'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函数的人......

提前感谢您花时间阅读所有这些内容!

干杯

2 个答案:

答案 0 :(得分:2)

对于它的价值,这适用于使用Python 3.2.1和2.7.2的32位Windows XP,使用i686-w64-mingw32-g ++编译.exe版本4.5.3:

C / TEST.CPP:

#include <windows.h>

#define INTELHOOK_API extern "C" __declspec(dllexport)
INTELHOOK_API BOOL test(void) {
    BOOL success = FALSE;
    HWND parent = NULL;

    parent = FindWindow("notepad", NULL);
    if (parent != NULL) {
        success = TRUE;
    }
    return success;
}

// g++ test.cpp -o test.dll -shared

test.py:

import ctypes
dll = ctypes.cdll.LoadLibrary('c/test.dll')
print(dll.test())

如果记事本窗口打开,则打印1,否则为0。

答案 1 :(得分:1)

@eryksun 谢谢你的尝试。你的回答让我找到解决方案。

我在编译时错过了与GCC的共享(即/ LD)相当的MSVC。

我最初从C开发并测试了DLL函数,然后添加了导出但忘记添加/ LD选项。

我会将此问题标记为已解决,但我仍然想知道如果我没有正确的DllMain,会导致访问冲突。