Untested... I think will work as is. GetWindowThreadProcessId was the function you were looking for.
http://msdn.microsoft.com/en-us/library/ms633522.aspx
code:
var D = Interop.Allocate(4);
var hWnd = Interop.Call('User32.dll', 'GetForegroundWindow');
var hThread = Interop.Call('User32.dll', 'GetWindowThreadProcessId', hWnd, D.DataPtr);
var ProcessId = D.ReadDWORD(0);
D.Size = 0;