[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Trouble passing a native function pointer to managed code

Skwerl

10/18/2007 7:58:00 PM

I am writing a native C++ plug-in for a program that will act as an interface
for managed code. My managed code needs to be able to call methods in an
active instance of the native plug-in running in its host program. Is the
following the best way (or even possible) to accomplish this?

1. Plug-in host program loads native plug-in.
2. Plug-in calls method in managed.dll, passing a function pointer
(&this->UnmanagedMethod) to ManagedClass.ManagedMethod(IntPtr funcPtr).
3. ManagedClass creates DelegateFromFuncPtr from function pointer and stores
it in class instance.
4. Managed class calls DelegateFromFuncPtr as necessary to communicate with
native plug-in instance.

If I'm going about this in a legitimate way, I'm having a hard time handing
the function pointer to the managed class. The compiler gives me a 2664
error because it won't allow me to cast a function pointer to a long (which
is what the CCW is asking for as an IntPtr). Any suggestions? Thanks in
advance.

//Native code sections
HRESULT __stdcall CVDJPlug::OnLoad()
{
//Initialize COM object
VDJCom::_VDJ *com_ptr;
CoInitialize(NULL);
VDJCom::_VDJPtr p(__uuidof(VDJCom::VDJ));
com_ptr = p;

//Give the managed class a pointer to a method in this class
instance
com_ptr->SetInfoPointer(&CVDJPlug::GetInfoMiddle);
return 0;
}

//The native member function
void CVDJPlug::GetInfoMiddle(char*, void*)
{
....
}


3 Answers

(Mattias Sjögren)

10/18/2007 8:07:00 PM

0

>1. Plug-in host program loads native plug-in.
>2. Plug-in calls method in managed.dll, passing a function pointer
>(&this->UnmanagedMethod) to ManagedClass.ManagedMethod(IntPtr funcPtr).
>3. ManagedClass creates DelegateFromFuncPtr from function pointer and stores
>it in class instance.
>4. Managed class calls DelegateFromFuncPtr as necessary to communicate with
>native plug-in instance.

In theory that should work, yes. But wouldn't it be a lot easier to
just use a COM callback interface?


>If I'm going about this in a legitimate way, I'm having a hard time handing
>the function pointer to the managed class. The compiler gives me a 2664
>error because it won't allow me to cast a function pointer to a long (which
>is what the CCW is asking for as an IntPtr). Any suggestions?

Well you need to explicitly cast it if that's what you want to do. And
the target function must be static.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.n... | http://www.dotneti...
Please reply only to the newsgroup.

Skwerl

10/19/2007 3:36:00 PM

0

Thank you, Mattias.

> In theory that should work, yes. But wouldn't it be a lot easier to
> just use a COM callback interface?

When you say a COM callback, you mean the COM object would be calling the
unmanaged code, right? I'd really like the .Net code to have complete
control, and I want to have all the timers and power in the managed code.
I'd only want the native plug-in to be a proxy between the host program and
the .Net code.

> Well you need to explicitly cast it if that's what you want to do. And
> the target function must be static.

I've tried the explicit cast, and it would not allow me to do that either.
It seems awfully finicky.

Josh Usovsky


"Mattias Sjögren" wrote:

> >1. Plug-in host program loads native plug-in.
> >2. Plug-in calls method in managed.dll, passing a function pointer
> >(&this->UnmanagedMethod) to ManagedClass.ManagedMethod(IntPtr funcPtr).
> >3. ManagedClass creates DelegateFromFuncPtr from function pointer and stores
> >it in class instance.
> >4. Managed class calls DelegateFromFuncPtr as necessary to communicate with
> >native plug-in instance.
>
> In theory that should work, yes. But wouldn't it be a lot easier to
> just use a COM callback interface?
>
>
> >If I'm going about this in a legitimate way, I'm having a hard time handing
> >the function pointer to the managed class. The compiler gives me a 2664
> >error because it won't allow me to cast a function pointer to a long (which
> >is what the CCW is asking for as an IntPtr). Any suggestions?
>
> Well you need to explicitly cast it if that's what you want to do. And
> the target function must be static.
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.org
> http://www.msjogren.n... | http://www.dotneti...
> Please reply only to the newsgroup.
>

(Mattias Sjögren)

10/20/2007 5:18:00 PM

0

>When you say a COM callback, you mean the COM object would be calling the
>unmanaged code, right?

I mean that the COM object itself is implemented in your native
plug-in, and you pass an interface reference to it to your managed
code (instead of a simple function pointer). The managed code then
calls a method on the COM interface to perform the callback. The end
result is pretty much the same but you have to do less work yourself.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.n... | http://www.dotneti...
Please reply only to the newsgroup.