471,863 Members | 912 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,863 software developers and data experts.

Error 2664 "cannot convert parameter 1" - callback to managed from unmanaged


I'm new to .NET, and I'm trying to implement a callback function. I want my managed C++ code to call an unmanaged function, passing in a callback function that the unmanaged C/C++ code will call. I'm getting Error 2664: cannot convert parameter 1 from CallBack __gc * to CUSTOM_HOOK_CALLBACK_FUNCTION. Here's what I have...

In my unmanaged DLL -- Header file:
typedef VOID (__stdcall * CUSTOM_HOOK_CALLBACK_FUNCTION )();

extern "C" void __stdcall CUSTOM_HOOK(CUSTOM_HOOK_CALLBACK_FUNCTION callbackFunction);

In my unmanaged DLL -- Source file:

extern "C" void __stdcall CUSTOM_HOOK(CUSTOM_HOOK_CALLBACK_FUNCTION callbackFunction)

// . . . eventually

pCustomHookCallbackFunction = callbackFunction;

In my managed C++ code -- the header file:
namespace Wrappers
public __gc class MyWrapper
void TestTheCallback();
static void DoCallback ( );

In my managed C++ code -- the source file:
// System namespace.
using namespace System::Runtime::InteropServices;

// A delegate type.
__delegate void CallBack();

extern "C" void __stdcall CUSTOM_HOOK(CUSTOM_HOOK_CALLBACK_FUNCTION callbackFunction);

namespace Wrappers

// ...

void MyWrapper::DoCallback ( )
// do something

void WrapperNonRealTimeAPI::TestTheCallback()
CallBack* myCallBack =
new CallBack(this, &MyWrapper::DoCallback);
CUSTOM_HOOK ( myCallBack );
} // namespace


I get the error on the last line: CUSTOM_HOOK ( myCallBack ); I think I understand why the error, but I don't know how to fix it. I've tried to do everything that I've read on the internet on this subject, but it's not working. Help...

(Note: since my hook function and the callback don't pass any parameters, I know the whole thing is pretty useless as a "custom" hook -- but I'm just trying to get this working right now).

Sep 27 '06 #1
1 2720
Hi again... I found one other source (David Notario's blog: http://blogs.msdn.com/davidnotario/a...3/512436.aspx), which clued me in to the fact that you could put the delegate right in the declaration in the managed source, like so:

extern "C" void __stdcall CUSTOM_HOOK(CallBack* callbackFunction);

But now I get a linker error:

error C2733: second C linkage of overloaded function 'CUSTOM_HOOK' not allowed


Sep 27 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Sorisio, Chris | last post: by
6 posts views Thread by juli | last post: by
2 posts views Thread by Andreas Poller | last post: by
5 posts views Thread by Dominiek | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by aboka | last post: by

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.