How to use GetCurrentThreadId() and AttachThreadInput() functions of coredll.dll

Currently, I am trying to develop a custom keyboard. I have used SendInput() for KeyPress, KeyDown, & Keyup methods which are working fine in a device console application. But when i am using the same KeyPress method in WinCE Forms , it’s not working , may be the issue is focus on the keyboard when I click on the mouse to SendInput (to make sure that the textbox capture that input). I have written a SwitchWindow() method to capture the focus on my form which used GetCurrentThreadId() and AttachThreadInput() method within SwitchWindow(). But, I am getting error while calling GetCurrentThreadId() and AttachThreadInput() method. Below is the error message which i am getting:

  1. Can’t find an Entry Point ‘GetCurrentThreadId’ in a PInvoke DLL ‘coredll.dll’.
  2. Can’t find an Entry Point ‘AttachThreadInput’ in a PInvoke DLL ‘coredll.dll’.

On GetCurrentThreadId (Compact 2013) | Microsoft Docs I have found that GetCurrentThreadId() methods belong to coredll.dll which i have used like below:

[DllImport(“coredll.dll”, SetLastError = true)]
public static extern uint GetCurrentThreadId();

[DllImport(“user32.dll”)]
public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

public static void SwitchWindow(IntPtr windowHandle)
{
if (GetForegroundWindow() == windowHandle)
return;

        IntPtr foregroundWindowHandle = GetForegroundWindow();
        uint currentThreadId = GetCurrentThreadId();

        uint temp;
        uint foregroundThreadId = GetWindowThreadProcessId(foregroundWindowHandle, out temp);
        AttachThreadInput(currentThreadId, foregroundThreadId, true);
        SetForegroundWindow(windowHandle);
        AttachThreadInput(currentThreadId, foregroundThreadId, false);

        while (GetForegroundWindow() != windowHandle)
        {
        }
    }

can anybody suggest me how can i use those 2 methods in order to pass my
SwitchWindow(IntPtr windowHandle) method to get the focus on my form for taking keyboard inputs.

OR Suggest me how can i get the focus before invoking KeyPress() so that the input comes in textbox of my Application’s form control.

Hello @Gufran_Ali

GetCurrentThreadId is an inline function. So you can not directly use this. May be this forum posts can help.

AttachThreadInput This function is not part of the coredll?

The question is, if there would not be other ways to get your issue solved. Did you ever checked to write a software keyboard events (keybd_eventEx)