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

Old style API function callback conversion to managed C++

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!

Aug 21 '06 #1
7 2601

ha*********@gmail.com wrote:
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

Aug 21 '06 #2
ha*********@gmail.com wrote:
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?
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.
br**********************@hotmail.com
Remove only "_nos_pam"

Aug 21 '06 #3
Bruno wrote:
ha*********@gmail.com wrote:
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?

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.
br**********************@hotmail.com
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!

Aug 22 '06 #4
ha*********@gmail.com wrote:
Bruno wrote:
ha*********@gmail.com wrote:
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?
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.
br**********************@hotmail.com
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!
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!

Aug 22 '06 #5
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.
--
Vladimir Nesterovsky
Aug 22 '06 #6
>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.

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
br**********************@hotmail.com
Remove only "_nos_pam"
Aug 22 '06 #7
Vladimir Nesterovsky wrote:
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?
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.
--
Vladimir Nesterovsky
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!

Aug 30 '06 #8

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

Similar topics

1
by: pawel | last post by:
Hi! I have a .dll library, with some exported function, which looks something like this: extern "C" { int exported_f (int (__cdecl *callback) (int a, int b)) ; } now, I want to invoke this...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
5
by: Maxwell | last post by:
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...
0
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
12
by: Haxan | last post by:
Hi, I have my main application class(unmanaged) that has a none static member function that I need to pass as a delegate to managed C# method. In one of the methods of this class(unmamanged),...
5
by: Filippo Bettinaglio | last post by:
I have a DLL written in C++ (unmanaged), which I want to inteface with. I created a standard window form program with VC++ 2005, all fine I have imported an .h file the .lib file and all works. ...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
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?
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
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,...
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
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...
0
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,...
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.