473,325 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Cannot pass a __delegate pointer to C++ function?

I have no trouble passing __delegate ptrs to native C functions in DLLs,
however when attempting to pass the __delegate ptr to a native C++ function
in a DLL I get the following runtime exception: An unhandled exception of
type 'System.EntryPointNotFoundException'.

This is confusing b/c if the entry point was not found, I would think I'd
get an unresolved symbol error during linking. Furthermore, I can
successfully pass void, int *, and function pointers, etc to C++ DLL
functions, but this error persists when attempting to pass a __delegate
pointer, which is used for callbacks to managed C++ functions.

Here is the relevant code snippet - the DLL code simply contains stubs of
the functions called here:

#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;
using namespace System::Runtime::InteropServices;

__delegate void testCB();
[DllImport("unmdll.dll")]
void fnunmdll_2(testCB *UpdateProgMeter);

int fnunmdll_0(void);
void fnunmdll_1(int *x);//testCB *UpdateProgMeter);

__gc class C1
{
public:
static void handleCB()
{
return;
}
};

int _tmain()
{
int k = 9;

//always works (as both C/C++)
fnunmdll_0();

//always works (as both C/C++)
fnunmdll_1(&k);

testCB *pCB = new testCB(0, &C1::handleCB);

//if this is a C++ function expecting a function pointer, the
aforementioned error occurs. If this is an extern "C" function, all is ok!
fnunmdll_2(pCB);

return 0;
}

As crazy as it sounds, I'm beginning to think this is not possible to do
with native C++ functions.

Thanks in advance, George
Nov 17 '05 #1
12 1682
This has nothing to do with delegates, this is a C++ name mangling issue,
your DllImport functions (fnunmdll_2) must use C linkage exports, or an
appropriate .DEF file.
extern "C"
{
....

}

Willy.

"glutz7878" <gl*******@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
I have no trouble passing __delegate ptrs to native C functions in DLLs,
however when attempting to pass the __delegate ptr to a native C++
function
in a DLL I get the following runtime exception: An unhandled exception of
type 'System.EntryPointNotFoundException'.

This is confusing b/c if the entry point was not found, I would think I'd
get an unresolved symbol error during linking. Furthermore, I can
successfully pass void, int *, and function pointers, etc to C++ DLL
functions, but this error persists when attempting to pass a __delegate
pointer, which is used for callbacks to managed C++ functions.

Here is the relevant code snippet - the DLL code simply contains stubs of
the functions called here:

#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;
using namespace System::Runtime::InteropServices;

__delegate void testCB();
[DllImport("unmdll.dll")]
void fnunmdll_2(testCB *UpdateProgMeter);

int fnunmdll_0(void);
void fnunmdll_1(int *x);//testCB *UpdateProgMeter);

__gc class C1
{
public:
static void handleCB()
{
return;
}
};

int _tmain()
{
int k = 9;

//always works (as both C/C++)
fnunmdll_0();

//always works (as both C/C++)
fnunmdll_1(&k);

testCB *pCB = new testCB(0, &C1::handleCB);

//if this is a C++ function expecting a function pointer, the
aforementioned error occurs. If this is an extern "C" function, all is
ok!
fnunmdll_2(pCB);

return 0;
}

As crazy as it sounds, I'm beginning to think this is not possible to do
with native C++ functions.

Thanks in advance, George

Nov 17 '05 #2
This has nothing to do with delegates, this is a C++ name mangling issue,
your DllImport functions (fnunmdll_2) must use C linkage exports, or an
appropriate .DEF file.
extern "C"
{
....

}

Willy.

"glutz7878" <gl*******@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
I have no trouble passing __delegate ptrs to native C functions in DLLs,
however when attempting to pass the __delegate ptr to a native C++
function
in a DLL I get the following runtime exception: An unhandled exception of
type 'System.EntryPointNotFoundException'.

This is confusing b/c if the entry point was not found, I would think I'd
get an unresolved symbol error during linking. Furthermore, I can
successfully pass void, int *, and function pointers, etc to C++ DLL
functions, but this error persists when attempting to pass a __delegate
pointer, which is used for callbacks to managed C++ functions.

Here is the relevant code snippet - the DLL code simply contains stubs of
the functions called here:

#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;
using namespace System::Runtime::InteropServices;

__delegate void testCB();
[DllImport("unmdll.dll")]
void fnunmdll_2(testCB *UpdateProgMeter);

int fnunmdll_0(void);
void fnunmdll_1(int *x);//testCB *UpdateProgMeter);

__gc class C1
{
public:
static void handleCB()
{
return;
}
};

int _tmain()
{
int k = 9;

//always works (as both C/C++)
fnunmdll_0();

//always works (as both C/C++)
fnunmdll_1(&k);

testCB *pCB = new testCB(0, &C1::handleCB);

//if this is a C++ function expecting a function pointer, the
aforementioned error occurs. If this is an extern "C" function, all is
ok!
fnunmdll_2(pCB);

return 0;
}

As crazy as it sounds, I'm beginning to think this is not possible to do
with native C++ functions.

Thanks in advance, George

Nov 17 '05 #3
Thanks for the post but do you know why there are no problems when passing
void or int *, even function pointers? Wouldn't they also have name mangling
issues? It seems strange that the only thing that fails is passing
__delegates.

Thanks again, George
Nov 17 '05 #4
Thanks for the post but do you know why there are no problems when passing
void or int *, even function pointers? Wouldn't they also have name mangling
issues? It seems strange that the only thing that fails is passing
__delegates.

Thanks again, George
Nov 17 '05 #5
Using a .DEF file to get rid of mangled names fixes the problem. extern C
also works of course but the .Def is better.

If anyone can explain why this worked all along for all non-delegate types,
i would really love to know. I'm not sure why name-mangling would affect the
function call based on argument type.

Thanks, George
Nov 17 '05 #6
Using a .DEF file to get rid of mangled names fixes the problem. extern C
also works of course but the .Def is better.

If anyone can explain why this worked all along for all non-delegate types,
i would really love to know. I'm not sure why name-mangling would affect the
function call based on argument type.

Thanks, George
Nov 17 '05 #7

"glutz7878" <gl*******@discussions.microsoft.com> wrote in message
news:67**********************************@microsof t.com...
Thanks for the post but do you know why there are no problems when passing
void or int *, even function pointers? Wouldn't they also have name
mangling
issues? It seems strange that the only thing that fails is passing
__delegates.

Thanks again, George


No, the diference is that you are calling the function through PInvoke
([DllImport("unmdll.dll")]), that means you are dynamically binding against
the library and the entrypoint will be resolved at call time(that's why the
linker doesn't complain), while in the other cases you are statically
linking and the linker is able to resolve the functions called points.

Willy.
Nov 17 '05 #8

"glutz7878" <gl*******@discussions.microsoft.com> wrote in message
news:67**********************************@microsof t.com...
Thanks for the post but do you know why there are no problems when passing
void or int *, even function pointers? Wouldn't they also have name
mangling
issues? It seems strange that the only thing that fails is passing
__delegates.

Thanks again, George


No, the diference is that you are calling the function through PInvoke
([DllImport("unmdll.dll")]), that means you are dynamically binding against
the library and the entrypoint will be resolved at call time(that's why the
linker doesn't complain), while in the other cases you are statically
linking and the linker is able to resolve the functions called points.

Willy.
Nov 17 '05 #9
samm wrote:
Thank you for your help. It works fine, but I have weird problem. It
works only on one of my devices: HTC Touch phone. If I use HTC Mogul
(the same Windows Mobile 6.0, but less memory) it does not. I have
the same .net CF 2.0 on both.
C++ interop doesn't work in the compact framework, but
Marshal::GetFunctionPointerForDelegate does.

__delegate doesn't exist in .NET 2.0, that's .NET 1.x Managed Extensions for
C++ syntax

Explain what you're trying to accomplish and what you're doing so far and we
should be able to help you.
Mar 12 '08 #10
Sure.

1. I have C++ dll which needs to register callback in my C# application.
This callback returns a string:
C# side:

public delegate void RegisterCallBackDelegateInt(System.IntPtr response);
....
RegisterCallBackDelegateInt Callback;
IntPtr callbackPointer;
....

Callback = new
RegisterCallBackDelegateInt(RegisterCallBackInt);
callbackPointer =
Marshal.GetFunctionPointerForDelegate(Callback);
GC.KeepAlive(callbackPointer);
ret = RegisterCallBack(callbackPointer);
....
public void RegisterCallBackInt(System.IntPtr response)
{
string myString = Marshal.PtrToStringUni(response);
Log("Callback: " + myString);
Marshal.FreeHGlobal(response);
}

C++ side

extern "C" {

__declspec( dllexport ) HRESULT RegisterCallBack(void(*CB)(BYTE *xmlResult) );

};

It works fine on Touch HTC phone, but not on Mogul.

2. The reason I do that is because I do not have ability to wrap my C++ dll
to C++ managed anymore. On win32 I can do that and my life is much better.

Thank you for your help.

Sam

"Ben Voigt [C++ MVP]" wrote:
samm wrote:
Thank you for your help. It works fine, but I have weird problem. It
works only on one of my devices: HTC Touch phone. If I use HTC Mogul
(the same Windows Mobile 6.0, but less memory) it does not. I have
the same .net CF 2.0 on both.

C++ interop doesn't work in the compact framework, but
Marshal::GetFunctionPointerForDelegate does.

__delegate doesn't exist in .NET 2.0, that's .NET 1.x Managed Extensions for
C++ syntax

Explain what you're trying to accomplish and what you're doing so far and we
should be able to help you.
Mar 12 '08 #11
samm wrote:
Sure.

1. I have C++ dll which needs to register callback in my C#
application.
This callback returns a string:
C# side:

public delegate void RegisterCallBackDelegateInt(System.IntPtr
response); ...
RegisterCallBackDelegateInt Callback;
IntPtr callbackPointer;
...

Callback = new
RegisterCallBackDelegateInt(RegisterCallBackInt);
callbackPointer =
Marshal.GetFunctionPointerForDelegate(Callback);
GC.KeepAlive(callbackPointer);
ret = RegisterCallBack(callbackPointer);
...
public void RegisterCallBackInt(System.IntPtr response)
{
string myString = Marshal.PtrToStringUni(response);
Log("Callback: " + myString);
Marshal.FreeHGlobal(response);
}

C++ side

extern "C" {

__declspec( dllexport ) HRESULT RegisterCallBack(void(*CB)(BYTE
*xmlResult) );

};

It works fine on Touch HTC phone, but not on Mogul.

2. The reason I do that is because I do not have ability to wrap my
C++ dll to C++ managed anymore. On win32 I can do that and my life is
much better.

Thank you for your help.
You are misusing GC.KeepAlive, so you will experience random failures
whenever a collection takes place before the callback is finished. Use
GCHandle.Alloc instead, on the delegate, not the native function pointer.
>
Sam

Mar 12 '08 #12
Thank you.
Can you give me an example?

"Ben Voigt [C++ MVP]" wrote:
samm wrote:
Sure.

1. I have C++ dll which needs to register callback in my C#
application.
This callback returns a string:
C# side:

public delegate void RegisterCallBackDelegateInt(System.IntPtr
response); ...
RegisterCallBackDelegateInt Callback;
IntPtr callbackPointer;
...

Callback = new
RegisterCallBackDelegateInt(RegisterCallBackInt);
callbackPointer =
Marshal.GetFunctionPointerForDelegate(Callback);
GC.KeepAlive(callbackPointer);
ret = RegisterCallBack(callbackPointer);
...
public void RegisterCallBackInt(System.IntPtr response)
{
string myString = Marshal.PtrToStringUni(response);
Log("Callback: " + myString);
Marshal.FreeHGlobal(response);
}

C++ side

extern "C" {

__declspec( dllexport ) HRESULT RegisterCallBack(void(*CB)(BYTE
*xmlResult) );

};

It works fine on Touch HTC phone, but not on Mogul.

2. The reason I do that is because I do not have ability to wrap my
C++ dll to C++ managed anymore. On win32 I can do that and my life is
much better.

Thank you for your help.

You are misusing GC.KeepAlive, so you will experience random failures
whenever a collection takes place before the callback is finished. Use
GCHandle.Alloc instead, on the delegate, not the native function pointer.

Sam


Mar 12 '08 #13

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

Similar topics

10
by: Doug Jordan | last post by:
I am fairly new to Python. This should be an easy answer but I cannot get this to work. The code is listed below. I know how to do this in C, Fortran, and VB but it doesn't seem to work the same...
3
by: Liu Ju | last post by:
Dear members: I want to use the multithread in my program which is developed in Visual C++ platform (version 6). I created a controlling function: UINT CCOMM1Dlg::WritingThreadFunc(LPVOID...
5
by: Joe Thompson | last post by:
Hi, I am using VC++.Net 2003 with WinForms to write a serial port application. I downloaded the newest VC++ examples from MSDN and found a project called Using the COM Port. In it, there is a...
0
by: Fireangel | last post by:
I'm trying to create a recursive class (Something like a tree), but I have an external function that needs to be called. I'm using __delegate to hold a pointer to this function, which only requires...
0
by: glutz7878 | last post by:
I have no trouble passing __delegate ptrs to native C functions in DLLs, however when attempting to pass the __delegate ptr to a native C++ function in a DLL I get the following runtime exception:...
1
by: stillh2os | last post by:
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...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
8
by: =?Utf-8?B?V2hpdG5leSBLZXc=?= | last post by:
Hi there, I'm having a bit of trouble using an HRASCONN object as a class member variable inside my managed C++ class. When I call RasDial() and pass in the address of my HRASCONN object, I get...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.