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