473,410 Members | 1,904 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,410 software developers and data experts.

How to invoke a managed function from a unmanaged thread?

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
Dec 5 '06 #1
3 1706
////////////// 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
br**********************@hotmail.com
Remove only "_nos_pam"
Dec 6 '06 #2


"CAIBird" <CA*****@discussions.microsoft.comwrote in message
news:A4**********************************@microsof t.com...
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
};
"Bruno van Dooren [MVP VC++]" wrote:
////////////// 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;
};
Could you post the code for
CCrawler::AsyncCrawl?
Or don't you have that?

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Dec 9 '06 #3
Yes, i don't have the source code. but i know it is unmanaged code and it
creates unmanaged thread to call the callback.

"Bruno van Dooren [MVP VC++]" wrote:
>

"CAIBird" <CA*****@discussions.microsoft.comwrote in message
news:A4**********************************@microsof t.com...
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
};
"Bruno van Dooren [MVP VC++]" wrote:
////////////// 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;
};

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

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Dec 11 '06 #4

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

Similar topics

11
by: Yoni Rabinovitch | last post by:
Is it possible to invoke a C# delegate/event handler asynchronously, from unmanaged C++ ? I assume this requires a Managed C++ wrapper, which would call BeginInvoke on the delegate ? Is this...
1
by: Don | last post by:
I have a third party C++ DLL that I am trying to use from C#. The specific function I am trying to use is declared in C++ as follows: ladybugConvertToMultipleBGRU32( LadybugContext ...
3
by: Thorsten | last post by:
HI I'm a C# developer and unfortunately I have to write now some code in managed and unmanaged C++. In this area I'm Newbie and therefore please forgive me if this is a really simple...
2
by: interX | last post by:
Hi, I have a little problem with managed/unmanaged in Visual Studio 2005 (Compiler setting /clr). I need to overhand several function pointers from managed to unmanaged. These function pointers...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.