Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / August 2006

Tip: Looking for answers? Try searching our database.

Old style API function callback conversion to managed C++

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
harishashim@gmail.com - 21 Aug 2006 10:16 GMT
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!
harishashim@gmail.com - 21 Aug 2006 11:04 GMT
> 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.
[quoted text clipped - 14 lines]
>
> 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
Bruno van Dooren [MVP VC++] - 21 Aug 2006 11:37 GMT
> > 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.
[quoted text clipped - 20 lines]
> 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.

Signature

Kind regards,
   Bruno.
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

harishashim@gmail.com - 22 Aug 2006 02:28 GMT
Bruno 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.
[quoted text clipped - 32 lines]
>     bruno_nos_pam_van_dooren@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!
harishashim@gmail.com - 22 Aug 2006 08:03 GMT
> Bruno wrote:
> > > > I am wrapping a digital camera API using Managed C++ VS .NET 2003). I
[quoted text clipped - 50 lines]
>
> 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!
Bruno van Dooren [MVP VC++] - 22 Aug 2006 16:53 GMT
>> I try this and somehow it doesnt give me compile error
>>
[quoted text clipped - 24 lines]
>
> 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.

Signature

Kind regards,
   Bruno van Dooren
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Vladimir Nesterovsky - 22 Aug 2006 09:30 GMT
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.
[quoted text clipped - 12 lines]
> 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.
Signature

Vladimir Nesterovsky

harishashim@gmail.com - 30 Aug 2006 05:01 GMT
> Hello,
>
[quoted text clipped - 17 lines]
> 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!

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.