如何(使用python)正确地创建ctypes结构并将其传递给werapi函数?

2024-10-01 09:20:07 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我发现了以下两个类似的问题:

Passing Structure to Windows API in python ctypes

ctypes and passing a by reference to a function

第一个问题没有一个公认的答案,我也不认为我是在不同的过程中做任何事情。第二个简单地指出pointer()和byref(),这两个我都试过使用,但都没有用。在

现在,请回答我的问题:

我试图用我自己的prepartinformation调用函数WERReportCreate(它是指向第一个数据值为自身大小的结构的指针)。这在很多方面都失败了,这取决于我如何去做,但我不确定如何正确地去做。这一点很复杂,因为其中一个要求是结构知道它自己的大小,我不知道如何用程序来确定它(尽管如果这是唯一的问题,我想我现在已经猜到了正确的值)。来自WER API的相关信息如下所示:

HRESULT WINAPI WerReportCreate(
  __in      PCWSTR pwzEventType,
  __in      WER_REPORT_TYPE repType,
  __in_opt  PWER_REPORT_INFORMATION pReportInformation,
  __out     HREPORT *phReportHandle
);

(完整信息请访问http://msdn.microsoft.com/en-us/library/windows/desktop/bb513625%28v=vs.85%29.aspx

^{pr2}$

(完整信息请访问http://msdn.microsoft.com/en-us/library/windows/desktop/bb513637%28v=vs.85%29.aspx

这是我尝试过的代码:

import ctypes
import ctypes.wintypes

class ReportInfo( ctypes.Structure):
    _fields_ = [ ("dwSize", ctypes.wintypes.DWORD),
                 ("hProcess", ctypes.wintypes.HANDLE),
                 ("wzConsentKey", ctypes.wintypes.WCHAR * 64),
                 ("wzFriendlyEventName", ctypes.wintypes.WCHAR * 128),
                 ("wzApplicationName", ctypes.wintypes.WCHAR * 128),
                 ("wzApplicationPath", ctypes.wintypes.WCHAR * ctypes.wintypes.MAX_PATH),
                 ("wzDescription", ctypes.wintypes.WCHAR * 512),
                 ("hwndParent", ctypes.wintypes.HWND) ]

def genReportInfo():
    import os
    size = 32 #Complete SWAG, have tried many values
    process = os.getpid()
    parentwindow = ctypes.windll.user32.GetParent(process)
    werreportinfopointer = ctypes.POINTER(ReportInfo)
    p_werinfo = werreportinfopointer()
    p_werinfo = ReportInfo(size, process, "consentkey", "friendlyeventname", "appname", "apppath", "desc", parentwindow)
    return p_werinfo

if __name__ == '__main__':
    reporthandle = ctypes.wintypes.HANDLE()
    res = ctypes.wintypes.HRESULT()

            ### First pass  NULL in as optional parameter to get default behavior ###
    p_werinfo = None
    res = ctypes.windll.wer.WerReportCreate(u'pwzEventType', 2, p_werinfo, ctypes.byref(reporthandle))
    print "Return Code",res,"\nHandle",reporthandle #Return Code 0, reporthandle is correct (verified by submitting report in a different test)

    p_werinfo = genReportInfo() # Create our own struct

            ### Try Again Using Our Own Struct (via 'byref')  ###      
    res = ctypes.windll.wer.WerReportCreate(u'pwzEventType', 2, ctypes.byref(p_werinfo), ctypes.byref(reporthandle))
    print "Return Code",res,"\nHandle",reporthandle #Return Code Nonzero, reporthandle is None

            ### Try Again Using Our Own Struct (directly)  ###
    res = ctypes.windll.wer.WerReportCreate(u'pwzEventType', 2, p_werinfo, ctypes.byref(reporthandle))
    print "Return Code",res,"\nHandle",reporthandle #Exception Occurs, Execution halts  

这是我得到的输出:

Return Code 0
Handle c_void_p(26085328)
Return Code -2147024809
Handle c_void_p(None)
Traceback (most recent call last):
  File "test.py", line 40, in <module>
    res = ctypes.windll.wer.WerReportCreate(u'pwzEventType', s.byref(reporthandle))
WindowsError: exception: access violation writing 0x0000087C

事实上,当我传入null时,它是有效的,但当我实际传递my(引用my?)时就不行了structure向我表明我有三个问题之一:我没有正确地创建结构(我不确定wzConsentKey是否正确定义),或者我没有正确地计算出结构的大小(我实际上正在使用结构calcsize用各种选项来获得最初的猜测,随机加减1),否则我就不正确了传递(引用?)结构。在

我在这里遇到了一个死胡同。任何帮助都将不胜感激(以及如何提高我问题的清晰度、格式或质量的建议;这是我的第一篇文章)。在


Tags: inreturncoderesctypes结构werwindll
2条回答

您可以使用ctypes.sizeof(ReportInfo)获得结构的字节大小。在

只需使用genReportInfo创建ReportInfo实例。此时不需要指针:

def genReportInfo():
    import os
    size = ctypes.sizeof(ReportInfo)
    process = os.getpid()
    parentwindow = ctypes.windll.user32.GetParent(process)
    return ReportInfo(size, process, "consentkey", "friendlyeventname", "appname", "apppath", "desc", parentwindow)

像这样叫WerReportCreatebyref传递指向ReportInfo实例的指针。在

^{pr2}$

我想这对你有用。我没有wer.dll所以我不能测试它。在

对这个问题的一般简短回答是:确保将正确的信息放入结构中,除此之外,所提供的代码是创建和传递结构的一个很好的例子。以下是解决我问题的具体方法:

提供的代码有两个问题:首先,正如marktolonen指出的,我传递的代码大小不正确。使用ctypes.sizeof的(ReportInfo)解决了这个问题。第二个问题是,我使用的进程ID需要进程句柄。使用OpenProcess获得一个有效的进程句柄来代替我的“process”参数解决了第二个问题。在

对于将来调试类似问题的任何人,请将HRESULT打印为十六进制数而不是整数,以便更好地理解返回代码:

print "Return Code %08x" % (res & 0xffffffff)

就我而言,这产生了以下结果:

^{pr2}$

对于我最初的错误,以及

Return Code 80070006

第二个错误。使用http://msdn.microsoft.com/en-us/library/bb446131.aspx上的信息,我看到前半部分是元数据,下半部分是我实际的错误代码。 在将十六进制数的错误代码部分转换回十进制数之后,我使用http://msdn.microsoft.com/en-us/library/bb202810.aspx来确定

错误代码87(十六进制57)表示“参数不正确”(大小错误) 以及

错误代码6(十六进制为6)意味着“句柄无效”(我正在传递一个进程ID)。在

相关问题 更多 >