473,386 Members | 1,644 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,386 software developers and data experts.

Pinning a managed object that is being used for a unmanged callback

Hello,

Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project
where I have a managed class reference in a unmanaged class...simple
enough. To keep things short I am for the most part attempting to do
what is this article by Nish:
http://www.voidnish.com/articles/Sho...px?code=cbwijw

I have to hook up a unmanaged callback to a managed method using IJW
NOT P\Invoke. So I am employing this "Thunk" or "Bridge" class as I
think it is called.

Based on what I have read so far (aka very limited understanding)
anytime one is accessing a managed object in a managed class one would
"pin it". However in the code snippet:

....snip

// Unmanged code
static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM)
{
// We need to get the managed callback
// up for each instance that our callback
// gets called. So we get a pointer to
// the current instance of the outer class
// and invoke the delegate that is holding
// the managed callback method that the
// callee code has passed to us
CEnumWindows* pew = CEnumWindows::GetClass();
//CEnumWindows is a managed C++ class
pew->m_EnumProc->Invoke(hwnd, NULL);
return TRUE;
}

....snip
I see a reference to get a pointer to the managed object->
CEnumWindows* pew = CEnumWindows::GetClass()" but I dont see a __pin *
or gcroot<CEnumWindows*>pew. Before its being Accessed? Is this because
the the pointer that GetClass() returns is static?

This code is getting called by my unmanaged C++ class at least 10-20
times a second so I wanted to make sure that:

1) Its safe to do this (code above: pew->m_EnumProc->Invoke(hwnd,
NULL);) without pinning the pointer to the managed class?

2) if I did change the code to:

// Unmanged code
static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM)
{
// We need to get the managed callback
// up for each instance that our callback
// gets called. So we get a pointer to
// the current instance of the outer class
// and invoke the delegate that is holding
// the managed callback method that the
// callee code has passed to us
gcroot<CEnumWindows*>pew = CEnumWindows::GetClass();
pew->m_EnumProc->Invoke(hwnd, NULL);
return TRUE;
}

Is this correct or completely overkill?

3) Since this callback is being called quite intensively by my
application, If pinning is actually required in this case or if its
safer to pin but not required kind of thing. Is it better performance
wise to do something like pinning (or gcrooting it) the entire object
in the unmanaged clas like so:

namespace UnmanagedLib
{
class UClass
{

private:
gcroot<ManagedClass *> pew;

.... snip

static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM)
{
pew->m_EnumProc->Invoke(hwnd, NULL);
return TRUE;
}
public:
UClass(ManagedClass *pMClass) : pManagedClass(pMClass)
{
pew = pMClass
}

~UClass()
{

}

....snip
};

Is this just bad practive to pin a entire managed object for the life
of my application process and also on a complete newbie side bar If I
dont have something in the destructor do I have a gcroot leak?

Nov 23 '05 #1
5 3395
Update post sorry:

This:

Based on what I have read so far (aka very limited understanding)
anytime one is accessing a managed object in a managed class one would
"pin it".

Should read

Based on what I have read so far (aka very limited understanding)
anytime one is accessing a managed object in a _unmanaged_ class one
would
"pin it".

Nov 23 '05 #2
Hello Maxwell

Oh btw, I am the "Nish" who authored that article :-)

The reason you don't need to pin that pointer is that pew is a CEnumWindows*
(an __gc*) - so if the object pointed to by m_pclass is moved around the CLI
heap, it doesn't matter because the __gc* will be automatically updated
(it's not a native pointer - it's a tracking pointer that actually tracks
the object it points to).

Hope this makes sense.

--
Regards,
Nish [VC++ MVP]
"Maxwell" <rb*@dslextreme.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hello,

Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project
where I have a managed class reference in a unmanaged class...simple
enough. To keep things short I am for the most part attempting to do
what is this article by Nish:
http://www.voidnish.com/articles/Sho...px?code=cbwijw

I have to hook up a unmanaged callback to a managed method using IJW
NOT P\Invoke. So I am employing this "Thunk" or "Bridge" class as I
think it is called.

Based on what I have read so far (aka very limited understanding)
anytime one is accessing a managed object in a managed class one would
"pin it". However in the code snippet:

...snip

// Unmanged code
static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM)
{
// We need to get the managed callback
// up for each instance that our callback
// gets called. So we get a pointer to
// the current instance of the outer class
// and invoke the delegate that is holding
// the managed callback method that the
// callee code has passed to us
CEnumWindows* pew = CEnumWindows::GetClass();
//CEnumWindows is a managed C++ class
pew->m_EnumProc->Invoke(hwnd, NULL);
return TRUE;
}

...snip
I see a reference to get a pointer to the managed object->
CEnumWindows* pew = CEnumWindows::GetClass()" but I dont see a __pin *
or gcroot<CEnumWindows*>pew. Before its being Accessed? Is this because
the the pointer that GetClass() returns is static?

This code is getting called by my unmanaged C++ class at least 10-20
times a second so I wanted to make sure that:

1) Its safe to do this (code above: pew->m_EnumProc->Invoke(hwnd,
NULL);) without pinning the pointer to the managed class?

2) if I did change the code to:

// Unmanged code
static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM)
{
// We need to get the managed callback
// up for each instance that our callback
// gets called. So we get a pointer to
// the current instance of the outer class
// and invoke the delegate that is holding
// the managed callback method that the
// callee code has passed to us
gcroot<CEnumWindows*>pew = CEnumWindows::GetClass();
pew->m_EnumProc->Invoke(hwnd, NULL);
return TRUE;
}

Is this correct or completely overkill?

3) Since this callback is being called quite intensively by my
application, If pinning is actually required in this case or if its
safer to pin but not required kind of thing. Is it better performance
wise to do something like pinning (or gcrooting it) the entire object
in the unmanaged clas like so:

namespace UnmanagedLib
{
class UClass
{

private:
gcroot<ManagedClass *> pew;

... snip

static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM)
{
pew->m_EnumProc->Invoke(hwnd, NULL);
return TRUE;
}
public:
UClass(ManagedClass *pMClass) : pManagedClass(pMClass)
{
pew = pMClass
}

~UClass()
{

}

...snip
};

Is this just bad practive to pin a entire managed object for the life
of my application process and also on a complete newbie side bar If I
dont have something in the destructor do I have a gcroot leak?

Nov 25 '05 #3
Wow, how cool is that...:) Your article helped me out so much, thanks
BTW :)

Well that explains lot I think I get it, I think I missed the fact that
it was a managed class in the sample. Just one note then on this, if
it is a managed pointer in unmanged code how come you didn't have to
use gcroot<CEnumWindows *>?

Thanks Again
Max

Nov 26 '05 #4
Hello Max,

You can instantiate and use __gc pointers from functions of __nogc classes.
You only need to use gcroot when you want to have an __gc member in an
__nogc class.

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com
http://blog.voidnish.com
"Maxwell" <rb********@gmail.com> wrote in message
news:11********************@g47g2000cwa.googlegrou ps.com...
Wow, how cool is that...:) Your article helped me out so much, thanks
BTW :)

Well that explains lot I think I get it, I think I missed the fact that
it was a managed class in the sample. Just one note then on this, if
it is a managed pointer in unmanged code how come you didn't have to
use gcroot<CEnumWindows *>?

Thanks Again
Max

Nov 27 '05 #5
Ah, now I see. Once Again Thanks much for the help...and the article :)!

Nov 28 '05 #6

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

Similar topics

6
by: Shai Levi | last post by:
Hi, I'm trying to migrate native c++ class to managed c++ class. The native class header definition looks as: class NativeClass { public: typedef void (CbFunc1)(int n,void* p);
4
by: Sai Kit Tong | last post by:
I have to interface managed application with my legacy dll. I have employed the wrapper approach but I have to deal with the asynchronous callback from the legacy dll, which likely goes through a...
7
by: Ioannis Vranos | last post by:
Consider the code: wchar_t __pin *p= &(someCommand->ToCharArray()); _wsystem(p); p=0;
9
by: Brian Victor | last post by:
I have a situation where I have a vector<gcroot<ManagedWrapperClass*> > whose contents I need to pass to an unmanaged function. Is there a way to pin all the pointers in the vector for that...
3
by: Hexar Anderson | last post by:
I have two questions: a) From documentation located at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/html/vcManagedExtensionsSpec_7_7.asp, it says, "Pinning a...
12
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley...
5
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have a class that is writen in unmanaged pure native C++. This class files (h and cpp) are inserted to a managed C++ (VC++ 2005, C++/CLI) DLL compoenet. This DLL compoenet is used in a C#...
9
by: =?Utf-8?B?RWR3YXJkUw==?= | last post by:
I would greatly appreciate some help on passing managed object into unmanaged code. I need to pass a reference (address of) of a managed class into unmanaged code (written by a thrid party). The...
4
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post by:
I am very sorry to bring a topic up that has been beaten with an ugly stick..... but... If I want to "fix" an byte array for the life time of a program would it be better allocating it on the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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...
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,...

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.