Old style API function callback conversion to managed C++

H

harishashim

I am wrapping a digital camera API using Managed C++ VS .NET 2003). I
have this function that called as bellow in the API sample.

err = PR_RC_StartViewFinder( m_hCamera, //line 1
(prContext)this, //line 2
(prViewFinderCB*)&ViewFinderCallBackFun ); //line 3

prContext is actually a typedef for unsigned long.
ViewFinderCallBackFun is a callback function. There is two error that I
get when i tried above code unchanged:

Line 2-> error C2440: 'type cast' : cannot convert from
'CameraSDK::Camera __gc *const ' to 'prContext'. Cannot cast a __gc
pointer to an integral type
Line 3 -> error C2276: '&' : illegal operation on bound member function
expression.

Thanks in advance!
 
H

harishashim

I am wrapping a digital camera API using Managed C++ VS .NET 2003). I
have this function that called as bellow in the API sample.

err = PR_RC_StartViewFinder( m_hCamera, //line 1
(prContext)this, //line 2
(prViewFinderCB*)&ViewFinderCallBackFun ); //line 3

prContext is actually a typedef for unsigned long.
ViewFinderCallBackFun is a callback function. There is two error that I
get when i tried above code unchanged:

Line 2-> error C2440: 'type cast' : cannot convert from
'CameraSDK::Camera __gc *const ' to 'prContext'. Cannot cast a __gc
pointer to an integral type
Line 3 -> error C2276: '&' : illegal operation on bound member function
expression.

Thanks in advance!

It seems that line 3 error is because I don't pun static in
ViewFinderCallBackFun declaration.

So now it is Line 2 error that is troubling me. Is it possible to pass
this to unmanaged API function?

TIA
 
G

Guest

It seems that line 3 error is because I don't pun static in
ViewFinderCallBackFun declaration.

So now it is Line 2 error that is troubling me. Is it possible to pass
this to unmanaged API function?

Your code as it is now tries to pass a managed pointer as an unmanaged
pointer.
You could pin the pointer (__pin) to solve that.
Check the documentation for __pin. There are a couple of examples there that
show you how to use it.
 
H

harishashim

Bruno said:
Your code as it is now tries to pass a managed pointer as an unmanaged
pointer.
You could pin the pointer (__pin) to solve that.
Check the documentation for __pin. There are a couple of examples there that
show you how to use it.

--
Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"

Thanks for the pointer :D

I try this and somehow it doesnt give me compile error

Camera __pin *This = this;
prContext pCon = (prContext) This;

CheckAPI(PR_RC_StartViewFinder(
m_hCamera,
pCon,
(prViewFinderCB*)&ViewFinderCallBackFun));


It is not working yet, will do further testing. In the mean time,
appreciate any feedback.

Thanks!
 
H

harishashim

Thanks for the pointer :D

I try this and somehow it doesnt give me compile error

Camera __pin *This = this;
prContext pCon = (prContext) This;

CheckAPI(PR_RC_StartViewFinder(
m_hCamera,
pCon,
(prViewFinderCB*)&ViewFinderCallBackFun));


It is not working yet, will do further testing. In the mean time,
appreciate any feedback.

Thanks!

It doesnt work, PR_RC_StartViewFinder return error.

I think this is because __pin *This scope is short. The scope end right
after PR_RC_StartViewFinder is called. According to the MSDN
documentation

---------------------

A pinned object is pinned only while a pinning pointer points to it. It
is no longer pinned when its pinning pointer goes out of scope, or is
set to 0 in the program. After that, any unmanaged pointers that remain
pointing to that object must not be dereferenced. An unpinned item can
be moved in the heap by the garbage collector. Any __nogc pointers that
still point to it will not be updated, and dereferencing one of them
could raise an unrecoverable exception.
 
V

Vladimir Nesterovsky

Hello,
I am wrapping a digital camera API using Managed C++ VS .NET 2003). I
have this function that called as bellow in the API sample.

err = PR_RC_StartViewFinder( m_hCamera, //line 1
(prContext)this, //line 2
(prViewFinderCB*)&ViewFinderCallBackFun ); //line 3

prContext is actually a typedef for unsigned long.
ViewFinderCallBackFun is a callback function. There is two error that I
get when i tried above code unchanged:

Line 2-> error C2440: 'type cast' : cannot convert from
'CameraSDK::Camera __gc *const ' to 'prContext'. Cannot cast a __gc
pointer to an integral type
Line 3 -> error C2276: '&' : illegal operation on bound member function
expression.

Why do you think it's appropriate to pass managed pointer into the native
function?
Do you have an example for this API?

P.S. There is nothing old in this style of callback API.
 
B

Bruno van Dooren [MVP VC++]

I try this and somehow it doesnt give me compile error
It doesnt work, PR_RC_StartViewFinder return error.

I think this is because __pin *This scope is short. The scope end right
after PR_RC_StartViewFinder is called. According to the MSDN
documentation

A pinned object is pinned only while a pinning pointer points to it. It
is no longer pinned when its pinning pointer goes out of scope, or is
set to 0 in the program. After that, any unmanaged pointers that remain
pointing to that object must not be dereferenced. An unpinned item can
be moved in the heap by the garbage collector. Any __nogc pointers that
still point to it will not be updated, and dereferencing one of them
could raise an unrecoverable exception.

I still need help to get arround this. Any help is appreciated!

Well, Is there a reason why you want to pass a pointer of a managed object
to the SDK?
Your design would be a lot simpler if you would only use unmanaged C++ to
work together with the SDK.
Perhaps a good solution would be to write a very thin unmanaged wrapper
class that abstracts the SDK (C style functions with callback) from your
managed code.

And then you let your managed code interact only with your wrapper class in
a way that does not require it to supply callback functions.

This would have almost 0 performance impact, while greatly simplifying your
design.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
H

harishashim

Vladimir said:
Hello,


Why do you think it's appropriate to pass managed pointer into the native
function?

I was at that time looking for quick fix to my problem and experiment
here and there on what I can do with mix managed and unmanaged class.
It appears that __pin allow me to pass "this" pointer to unmanaged
class. It does work. however please read my conclussion at the end of
this post.
Do you have an example for this API?

The example that I get for this API is purely c++ (not managed
extension sample).
P.S. There is nothing old in this style of callback API.

I am referring to it with delegate callback style in mind. But you are
right that there is nothing old about it since delegate mainly applies
to .NET or managed c++. Well I can be wrong about this too.

At the end of the experiment process I have decided to follow Bruno
van Dooren advise to use unmanaged C++ to interact with the SDK. By
doing this there is no need to pin "this" pointer. I have choosen this
as I have some doubt regarding pinning "this" pointer and passing it to
callback function that will run through the application/dll runtime.

My initial __pin "this" try is successful but I suspect that this is a
time bomb waiting to explode :D.

Thanks all and best regards!
 

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