I've checked how this is handled in MFC, and it looks like UI thread is determined from constructor:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\atlmfc\src\mfc\appcore.cpp:
CWinApp::CWinApp(LPCTSTR lpszAppName)
{
...
m_nThreadID = ::GetCurrentThreadId();
And using MFC call AfxGetApp()->m_nThreadID
you can figure out UI thread ID.
However - this approach does not work if .dll was loaded not from main thread - then even MFC's approach will not work - AfxGetApp()->m_nThreadID
will return something else than main thread.
But typically your .dll gets loaded from main thread, but your .dll is not necessary mfc enabled. I've could recommend approach like this:
class GetMainThread
{
public:
GetMainThread()
{
m_nThreadID = ::GetCurrentThreadId();
}
DWORD m_nThreadID;
}getMainThread;
DWORD getUIThread()
{
DWORD id = 0;
if( AfxGetApp() != NULL )
id = AfxGetApp()->m_nThreadID;
else
id = getMainThread.m_nThreadID;
return id;
} //getUIThread
If .dll is loaded by main UI thread, you will get correct thread id from constructor call (GetMainThread class).
Remove AfxGetApp()
calls if you don't need them (In my application I needed those)