Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / December 2006

Tip: Looking for answers? Try searching our database.

How to invoke a managed function from a unmanaged thread?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
CAIBird - 05 Dec 2006 02:59 GMT
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"


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.