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
Bruno van Dooren [MVP VC++] - 06 Dec 2006 12:16 GMT
> ////////////// CrawlerWrapper.h
> public ref class CrawlerWrapper abstract sealed
[quoted text clipped - 41 lines]
> 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.

Signature
Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@hotmail.com
Remove only "_nos_pam"