473,796 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Invocation of C Method

Can someone help by explaining why the following class will not compile
(VS2005), and what can be done to pass the function pointer to the "C"
method?

When I compile the following program, I get "error C2664: 'CMethod' : cannot
convert parameter 1 from 'Test::DoItFn ^' to 'void (__cdecl *)(void *)':

#include "stdafx.h"

void CMethod(void (* pfvFunPointer)( void *), void * pfvValue)
{
(*pfvFunPointer )(pfvValue);
return;
}

ref class Test
{
public:
delegate void DoItFn(void * pfvValue);

virtual void DoIt(void * pfvValue)
{
return;
}

virtual void Run()
{
DoItFn ^ tvfnDoIt = gcnew DoItFn(this, &Test::DoIt) ;
CMethod(tvfnDoI t, nullptr);
return;
}
};

Can someone please help? Any good documentation references would also be
appreciated.

Thanks, Tyler
Feb 25 '06 #1
3 1428
http://msdn2.microsoft.com/en-us/lib.../367eeye0.aspx

Willy.

"Tyler" <Ty****@newsgro ups.nospam> wrote in message
news:ek******** *****@TK2MSFTNG P14.phx.gbl...
Can someone help by explaining why the following class will not compile
(VS2005), and what can be done to pass the function pointer to the "C"
method?

When I compile the following program, I get "error C2664: 'CMethod' :
cannot convert parameter 1 from 'Test::DoItFn ^' to 'void (__cdecl *)(void
*)':

#include "stdafx.h"

void CMethod(void (* pfvFunPointer)( void *), void * pfvValue)
{
(*pfvFunPointer )(pfvValue);
return;
}

ref class Test
{
public:
delegate void DoItFn(void * pfvValue);

virtual void DoIt(void * pfvValue)
{
return;
}

virtual void Run()
{
DoItFn ^ tvfnDoIt = gcnew DoItFn(this, &Test::DoIt) ;
CMethod(tvfnDoI t, nullptr);
return;
}
};

Can someone please help? Any good documentation references would also be
appreciated.

Thanks, Tyler


Feb 25 '06 #2
Hi Tyler,
Thanks for Willy's reply!

I just wanted to check how things are going and whether Willy's suggestion
is useful for you. If there is any question, please feel free to join the
community and we are here to support you at your convenience. Thanks for
your understanding!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
From: "Tyler" <Ty****@newsgro ups.nospam>
Subject: Help with Invocation of C Method
Date: Sat, 25 Feb 2006 15:13:00 -0500
Lines: 39
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <ek************ *@TK2MSFTNGP14. phx.gbl>
Newsgroups: microsoft.publi c.dotnet.langua ges.vc
NNTP-Posting-Host: cpe0004e2fa95db-cm00e06f161d18. cpe.net.cable.r ogers.com 70.29.125.126Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP14.phx. gbl
Xref: TK2MSFTNGXA01.p hx.gbl microsoft.publi c.dotnet.langua ges.vc:54433
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vc

Can someone help by explaining why the following class will not compile
(VS2005), and what can be done to pass the function pointer to the "C"
method?

When I compile the following program, I get "error C2664: 'CMethod' : cannotconvert parameter 1 from 'Test::DoItFn ^' to 'void (__cdecl *)(void *)':

#include "stdafx.h"

void CMethod(void (* pfvFunPointer)( void *), void * pfvValue)
{
(*pfvFunPointer )(pfvValue);
return;
}

ref class Test
{
public:
delegate void DoItFn(void * pfvValue);

virtual void DoIt(void * pfvValue)
{
return;
}

virtual void Run()
{
DoItFn ^ tvfnDoIt = gcnew DoItFn(this, &Test::DoIt) ;
CMethod(tvfnDoI t, nullptr);
return;
}
};

Can someone please help? Any good documentation references would also be
appreciated.

Thanks, Tyler


Mar 2 '06 #3
Thanks Willy,

The link was a lot of help. I have still one problem though that I hope
someone can help with. When I take the example and modify the callback
function to have a parameter, I get unexplained behaviour where my managed
callback function is invoked 2x's. I think this has to do with the calling
convention (if I add __stdcall to the ANSWERCB typedef line, the problem
goes awa), but I'm not sure what I should really do (is there a better
answer, something that makes more sense?) to fix the problem.

Can someone help - I have included modified test below. When I run it, the
GetNumber method is invoked 2x's - once when expected, and once after
execution returns to the main() immediately after having executed the first
TakesCallback() method.

Thanks, Tyler
---------------------------------

// MarshalDelegate 2.cpp
// compile with: /clr
#include "stdafx.h"
#include <iostream>

using namespace System;
using namespace System::Runtime ::InteropServic es;

#pragma unmanaged
typedef int (/* __stdcall */ *ANSWERCB)(void *);
static ANSWERCB cb;

int TakesCallback(A NSWERCB fp, void * fpValue) {
cb = fp;
if (cb) {
printf_s("[unmanaged] got callback address (%d), calling it...\n",
cb);
return cb(fpValue);
}
printf_s("[unmanaged] unregistering callback");
return 0;
}
#pragma managed

public delegate int GetTheAnswerDel egate(void *);

int GetNumber(void * fpValue) {
Console::WriteL ine("[managed] callback!");
static int x = 0;
return ++x;
}

static GCHandle gch;

int main() {
GetTheAnswerDel egate^ fp = gcnew GetTheAnswerDel egate(GetNumber );

gch = GCHandle::Alloc (fp);

IntPtr ip = Marshal::GetFun ctionPointerFor Delegate(fp);
ANSWERCB cb = static_cast<ANS WERCB>(ip.ToPoi nter());
Console::WriteL ine("[managed] sending delegate as callback...");

int answer = TakesCallback(c b, 0);

// possibly much later (in another function)...

Console::WriteL ine("[managed] releasing callback mechanisms...") ;
TakesCallback(0 , 0);
gch.Free();
}

---------------------------------
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:99******** *************** ***********@mic rosoft.com...
http://msdn2.microsoft.com/en-us/lib.../367eeye0.aspx

Willy.

"Tyler" <Ty****@newsgro ups.nospam> wrote in message
news:ek******** *****@TK2MSFTNG P14.phx.gbl...
Can someone help by explaining why the following class will not compile
(VS2005), and what can be done to pass the function pointer to the "C"
method?

When I compile the following program, I get "error C2664: 'CMethod' :
cannot convert parameter 1 from 'Test::DoItFn ^' to 'void (__cdecl
*)(void *)':

#include "stdafx.h"

void CMethod(void (* pfvFunPointer)( void *), void * pfvValue)
{
(*pfvFunPointer )(pfvValue);
return;
}

ref class Test
{
public:
delegate void DoItFn(void * pfvValue);

virtual void DoIt(void * pfvValue)
{
return;
}

virtual void Run()
{
DoItFn ^ tvfnDoIt = gcnew DoItFn(this, &Test::DoIt) ;
CMethod(tvfnDoI t, nullptr);
return;
}
};

Can someone please help? Any good documentation references would also be
appreciated.

Thanks, Tyler

Mar 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1483
by: Bill | last post by:
When I compile the following code fragment, the compiler errors saying the variable connection is not initialized. As I understand C#, I thought it would be initialized. Please help me and explain what I am missing Bil static void Main(string args SqlConnection connection try // to open the connection to the database connection=new SqlConnection((string)( System.Configuration.ConfigurationSettings.AppSettings))
6
15975
by: Sharon | last post by:
How to catch an asynchronous delegate invocation exception? The code: --------------------------------------- String message = “my message”; foreach( MyDelegate handler in m_MyDelegate.GetInvocationList() ) { try { handler.BeginInvoke(message, null, null);
3
2328
by: N8 | last post by:
I am trying to get an exception to occur and consequently found that when adding a target method to a delegates invocation list, a copy of that object is added instead of a reference to the object. Here's what I'm trying to do... I am iterating through the invocation list invoking each delegate manually. I want to eventually try to invoke a delegate to an object that has been destroyed. I'm essentially looking to get the exception...
5
295
by: Praveen | last post by:
here is my code. want to use multithreading. Means, want to call DoThread() in a different thread and want output as DefBegin InThread OutThread DefEnd is it possible?
0
9531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10187
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.