473,789 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::GetFun ctionPointerFor Delegate(dele);
}
static void Uninitialize()
{
gch.Free();
}
static int AsyncCrawl(Stri ng^ url)
{
return CCrawler::Async Crawl(
(CRAWLCALLBACK) ptrDele.ToPoint er());
}
private:
static void OnCrawlEnd()
{
printf("%d\n",: :GetCurrentThre adId()); // OK
printf("%d\n",T hread::CurrentT hread->ManagedThreadI d);//ERROR
};
delegate static void DeleOnCrawlEnd( );
static DeleOnCrawlEnd^ dele;
static GCHandle gch;
static IntPtr ptrDele;
};

CCrawler is a class exported from a traditional DLL, CCrawler::Async Crawl()
accepts a function pointer as callback. After you call
CCrawler::Async Crawl(), 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.StackOv erflowException ".

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

-Joe
Dec 5 '06 #1
3 1733
////////////// CrawlerWrapper. h
public ref class CrawlerWrapper abstract sealed
{
public:
static void Initialize()
{
dele = gcnew DeleOnCrawlEnd( OnCrawlEnd);
gch = GCHandle::Alloc (dele);
ptrDele = Marshal::GetFun ctionPointerFor Delegate(dele);
}
static void Uninitialize()
{
gch.Free();
}
static int AsyncCrawl(Stri ng^ url)
{
return CCrawler::Async Crawl(
(CRAWLCALLBACK) ptrDele.ToPoint er());
}
private:
static void OnCrawlEnd()
{
printf("%d\n",: :GetCurrentThre adId()); // OK
printf("%d\n",T hread::CurrentT hread->ManagedThreadI d);//ERROR
};
delegate static void DeleOnCrawlEnd( );
static DeleOnCrawlEnd^ dele;
static GCHandle gch;
static IntPtr ptrDele;
};

CCrawler is a class exported from a traditional DLL,
CCrawler::Async Crawl()
accepts a function pointer as callback. After you call
CCrawler::Async Crawl(), 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.StackOv erflowException ".

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, GetFunctionPoin terForDelegate 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************* *********@hotma il.com
Remove only "_nos_pam"
Dec 6 '06 #2


"CAIBird" <CA*****@discus sions.microsoft .comwrote in message
news:A4******** *************** ***********@mic rosoft.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",: :GetCurrentThre adId()); // OK
printf("%d\n",T hread::CurrentT hread->ManagedThreadI d);//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::GetFun ctionPointerFor Delegate(dele);
}
static void Uninitialize()
{
gch.Free();
}
static int AsyncCrawl(Stri ng^ url)
{
return CCrawler::Async Crawl(
(CRAWLCALLBACK) ptrDele.ToPoint er());
}
private:
static void OnCrawlEnd()
{
printf("%d\n",: :GetCurrentThre adId()); // OK
printf("%d\n",T hread::CurrentT hread->ManagedThreadI d);//ERROR
};
delegate static void DeleOnCrawlEnd( );
static DeleOnCrawlEnd^ dele;
static GCHandle gch;
static IntPtr ptrDele;
};
Could you post the code for
CCrawler::Async Crawl?
Or don't you have that?

--

Kind regards,
Bruno van Dooren
br************* *********@hotma il.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*****@discus sions.microsoft .comwrote in message
news:A4******** *************** ***********@mic rosoft.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",: :GetCurrentThre adId()); // OK
printf("%d\n",T hread::CurrentT hread->ManagedThreadI d);//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::GetFun ctionPointerFor Delegate(dele);
}
static void Uninitialize()
{
gch.Free();
}
static int AsyncCrawl(Stri ng^ url)
{
return CCrawler::Async Crawl(
(CRAWLCALLBACK) ptrDele.ToPoint er());
}
private:
static void OnCrawlEnd()
{
printf("%d\n",: :GetCurrentThre adId()); // OK
printf("%d\n",T hread::CurrentT hread->ManagedThreadI d);//ERROR
};
delegate static void DeleOnCrawlEnd( );
static DeleOnCrawlEnd^ dele;
static GCHandle gch;
static IntPtr ptrDele;
};

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

--

Kind regards,
Bruno van Dooren
br************* *********@hotma il.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
2337
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 correct, and if so, does anyone have an example ? Thanks !!
1
273
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 context, const LadybugImage* pimage, unsigned char* arpszDestBuffers, LadybugImageInfo* pImageInfo );
3
2782
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 question.
2
2981
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 are stored in an unmanaged struct and this struct is then overhanded by reference to the unmanaged code (See code pieces below). I thought the way to go is to create a delegate, pin it via GCHandel::Alloc and use the function
3
4712
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 application. Furthermore, I use a pointer to an unmanaged function to jump back into the managed dll. The managed part is basically a remoting enhancement which asynchronly
0
9502
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10393
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10187
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7523
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5412
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5544
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4084
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3687
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2902
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.