I checked how this is handled in MFC, and it looks like the UI thread is defined from the 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 the MFC call AfxGetApp()->m_nThreadID , you can determine the thread identifier of the user interface.
However - this approach does not work if the .dll was not loaded from the main thread - then even the MFC approach will not work - AfxGetApp()->m_nThreadID will return something else than the main thread.
But usually your .dll is loaded from the main thread, but your .dll is not needed by mfc. I could recommend this approach:
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; }
If the .dll is loaded by the main user interface thread, you will get the correct thread identifier from the constructor call (GetMainThread class).
Remove AfxGetApp() calls if you don't need them (in my application I needed)
source share