GetCurrentThread returns null

  • Thread starter Thread starter Claes Bergefall
  • Start date Start date
C

Claes Bergefall

I'm trying to call the GetCurrentThread API function
from a managed C++ app, but for some reason it
always returns a null handle?!

And calling GetLastError (or Marshal::GetLastWin32Error) returns
different values depending on where I put the code. For example:

The following is the code that VS generates when selecting
the Windows Forms Application (.NET) project type:

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
Application::Run(new Form1());
return 0;
}

If I call GetCurrentThreadId before setting ApartmentState I get error 2
(ERROR_FILE_NOT_FOUND). Calling it after gives error 183
(ERROR_ALREADY_EXISTS)




Calling GetCurrentThread from an MFC app (no .NET) or P/Invoking
it from a VB.NET app works just fine.

Perhaps this is some kind of security issue?
I'm doing this on my local machine so there
is no network or related stuff involved.
I'm using .NET 1.1

Any ideas?


/claes
 
Hello Claes,

can you please post the complete code !?

The following works just fine:

#include "stdafx.h"
#include "Form1.h"
#include <windows.h>

using namespace cw;

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;

HANDLE h = GetCurrentThread();
DWORD id = GetCurrentThreadId();

Application::Run(new Form1());
return 0;
}
 
Hmm, seems that it is working for me too.
I was fooled by the debugger that shows "{void}",
and later calls using the handle fails (in another project)

Thanks for the help

/claes
 
Back
Top