ref: ISystemMediaTransportControlsInterop
Compilei uma dll sobre ISystemMediaTransportControlsInterop::GetForWindow. Usei o IDA para descompilá-lo. Então escrevi o código tipo C como Python. Acredito que os escrevi da maneira correta. Mas, infelizmente, encontrei um erro de ponteiro. Sei que é porque o endereço da função SMTC_Interop_GetForWindow está incorreto, mas não sei por quê.
Registro do Python:
RoInit: 0
String Create: 0, SMTCInterop: c_void_p(None)
RoGetActivationFactory: 0, SMTCInterop: c_void_p(7948368)
Traceback (most recent call last):
File "D:\儿子文件\编程\python\PyCharm项目\WinSMTC\test.py", line 56, in <module>
TestFunc()
File "D:\儿子文件\编程\python\PyCharm项目\WinSMTC\test.py", line 45, in TestFunc
GetForWindow(Create_SMTC_Window(), smtc_obj)
File "D:\儿子文件\编程\python\PyCharm项目\WinSMTC\test.py", line 26, in GetForWindow
result = SMTC_Interop_GetForWindow(smtc_interop.value, hwnd, ctypes.byref(REF_IID), ctypes.byref(smtc_obj))
OSError: exception: access violation writing 0x00007FFBED118788
Código Python (tecla func):
def GetForWindow(hwnd: int, smtc_obj: ctypes.c_void_p):
result = RoInitialize(RO_INIT_MULTITHREADED)
print("RoInit:", result)
smtc_interop = VoidPtr()
h_string = HSTRING()
result = WindowsCreateString(String("Windows.Media.SystemMediaTransportControls"), ctypes.c_uint32(42),
ctypes.byref(h_string))
print(f"String Create: {result}, SMTCInterop: {smtc_interop}")
result = RoGetActivationFactory(h_string, ctypes.byref(IID_SystemMediaTransportControls),
ctypes.byref(smtc_interop))
print(f"RoGetActivationFactory: {result}, SMTCInterop: {smtc_interop}")
SMTC_Interop_GetForWindow = ctypes.CFUNCTYPE(ctypes.c_int64, ctypes.c_int64, ctypes.c_int32, ctypes.POINTER(ctypes.c_int64),
ctypes.POINTER(ctypes.c_void_p))(ctypes.cast(smtc_interop.value, ctypes.POINTER(ctypes.c_int64)).contents.value + 48)
result = SMTC_Interop_GetForWindow(smtc_interop.value, hwnd, ctypes.byref(REF_IID), ctypes.byref(smtc_obj))
Código de função IDA (para a tecla func):
Você pode baixar o código-fonte DLL e o arquivo DLL e o arquivo de depuração IDA e testar o código Python aqui
O erro sugere que você está tentando escrever em uma memória que não está acessível. O problema principal está nesta linha:
Mude com
Essa abordagem deve resolver o erro, pois acessa o ponteiro de função por meio da estrutura vtable em vez de usar um deslocamento direto do ponteiro de interface.