473,396 Members | 1,879 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,396 software developers and data experts.

C code passing a pointer to a function to a C# .Net object?

TN
I have a bit of C code, that creates an instance of a .Net class that
has been built as a type library. Everything is working as expected, I
can pass strings to methods in the object. What I would like to also do
is pass a pointer to C function to one method to store it as a callback,
then later have another method call that callback. I know this is not a
"safe" operation, but I am wondering if it can be done. If not what
other methods do I have to implement a callback to C code from a .Net
class that is a type library?
Thanks much.
Mar 16 '06 #1
2 1531
vj
Check these below... one of them might help

http://www.codeproject.com/dotnet/Cdecl_CSharp_VB.asp

http://www.ddj.com/documents/s=1642/ddj0303d/

In general you have to use.. delegates to achieve... there is a good article
on MSDN on delegates... just don't have it saved...

VJ
"TN" <ti*******@phreaker.net> wrote in message
news:ek*************@TK2MSFTNGP12.phx.gbl...
I have a bit of C code, that creates an instance of a .Net class that has
been built as a type library. Everything is working as expected, I can
pass strings to methods in the object. What I would like to also do is
pass a pointer to C function to one method to store it as a callback, then
later have another method call that callback. I know this is not a "safe"
operation, but I am wondering if it can be done. If not what other methods
do I have to implement a callback to C code from a .Net class that is a
type library?
Thanks much.

Mar 16 '06 #2

"TN" <ti*******@phreaker.net> wrote in message
news:ek*************@TK2MSFTNGP12.phx.gbl...
|I have a bit of C code, that creates an instance of a .Net class that
| has been built as a type library. Everything is working as expected, I
| can pass strings to methods in the object. What I would like to also do
| is pass a pointer to C function to one method to store it as a callback,
| then later have another method call that callback. I know this is not a
| "safe" operation, but I am wondering if it can be done. If not what
| other methods do I have to implement a callback to C code from a .Net
| class that is a type library?
| Thanks much.

Pass the function pointer to .NET as a long on 32 bit OS, as a long long on
64 bit.

C++
static void CALLBACK CallbackProc(args..)
{
......
}

....
obj->SomeCOMMethod(reinterpret_cast<long>(CallbackProc ));
C#

delegate void Proc(args..);

SomeCOMMethod(IntPtr fPtr)
{
...
Proc p = (Proc)Marshal.GetDelegateForFunctionPointer(fPtr,
typeof(Proc));
p(...); // Call CallBackProc
....

Willy.
Mar 16 '06 #3

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

Similar topics

3
by: Andy Read | last post by:
Dear all, I thought I understood passing parameters ByVal and ByRef but I clearly don't! If I define a simple class of: Public Class Person Public Name as String Public Age as Integer End...
5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
3
by: dice | last post by:
Hi, In order to use an external api call that requires a function pointer I am currently creating static wrappers to call my objects functions. I want to re-jig this so I only need 1 static...
6
by: Roman Mashak | last post by:
Hello, I belive the reason of problem is simple, but can't figure out. This is piece of code: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };
8
by: Ivan Liu | last post by:
Hi, I'd like to ask if passing an object as an pointer into a function evokes the copy constructor. Ivan
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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 project—planning, coding, testing,...

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.