473,732 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=(CNativ eClass*)lpv;
DWORD dwValue=0;
while (bContinue)
{
pParent->UpdateValue(dw Value++);
::Sleep(1000);
}
}
void UpdateValue(DWO RD dw)
{
m_pfn(dw);
}
void Setfn(LPFNUPDAT EVALUE pfn)
{
m_pfn=pfn;
}
};

the delegate is declared as
public delegate void UpdatevalueDele gate(DWORD);
and the function ptr is declared as
typedef void (*LPFNUPDATEVAL UE)(DWORD);

public ref class CManagedClass
{
UpdatevalueDele gate^ 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::GetFun ctionPointerFor Delegate(m_Upda te);
pNative->Setfn(static_c ast<LPFNUPDATEV ALUE>(ip.ToPoin ter()));
}.
void UpdateDel(DWORD dw) <==this is the managed function called from
the native thread via delegate
{... }

};
Jan 11 '06 #1
3 3670
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.BeginIn voke 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.BeginIn voke 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=(CNativ eClass*)lpv;
DWORD dwValue=0;
while (bContinue)
{
pParent->UpdateValue(dw Value++);
::Sleep(1000);
}
}
void UpdateValue(DWO RD dw)
{
m_pfn(dw);
}
void Setfn(LPFNUPDAT EVALUE pfn)
{
m_pfn=pfn;
}
};

the delegate is declared as
public delegate void UpdatevalueDele gate(DWORD);
and the function ptr is declared as
typedef void (*LPFNUPDATEVAL UE)(DWORD);

public ref class CManagedClass
{
UpdatevalueDele gate^ 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::GetFun ctionPointerFor Delegate(m_Upda te);
pNative->Setfn(static_c ast<LPFNUPDATEV ALUE>(ip.ToPoin ter()));

}.
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 "politicall y" 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
7576
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 DLL C++ (unmanaged) project. I found the decorated names of the symbols I wanted to export and created a .def file, so that it generated an import library. I took this import library and had my Managed-C++ class library link against it. The...
5
11360
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, unmanaged C++. I have written a managed wrapper for it using C++/CLI. problem comes when I try to pass a C# string over to the DLL via C++/CLO. Basically I have a textbox in the C# GUI, and I declared my C++/CLI property as follows. C++/CLI property...
0
1295
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 assembly makes calls to the legacy dlls. All seems to work fine when the assembly is a private assembly. When I go through the process of strong naming the assembly and placing it into the GAC, none of my VB apps will initialize. I have gone to...
1
2696
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. So, I have decided to solve the problem by using a mixed assembly: Most of the program will be my MFC - based code (unmanaged), but I will implement one dialog box as managed code. In my original MFC app, this dialog box contains the problem ActiveX...
17
7253
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 to show the array data to the end user. Can I do that? How?
20
6015
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 members / singletons (especially meyer type singletons) which use locally declared static variables. These variables are normally cleaned up automatically at shutdown of the application by registering with the atexit. I break point the destructor on...
14
3788
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 more why C++/CLI would be better to PInvoke than doing the PInvoke in C#? Because, usually in C# as you already know we use DLLImport and extern
0
2898
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 and invokes a method in it which invokes a method on The command passed from java to .Net is a byte array and the response passed back is also a byte array the C# managed COM object which finally invokes a method on a regular C# managed dll....
2
2965
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 InnerException public members. One of these days I'll dig into how to implement a StackTrace property (I assume that this is possible using something like dbghelp.dll, but I've never had the time to look into it). I've recently written a managed...
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
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
9447
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
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
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
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3261
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
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.