473,406 Members | 2,954 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,406 software developers and data experts.

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

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
{... }

};
Jan 11 '06 #1
3 3642
Lonewolf wrote:
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
Jan 11 '06 #2
> 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?
Jan 12 '06 #3
Lonewolf wrote:
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
Jan 12 '06 #4

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

Similar topics

2
by: Jacob Cohen | last post by:
Under VC7.1, I am trying to wrap a native-C++ DLL that contains C++ objects in a Managed-C++ class library for use in a C# project. I created and compiled the native DLL under VC7.1 as a Win32...
5
by: Lonewolf | last post by:
Hi, I'm not sure if this has been asked before so please pardon me if this is a repeated question. Basically I have some performance critical directshow codes which is implemented in native,...
0
by: MC-Advantica | last post by:
I have built a managed wrapper class and assembly that expose much functionality available in unmanaged legacy DLLs. I can write VB apps that interface with the managed assembly, and the managed...
1
by: =?Utf-8?B?SmltIFdhbHNo?= | last post by:
I have an VC++ MFC Win32 application that isn't working correctly when I build it with VS2005. The problem seems to be in some ADO ActiveX controls that came with VS6 and are now out of support....
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
20
by: Aek | last post by:
We recently moved our large codebase over from VS7 to 8 and found that we now get access violations in atexit calls at shutdown when debugging the application in VS2005. This occurs in static...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
0
by: eitanyan | last post by:
I am trying to create a Java to .Net interop and the way I am doing it is by creating a C# com object and a native unmanaged c++ dll that uses JNIEnv of java. the java is loading the native c++ dll...
2
by: Bob Altman | last post by:
Hi all, We have a native class modeled after the System::Exception class, and all exceptions that we throw derive from this class. For now this class is quite simple: just Description and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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
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...

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.