problems passing data from native thread back to managed class inmixed assembly

L

Lonewolf

Hi all,
I'm having difficulties passing data back to managed class from my
native class when the data is generated from within a native thread in
the native class itself. I will give the following runtime error,

" Attempting to call into managed code without transitioning out first.
Do not attempt to run managed code inside low-level native
extensibility points, such as the vectored exception handler, since
doing so can cause corruption and data loss."

Within this native class is a thread which updates some data, adn I
would like to pass these data back to the managed class via delegate
when it is updated. I created a delegate and function ptr for the
delegate to pass to the native class so that it can call the managed
class via the function ptr when the data gets updated. However, when
calling from within the thread, the above error occurs. How do I transit
out of the said "situation"? What should be the right way of doing this?

Below are some codes, greatly simplified for clarity to illustrate the
situation. Please enlighten me on the right way. The thread must be
present, as the data in actual fact is updated when soem events are set,
and I need to reflect this back to the managed class and store the
updated values within the managed class for easy retrieval from the
managed client using this mixed assembly. In my code, I have similar
delegate adn function ptr for native callback to managed class which
worked so I am puzzled why this can't. my guess is, it's called from the
thread itself and thus not allowed, as as .NET disallow updating of UI
from threads which don't own the UI. But I am lost how to overcome it.

CNativeClass
{
LPFNUPDATEVALUE m_pfn;

static uint ThreadProc(void* lpv)
{
BOOL bContinue=true;
CNativeClass* pParent=(CNativeClass*)lpv;
DWORD dwValue=0;
while (bContinue)
{
pParent->UpdateValue(dwValue++);
::Sleep(1000);
}
}
void UpdateValue(DWORD dw)
{
m_pfn(dw);
}
void Setfn(LPFNUPDATEVALUE pfn)
{
m_pfn=pfn;
}
};

the delegate is declared as
public delegate void UpdatevalueDelegate(DWORD);
and the function ptr is declared as
typedef void (*LPFNUPDATEVALUE)(DWORD);

public ref class CManagedClass
{
UpdatevalueDelegate^ m_Update;
CNativeClass* m_pNative;
GCHandle m_hGC;
public:
void InitNativeClass()
{
m_pNative=new CNativeClass();
m_Update=gcnew (this, &UpdateDel);
m_hGC = GCHandle::Alloc(m_Update);
IntPtr ip = Marshal::GetFunctionPointerForDelegate(m_Update);
pNative->Setfn(static_cast<LPFNUPDATEVALUE>(ip.ToPointer()));
}.
void UpdateDel(DWORD dw) <==this is the managed function called from
the native thread via delegate
{... }

};
 
C

Carl Daniel [VC++ MVP]

Lonewolf said:
Hi all,
I'm having difficulties passing data back to managed class from my
native class when the data is generated from within a native thread in
the native class itself. I will give the following runtime error,

" Attempting to call into managed code without transitioning out
first. Do not attempt to run managed code inside low-level native
extensibility points, such as the vectored exception handler, since
doing so can cause corruption and data loss."

Within this native class is a thread which updates some data, adn I
would like to pass these data back to the managed class via delegate
when it is updated. I created a delegate and function ptr for the
delegate to pass to the native class so that it can call the managed
class via the function ptr when the data gets updated. However, when
calling from within the thread, the above error occurs. How do I
transit out of the said "situation"? What should be the right way of
doing this?

I believe that the problem is that you're calling into the CLR from a thread
that the CLR didn't create, so the necessary managed context doesn't exist
for your thread.

I believe that you have a few choices:

1. Use standard IPC mechanisms to get the messages into the context of a
thread that was created by the CLR.
2. Have your managed code create the thread and call into native code.
3. Through the CLR hosting APIs there might be a way to attach a new thread
to the CLR.
4. Rewrite your low-level code as managed.
Below are some codes, greatly simplified for clarity to illustrate the
situation. Please enlighten me on the right way. The thread must be
present, as the data in actual fact is updated when soem events are
set, and I need to reflect this back to the managed class and store
the updated values within the managed class for easy retrieval from
the managed client using this mixed assembly. In my code, I have
similar delegate adn function ptr for native callback to managed
class which worked so I am puzzled why this can't. my guess is, it's
called from the thread itself and thus not allowed, as as .NET
disallow updating of UI from threads which don't own the UI. But I am
lost how to overcome it.

Once you get your native callbacnk into the CLR, if you in fact need to
update your UI, then you need to use Control.BeginInvoke to transfer the
callback into the UI thread. This is a general Windows restriction, not
just .NET - a window can only be updated by the thread that owns it.
Attemps to access a window from another thread can result in difficult to
diagnose deadlocks and all sorts of wierd behavior.

-cd
 
L

Lonewolf

Once you get your native callbacnk into the CLR, if you in fact need to
update your UI, then you need to use Control.BeginInvoke to transfer the
callback into the UI thread. This is a general Windows restriction, not
just .NET - a window can only be updated by the thread that owns it.
Attemps to access a window from another thread can result in difficult to
diagnose deadlocks and all sorts of wierd behavior.

Hi,
thanx for taking the time to enlighten me. I knew abt the UI update
from another thread via delegate. However, for my case, it could be
difficult to re-write the native code in managed as it deals with
directshow. The events are directshow events, so the managed thread
won't be able to respond to the events. Like i said, the native thread
in the native code must stay, it is used for detecting events from
Directshow, which has no .NET version.

any one has anything to add to this?
 
L

Lonewolf

Lonewolf said:
Hi all,
I'm having difficulties passing data back to managed class from my
native class when the data is generated from within a native thread in
the native class itself. I will give the following runtime error,

" Attempting to call into managed code without transitioning out first.
Do not attempt to run managed code inside low-level native
extensibility points, such as the vectored exception handler, since
doing so can cause corruption and data loss."

Within this native class is a thread which updates some data, adn I
would like to pass these data back to the managed class via delegate
when it is updated. I created a delegate and function ptr for the
delegate to pass to the native class so that it can call the managed
class via the function ptr when the data gets updated. However, when
calling from within the thread, the above error occurs. How do I transit
out of the said "situation"? What should be the right way of doing this?

Below are some codes, greatly simplified for clarity to illustrate the
situation. Please enlighten me on the right way. The thread must be
present, as the data in actual fact is updated when soem events are set,
and I need to reflect this back to the managed class and store the
updated values within the managed class for easy retrieval from the
managed client using this mixed assembly. In my code, I have similar
delegate adn function ptr for native callback to managed class which
worked so I am puzzled why this can't. my guess is, it's called from the
thread itself and thus not allowed, as as .NET disallow updating of UI
from threads which don't own the UI. But I am lost how to overcome it.

CNativeClass
{
LPFNUPDATEVALUE m_pfn;

static uint ThreadProc(void* lpv)
{
BOOL bContinue=true;
CNativeClass* pParent=(CNativeClass*)lpv;
DWORD dwValue=0;
while (bContinue)
{
pParent->UpdateValue(dwValue++);
::Sleep(1000);
}
}
void UpdateValue(DWORD dw)
{
m_pfn(dw);
}
void Setfn(LPFNUPDATEVALUE pfn)
{
m_pfn=pfn;
}
};

the delegate is declared as
public delegate void UpdatevalueDelegate(DWORD);
and the function ptr is declared as
typedef void (*LPFNUPDATEVALUE)(DWORD);

public ref class CManagedClass
{
UpdatevalueDelegate^ m_Update;
CNativeClass* m_pNative;
GCHandle m_hGC;
public:
void InitNativeClass()
{
m_pNative=new CNativeClass();
m_Update=gcnew (this, &UpdateDel);
m_hGC = GCHandle::Alloc(m_Update);
IntPtr ip =
Marshal::GetFunctionPointerForDelegate(m_Update);


pNative->Setfn(static_cast<LPFNUPDATEVALUE>(ip.ToPointer()));

}.
void UpdateDel(DWORD dw) <==this is the managed function called
from the native thread via delegate
{... }

};


problem solved. I copy the data from the thread to the member in the
native class, and pass this member as data to the managed code. this
way, it won't complain, and I'm getting the data correctly with no
corruption. I don't know if this is the correct way of doing, but it
works. I welcome anyone with a "politically" correct way of doing it. :p
 

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