Code:
// wrapper around GetProcAddress
let dm = getProcAddress(moduleName: "USER32", procName: "DestroyMenu")
print("!!! getProcAddress: \(dm)")
withUnsafePointer(to: DestroyMenu) { ptr in
print("!!! withUnsafePointer(to:): \(ptr)")
ptr.withMemoryRebound(to: UnsafeRawPointer.self, capacity: 2) { ptr2 in
let bp = UnsafeBufferPointer(start: ptr2, count: 2)
print("!!! bp[0]: \(bp[0]) bp[1]: \(bp[1])")
}
}
Output:
!!! getProcAddress: Optional(0x00007fffbaac3d50)
!!! withUnsafePointer(to:): 0x000000b8f52ff4c8
!!! bp[0]: 0x00007ff65bce73e0 bp[1]: 0x00000230e02a0320
Is it possible to get DestroyMenu address without GetProcAddress?
What is DestroyMenu really is? It doesn't look like pointer to C function. Next code doesn't combile because of __stdcall convention I guess:
typealias DestroyMenuFunction = (@convention(c) (HMENU?) -> Bool)
let dmm: DestroyMenuFunction = DestroyMenu
compnerd
(Saleem Abdulrasool)
2
It is just a regular function, from WinUser.h:
WINUSERAPI
BOOL
WINAPI
DestroyMenu(
_In_ HMENU hMenu);
Why are you trying to get the address of the function? In general, that should be fine, but be careful that you are disabling support for non-X64 environments implicitly. I guess that it should work on ARM64 at least since that does treat WINAPI as __cdecl.
As to how to do this, does this not work?
import WinSDK
typealias pDestroyMenu = (HMENU?) -> Bool
var pfnDestroyMenu: pDestroyMenu = DestroyMenu
withUnsafePointer(to: pfnDestroyMenu) { print($0) }