gpt4 book ai didi

Passing pointer to ctypes.Structure(将指针传递给ctye.Structure)

转载 作者:bug小助手 更新时间:2023-10-25 17:23:42 27 4
gpt4 key购买 nike



I am trying to figure out how to implement a function that waits for an event to be signaled.
The pointer is returned by a dll function which is a struct that stores 3 items. Two of them which are handles which are just pointers and last is some random unused pointer.
I am really unsure how this is supposed to be formatted as I both have no experienced working with ctypes or events in Windows and I don't know C++.

我正在试图弄清楚如何实现一个等待事件发出信号的函数。指针由DLL函数返回,该函数是存储3个项的结构。其中两个是句柄,它们只是指针,最后是一些随机的未使用的指针。我真的不确定这应该如何格式化,因为我两个人都没有在Windows中使用ctype或事件的经验,我也不懂C++。


Right now it does return something but the value in the 3 items in the struct are just small int values(value less than 1000 each). I thought it was the pointer address cut in 3 but it was not.

现在它确实返回了一些东西,但结构中3个项中的值只是小的int值(每个值都小于1000)。我以为是指针地址被切成了3个,但事实并非如此。


Anyone can help me figuring out why it doesn't work? I can't figure out what I am doing wrong OR find a DLL I could use to provide a functional example

有没有人能帮我弄清楚为什么它不管用?我找不出我做错了什么,也找不到我可以用来提供函数示例的DLL


Example code:

示例代码:


class MyStruct(ctypes.Structure):
_fields_ = [("Event1", wintypes.HANDLE),
("Event2", wintypes.HANDLE),
("Private", ctypes.POINTER(ctypes.c_void_p)),
]

some_dll_that_returns_the_pointer.restype = ctypes.POINTER(MyStruct)
my_struct = some_dll_that_returns_data()

while win32event.WaitForMultipleObjects([my_struct.contents.Event1], 0, win32event.INFINITE):
pass

更多回答

Looks correct. Are you sure that function returns the correct (waitable) values? What's the actual behavior? Can you show the C structure and function prototype (or some documentation)?

看起来没问题。您确定函数返回正确的(可等待的)值吗?实际的行为是什么?你能展示一下C语言的结构和功能原型(或一些文档)吗?

Thank you for confirming. Been going though this multiple times and couldn't wrap my head around why it didn't work. What i just find weird is that if i look at the my_struct object in PyCharm it does not show the data as a struct, so I would assume you need to unpack the pointer to the struct somehow. I sadly cannot provide any of the information as it is some of that company internal secret kind of stuff. I have also been wondering a lot about if the function even works. But since it works in other cases it must work

感谢您的确认。我已经试过很多次了,我不明白为什么它不起作用。我只是觉得奇怪的是,如果我查看PyCharm中的my_struct对象,它不会将数据显示为结构,因此我假设您需要以某种方式解压指向结构的指针。遗憾的是,我不能提供任何信息,因为这是公司内部的一些秘密。我也一直在想,这个功能是否有效。但因为它在其他情况下有效,所以它一定有效

I think with you confirming that my values being correct, I can try to focus on the WaitForMultipleObjects part, I could also be facing that the method is not working and that the pointers is actually returned correctly.

我认为在您确认我的值是正确的情况下,我可以尝试专注于WaitForMultipleObjects部分,我也可能面临方法不起作用以及指针实际上返回正确的问题。

WFMO should work as well. Maybe: win32event.WaitForMultipleObjects([ctypes.cast(my_struct.contents.Event1, ctypes.c_void_p).value], 0, win32event.INFINITE)? That's the most generic form, but in this case (only having an object) WFSO would be better.

WFMO也应该发挥作用。或许:win32event.WaitForMultipleObjects([ctypes.cast(my_struct.contents.Event1,ctyes.c_VOID_p).Value],0,Win32Event.INFINITE)?这是最通用的形式,但在这种情况下(只有一个对象)WFSO会更好。

I do expect to add more handler at some point so if an event is triggered to for example kill the thread it can bypass the method. HUGE thank you for your help!

我确实希望在某个时刻添加更多的处理程序,因此,如果事件被触发,例如终止线程,它可以绕过该方法。非常感谢您的帮助!

优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com