By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,931 Members | 1,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,931 IT Pros & Developers. It's quick & easy.

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

P: 2
Hello.

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:
---------------------------------------------------
CUSTOM_HOOK_CALLBACK_FUNCTION pCustomHookCallbackFunction = NULL;

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

// . . . eventually

pCustomHookCallbackFunction = callbackFunction;
(pCustomHookCallbackFunction)();
}


In my managed C++ code -- the header file:
---------------------------------------------------------
namespace Wrappers
{
public __gc class MyWrapper
{
public:
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();

[DllImport("nr")]
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).

-stillh2os
Sep 27 '06 #1
Share this Question
Share on Google+
1 Reply


P: 2
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

Help...

-stillh2os
Sep 27 '06 #2

Post your reply

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