How to invoke a managed function from a unmanaged thread?

G

Guest

hi,

i'v been coding with c++/cli and i hit a terrible problem.

i created a class library project and add a ref class(abstract) with some
static member functions like:

////////////// CrawlerWrapper.h
public ref class CrawlerWrapper abstract sealed
{
public:
static void Initialize()
{
dele = gcnew DeleOnCrawlEnd(OnCrawlEnd);
gch = GCHandle::Alloc(dele);
ptrDele = Marshal::GetFunctionPointerForDelegate(dele);
}
static void Uninitialize()
{
gch.Free();
}
static int AsyncCrawl(String^ url)
{
return CCrawler::AsyncCrawl(
(CRAWLCALLBACK)ptrDele.ToPointer());
}
private:
static void OnCrawlEnd()
{
printf("%d\n",::GetCurrentThreadId()); // OK
printf("%d\n",Thread::CurrentThread->ManagedThreadId);//ERROR
};
delegate static void DeleOnCrawlEnd();
static DeleOnCrawlEnd^ dele;
static GCHandle gch;
static IntPtr ptrDele;
};

CCrawler is a class exported from a traditional DLL, CCrawler::AsyncCrawl()
accepts a function pointer as callback. After you call
CCrawler::AsyncCrawl(), it will create a thread to perform a task and then
when the thread has done, it will call the callback function to inform you
some information.

My problem is that my callback function CrawlerWrapper::OnCrawlEnd() crashes
when excutes the second line with a exception of
"System.StackOverflowException".

I can't figure out the root cause. Could anyone help me?
Many many thanks.

-Joe
 
B

Bruno van Dooren [MVP VC++]

////////////// CrawlerWrapper.h
public ref class CrawlerWrapper abstract sealed
{
public:
static void Initialize()
{
dele = gcnew DeleOnCrawlEnd(OnCrawlEnd);
gch = GCHandle::Alloc(dele);
ptrDele = Marshal::GetFunctionPointerForDelegate(dele);
}
static void Uninitialize()
{
gch.Free();
}
static int AsyncCrawl(String^ url)
{
return CCrawler::AsyncCrawl(
(CRAWLCALLBACK)ptrDele.ToPointer());
}
private:
static void OnCrawlEnd()
{
printf("%d\n",::GetCurrentThreadId()); // OK
printf("%d\n",Thread::CurrentThread->ManagedThreadId);//ERROR
};
delegate static void DeleOnCrawlEnd();
static DeleOnCrawlEnd^ dele;
static GCHandle gch;
static IntPtr ptrDele;
};

CCrawler is a class exported from a traditional DLL,
CCrawler::AsyncCrawl()
accepts a function pointer as callback. After you call
CCrawler::AsyncCrawl(), it will create a thread to perform a task and then
when the thread has done, it will call the callback function to inform you
some information.

My problem is that my callback function CrawlerWrapper::OnCrawlEnd()
crashes
when excutes the second line with a exception of
"System.StackOverflowException".

I can't figure out the root cause. Could anyone help me?
Many many thanks.

Hi,

You should show us the definition of the delegate, and the declaration of
the callback function in native code.
By default, GetFunctionPointerForDelegate will give you a stdcall function
pointer.
If your native declaration is cdecl, bad things will happen.

another possibility is that one or more of the parameters is marshalled
incorrectly, which is why I need to see both the native declaration and the
delegate definition before I can hazard a guess.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
B

Bruno van Dooren [MVP VC++]

CAIBird said:
Thanks,

The native callback is defined as:
typedef void (__stdcall *CRAWLCALLBACK)(void);

The delegate is declared as:
delegate static void DeleOnCrawlEnd();

and defined as:
static void OnCrawlEnd()
{
printf("%d\n",::GetCurrentThreadId()); // OK
printf("%d\n",Thread::CurrentThread->ManagedThreadId);//ERROR
};

Could you post the code for
CCrawler::AsyncCrawl?
Or don't you have that?

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
G

Guest

Yes, i don't have the source code. but i know it is unmanaged code and it
creates unmanaged thread to call the callback.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top