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 / .NET Framework / Interop / June 2004

Tip: Looking for answers? Try searching our database.

Using a callback interface between managed/unmanaged code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony A. - 25 Jun 2004 00:49 GMT
I have an application that is split into two parts--the user-interface
portion, written in C#, and a C++ DLL that contains both managed and
unmanaged code, where the unmanaged code is core processing that needs
to be as fast as possible.

Now, during the course of one of these long unmanaged operations, I'd
like to be able to periodically call back to the managed UI in order to
show some kind of progress update.  Here is the basic calling sequence
for my application:

1) C# UI code calls a managed function in the C++ DLL
2) The managed C++ stub calls an unmanaged worker function
3) The worker function performs the operation

Step 3 is where I would like to be able to call back into the managed UI
to update the progress.  At first, I started with a simple delegate, and
this worked, but I'd like to have something more powerful.  So, I want
to be able to pass an interface into my unmanaged code instead.  Some
class in the UI would implement this interface, and it would be passed
to the unmanaged code so the worker function can update the progress at
regular intervals.

But I'm not really sure where to start with getting an entire interface
working.  I've tried a couple things, but they only result in crashes.

Can anyone offer any advice?  Thanks,

--
Tony
chaor - 25 Jun 2004 15:50 GMT
If I wrote like this:
......
private AnyCallBack acb;
public void tt(){
acb = new AnyCallBack(this.dd);
call the unmanaged code and pass the acb in.
}
......
It wound't result in crash.

If I wrote like this:

......

public void tt(){
AnyCallBack acb = new AnyCallBack(this.dd);
call the unmanaged code and pass the acb in.
}
......
Sometime it will result in crash,but sometimes not.

I don't know if this can help you.

>I have an application that is split into two parts--the user-interface
>portion, written in C#, and a C++ DLL that contains both managed and
[quoted text clipped - 22 lines]
>
>Can anyone offer any advice?  Thanks,

--------------------
chaor
chaor@chaor.vicp.net
Tony A. - 25 Jun 2004 18:32 GMT
Well, after tinkering with it a little more, I was finally able to get
it working.  It turns out the error was in my unmanaged interface
declaration.  I figure I'll post my solution, in case anyone else has
similar problems, or any comments.

At the very least, it will be catalogued by Google and hopefully will be
of some use to someone in the future.

First off, here's my managed C++ interface declaration (Eclipse users
will probably recognize a similarity):

----
[InterfaceTypeAttribute(ComInterfaceType::InterfaceIsIUnknown),
 GuidAttribute("A340C789-D1E8-4d13-B30D-25B1A2659BD7")]
public __gc __interface IProgressMonitor
{
    void BeginTask(String* name, int totalWork);
    void Done();
    void Worked(int work);
};
----

In order to use this from unmanaged code, I created the following
declaration in my unmanaged portion:

----
#define SIID_IProgressMonitor S"A340C789-D1E8-4d13-B30D-25B1A2659BD7"

DECLARE_INTERFACE_(IUnmanagedProgressMonitor, IUnknown)
{
    /*** IUnknown methods ***/
    STDMETHOD(QueryInterface)(THIS_ REFIID riid, LPVOID * ppvObj) PURE;
    STDMETHOD_(ULONG,AddRef)(THIS) PURE;
    STDMETHOD_(ULONG,Release)(THIS) PURE;

    /*** IUnmanagedProgressMonitor methods ***/
    STDMETHOD(BeginTask)(THIS_ wchar_t*, int) PURE;
    STDMETHOD(Done)(THIS) PURE;
    STDMETHOD(Worked)(THIS_ int) PURE;
};
----

Then, I have a class in my C# UI that implements IProgressMonitor, which
I pass to a managed C++ function in my core DLL.  This function has some
code that looks like the following:

----
// "monitor" is the IProgressMonitor*

IntPtr unknownIntPtr = Marshal::GetIUnknownForObject(monitor);
Guid guid(SIID_IProgressMonitor);
IntPtr monitorIntPtr;
Marshal::QueryInterface(unknownIntPtr, &guid, &monitorIntPtr);
IUnmanagedProgressMonitor* pMonitor =
(IUnmanagedProgressMonitor*)(void*)monitorIntPtr;

// Call an unmanaged C++ function here, passing it pMonitor

Marshal::Release(monitorIntPtr);
Marshal::Release(unknownIntPtr);
----

Then, the unmanaged C++ function can call functions from
IUnmanagedProgressMonitor, e.g.: pMonitor->BeginTask(L"Some task", 100);

This, as far as I can tell, works like a charm.

--
Tony

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.