Skip to content

Instantly share code, notes, and snippets.

@toptensoftware
Created November 12, 2016 23:30
Show Gist options
  • Select an option

  • Save toptensoftware/79e9e13a1998b28c2c9e14d48fdb2447 to your computer and use it in GitHub Desktop.

Select an option

Save toptensoftware/79e9e13a1998b28c2c9e14d48fdb2447 to your computer and use it in GitHub Desktop.
// Call a 16-bit window procedure
public IntPtr CallWndProc16from32(uint pfnProc, IntPtr hWnd, uint message, IntPtr wParam, IntPtr lParam, bool dlgProc)
{
// Put it into structure
var msg32 = new Win32.MSG()
{
hWnd = hWnd,
message = message,
wParam = wParam,
lParam = lParam,
};
// Get message semantics
ushort message16;
var sem = TryGetMessageSemantics32(hWnd, message, out message16);
// Convert the easy stuff
var msg16 = new Win16.MSG()
{
hWnd = HWND.Map.To16(hWnd),
message = message16,
};
// Bypass?
if (sem!=null && sem.ShouldBypass(_machine, ref msg32))
{
// Bypassed messages to a dialog box procedure can just return FALSE
if (dlgProc)
return IntPtr.Zero;
// Unsupported message - sneak it through
var bpm = AllocBypassMessage(message, wParam, lParam);
var ret = _machine.CallWndProc16(pfnProc, HWND.Map.To16(hWnd), Win16.WM_WIN3MU_BYPASS16, 0, bpm.id);
return FreeBypassMessage(bpm);
}
// Postable?
var postable = sem as MessageSemantics.Postable;
if (postable != null)
{
// Convert it
postable.To16(_machine, ref msg32, ref msg16);
// Call it
var x = _machine.CallWndProc16(pfnProc, msg16.hWnd, msg16.message, msg16.wParam, msg16.lParam);
// Return it
return BitUtils.DWordToIntPtr(x);
}
// Callable?
var callable = sem as MessageSemantics.Callable;
if (callable != null)
{
// Call it
var retv = callable.Call16from32(_machine, false, dlgProc, ref msg32, ref msg16, () =>
{
return _machine.CallWndProc16(pfnProc, msg16.hWnd, msg16.message, msg16.wParam, msg16.lParam);
});
if (dlgProc)
retv = (IntPtr)(x.ToInt32().Loword());
return retv;
}
// Unsupported
throw new VirtualException($"Unknown windows message {MessageNames.NameOfMessage(message)} for window class '{User.GetClassName(hWnd32)}'");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment