473,379 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

GetCurrentThread returns null

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

Nov 17 '05 #1
2 2189
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;
}

--
Greetings
Jochen
Nov 17 '05 #2
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

"Jochen Kalmbach" <no********************@holzma.de> wrote in message
news:Xn*********************************@127.0.0.1 ...
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;
}

--
Greetings
Jochen

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rene van Hoek | last post by:
Hi, I am using Xalan 1.8.0 and Xerces 2.6.0 in C++. I have an XML document which I first transform into an other XML document using an XSL styelsheet. Then I want to parse with XPathEvaluator...
4
by: ezra epstein | last post by:
Aother head banger for me. Below is a complete example of the code Using Postgres 7.4, the function "test" gets this: psql:temp3.sql:10: ERROR: syntax error at or near "%" at character 135...
2
by: Jeff Adams | last post by:
I am using MSVC .NET to create a C program. I am having trouble creating a window. The createwindow returns NULL however no error is caught. The GetLastError() returns "operation completed...
2
by: Claes Bergefall | last post by:
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)...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
4
by: Henning M | last post by:
Hej All Im relativ new to VB.net and im trying to collect som device information using cfgmgr32.dll I use - Declare Function GetListLength Lib "cfgmgr32.dll" Alias...
12
by: robertino | last post by:
Hi all, I've put together a few SPs to produce a BOM (bill of materials) listing, which together use a couple of global temp tables, and return the results from a cursor. Here's the code: ...
4
by: otengo | last post by:
Hi all, I tried using GetCurrentThread to get the Handle of the current thread but it keeps returning 0xfffffffe for all the threads that I access it in. This is not the correct handle. Am I...
1
by: asilter | last post by:
Hi, I'm trying to assign variables where vpSec0 points. void * vpSec0 = NULL; CreateHVFESection0(vpSec0); CreateHVFESection0 function is below.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.