Click here to Skip to main content
15,923,142 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
QuestionC++/CLI being used with VB and VC++ Pin
Shadowsoal14-Jul-09 8:45
Shadowsoal14-Jul-09 8:45 
AnswerRe: C++/CLI being used with VB and VC++ Pin
N a v a n e e t h14-Jul-09 16:12
N a v a n e e t h14-Jul-09 16:12 
GeneralRe: C++/CLI being used with VB and VC++ Pin
Shadowsoal15-Jul-09 4:13
Shadowsoal15-Jul-09 4:13 
GeneralRe: C++/CLI being used with VB and VC++ Pin
Shadowsoal15-Jul-09 5:21
Shadowsoal15-Jul-09 5:21 
GeneralRe: C++/CLI being used with VB and VC++ Pin
N a v a n e e t h15-Jul-09 17:49
N a v a n e e t h15-Jul-09 17:49 
GeneralRe: C++/CLI being used with VB and VC++ Pin
Shadowsoal15-Jul-09 17:52
Shadowsoal15-Jul-09 17:52 
GeneralRe: C++/CLI being used with VB and VC++ Pin
N a v a n e e t h15-Jul-09 18:23
N a v a n e e t h15-Jul-09 18:23 
QuestionNeed to call C++ function callback from C code in a Windows Form app Pin
KawiRider13-Jul-09 15:44
KawiRider13-Jul-09 15:44 
Hi All,

As the title suggests, I have some C code serving as an API for interacting with a certain device. We're in the process of porting the current console application to a GUI app that uses Windows Forms via C++. Some of the C code takes function pointers as callbacks, and I'm currently struggling with figuring out how to pass managed C++ delegates in as those parameters.

Here's what I have so far - from Form1.h:
public: delegate void FuncCallbackDelegate(String^ str, int status);

public: void funcCallback(String^ str, int status)
{
    printf("Passed in string: %s\n", str);
}

private: System::Void btnStartTest_Click(System::Object^  sender, System::EventArgs^  e)
{
    FuncCallbackDelegate^ FuncDelegate = gcnew FuncCallbackDelegate(this, &Form1::funcCallback);
    int retval = CFunction("Dummy path", Marshal::GetFunctionPointerForDelegate(FuncDelegate).ToPointer());  // THIS LINE FAILS
}


Some more code - this time from the .h file included in my Windows Form app:
#ifdef __cplusplus
extern "C" {
#endif

int CFunction(char *src_path, void (__stdcall *func)(const char *,int ));

#ifdef __cplusplus
}
#endif


And lastly, the C function itself:
int CFunction(char *src_path,void (__stdcall *func)(const char *, int))
{
    if (func != NULL)
    {
        func("Test Output String", 1);
    }
    
    return 1;
}


I understand why the failed line fails - Marshal::GetFunctionPointerForDelegate(FuncDelegate).ToPointer() causes this error:
<br />
error C2664: 'CFunction' : cannot convert parameter 2 from 'void *' to 'void (__cdecl *)(const char *,int)'<br />


My question is, what do I need to do to allow a Managed C++ function to be passed in to a C function as a callback? (I tried using #pragma unmanaged and #pragma managed flags to no avail)

Thank You.
AnswerRe: Need to call C++ function callback from C code in a Windows Form app Pin
Mark Salsbery14-Jul-09 6:48
Mark Salsbery14-Jul-09 6:48 
GeneralRe: Need to call C++ function callback from C code in a Windows Form app Pin
KawiRider14-Jul-09 8:59
KawiRider14-Jul-09 8:59 
GeneralRe: Need to call C++ function callback from C code in a Windows Form app Pin
Mark Salsbery14-Jul-09 9:49
Mark Salsbery14-Jul-09 9:49 
GeneralRe: Need to call C++ function callback from C code in a Windows Form app Pin
KawiRider14-Jul-09 12:13
KawiRider14-Jul-09 12:13 
GeneralRe: Need to call C++ function callback from C code in a Windows Form app Pin
Daniel Grunwald20-Jul-09 4:47
Daniel Grunwald20-Jul-09 4:47 
QuestionSample for migrating C++ console app Pin
alleyes13-Jul-09 7:52
professionalalleyes13-Jul-09 7:52 
AnswerRe: Sample for migrating C++ console app Pin
Randor 14-Jul-09 11:24
professional Randor 14-Jul-09 11:24 
GeneralRe: Sample for migrating C++ console app Pin
alleyes15-Jul-09 2:24
professionalalleyes15-Jul-09 2:24 
QuestionPlease Help -- Link Error in Managed Wrapper Pin
Bharat Mallapur11-Jul-09 8:55
Bharat Mallapur11-Jul-09 8:55 
AnswerRe: Please Help -- Link Error in Managed Wrapper Pin
Bharat Mallapur13-Jul-09 23:14
Bharat Mallapur13-Jul-09 23:14 
QuestionMixed DLL Pin
cherrymotion10-Jul-09 5:29
cherrymotion10-Jul-09 5:29 
AnswerRe: Mixed DLL Pin
Richard Andrew x6414-Jul-09 20:12
professionalRichard Andrew x6414-Jul-09 20:12 
QuestionCEdit Phone number Pin
DanYELL9-Jul-09 10:00
DanYELL9-Jul-09 10:00 
AnswerRe: CEdit Phone number Pin
teejayem14-Jul-09 7:17
teejayem14-Jul-09 7:17 
QuestionDeclaring variables as 'new' in C++ CLR - Heap or GCHeap? Pin
Will58019-Jul-09 5:31
Will58019-Jul-09 5:31 
AnswerRe: Declaring variables as 'new' in C++ CLR - Heap or GCHeap? Pin
Mark Salsbery9-Jul-09 6:49
Mark Salsbery9-Jul-09 6:49 
QuestionCLR C++ Q. Does BYTE b[10] create a ref class System.Array? Pin
Will58019-Jul-09 3:15
Will58019-Jul-09 3:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.