473,218 Members | 1,696 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,218 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 3627
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.