Delegates and pointers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to use delegates for the first time and I am probably missing
something. I have a legacy DLL and I have used DllImport to get to the
functions that it contains...the normal function calls work fine, but I can't
get callbacks to work. The delegate declaration etc is below.

public delegate void callbackEvent();

callbackEvent Event = delegate()
{
MessageBox.Show("Callback Event Received");
}


The function in the DLL to register the callback is

myAPIregister(Event);

So I am expecting that myAPIregister(Event) will provide a pointer to the
Event delegate. When the callback event occurs it should simply display the
MessageBox with the text. The parameter in myAPIregister(parameter) is
expecting a pointer to the callback handler.

I have done a lot of reading about delegates over the past few days to get
this far but I appear to be missing something. The Compiler complains that
the argument is invalid.

Thanks for any help and guidance.
 
callbackEvent Event = delegate()
{
MessageBox.Show("Callback Event Received");
}

The above syntax is incorrect.
Treat your callback as a method:

delegate void callbackEvent();

void MyCallBack()
{
MessageBox.Show("Callback Event Received");
}

void ConsumeDelegate()
{
// Create a pointer to MyCallBack using the callbackEvent signature:
callbackEvent cb = new callbackEvent(MyCallBack);

// Use the method pointer in your interop call:
myAPIregister(cb);

// > The parameter in myAPIregister(parameter) is
// > expecting a pointer to the callback handler.
//
// .NET Interop will automattically Marshal the delegate you pass to the external method as a pointer to the method
}
 
Thanks Dave,

I was confused on how to use delegates, but I think I understand how they
are used now. I have used your code as the basis for correcting what I had
done. I still have one final issue that perhaps you may know how to fix.
Here is the code:

delegate void callbackEvent();

void MyCallback()
{
MessageBox.Show("Callback Event Received");
}

private void ptRegisterLinkStateCallback_Click(object sender, EventArgs e)
{
callbackEvent cb = new callbackEvent(MyCallback);
status = ptAPI.ptRegisterLinkStateCallback(ptHostLinkStateChange, cb);
MessageBox.Show("Status = " + status.ToString());
}

So when the "ptRegisterLink...." button is clicked a pointer, cb, is created
to MyCallback. The "ptAPI.ptRegister..." function call is to an external DLL
using DLLImport and I am expecting the 'cb' argument to be the pointer to the
MyCallback method. The code in the DLL is:

public class ptAPI
{
[DllImport("patsapi.dll")]
public static extern int ptRegisterLinkStateCallback(int callbackID, int
mtdAddress);
}

The compiler complains that there are invalid arguments and that argument 2
'cannot convert from "...Form1.callbackEvent to int". Clearly I still have
something wrong with my code.

Any ideas as to why this is wrong? Thanks for your help with this, much
appreciated.

Wayne
 
No problem.

The declaration for your interop call (external method) should also declare the paramter type as the delegate your passing it:
[DllImport("patsapi.dll")]
public static extern int ptRegisterLinkStateCallback(int callbackID, callbackEvent CallBack);

This, of course, means that you have to declare the delegate in your library and not the consuming program.

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Wayne Weeks said:
Thanks Dave,

I was confused on how to use delegates, but I think I understand how they
are used now. I have used your code as the basis for correcting what I had
done. I still have one final issue that perhaps you may know how to fix.
Here is the code:

delegate void callbackEvent();

void MyCallback()
{
MessageBox.Show("Callback Event Received");
}

private void ptRegisterLinkStateCallback_Click(object sender, EventArgs e)
{
callbackEvent cb = new callbackEvent(MyCallback);
status = ptAPI.ptRegisterLinkStateCallback(ptHostLinkStateChange, cb);
MessageBox.Show("Status = " + status.ToString());
}

So when the "ptRegisterLink...." button is clicked a pointer, cb, is created
to MyCallback. The "ptAPI.ptRegister..." function call is to an external DLL
using DLLImport and I am expecting the 'cb' argument to be the pointer to the
MyCallback method. The code in the DLL is:

public class ptAPI
{
[DllImport("patsapi.dll")]
public static extern int ptRegisterLinkStateCallback(int callbackID, int
mtdAddress);
}

The compiler complains that there are invalid arguments and that argument 2
'cannot convert from "...Form1.callbackEvent to int". Clearly I still have
something wrong with my code.

Any ideas as to why this is wrong? Thanks for your help with this, much
appreciated.

Wayne

Dave said:
The above syntax is incorrect.
Treat your callback as a method:

delegate void callbackEvent();

void MyCallBack()
{
MessageBox.Show("Callback Event Received");
}

void ConsumeDelegate()
{
// Create a pointer to MyCallBack using the callbackEvent signature:
callbackEvent cb = new callbackEvent(MyCallBack);

// Use the method pointer in your interop call:
myAPIregister(cb);

// > The parameter in myAPIregister(parameter) is
// > expecting a pointer to the callback handler.
//
// .NET Interop will automattically Marshal the delegate you pass to the external method as a pointer to the method
}
 
OK, thankyou, yes that makes sense, need to make the parameter types the
same. Just one final issue and I hope I can move on with the fun part of
this application. As you note, I added the declaration of the delegate to
the library code as follows:

public class ptAPI
{
delegate void callbackEvent();

[DllImport("patsapi.dll")]
public static extern int ptRegisterLinkStateCallback(int callbackID,
callbackEvent Callback);
}

However when I try to recompile the library the compiler complains that
there is "Inconsistent accessibility", parameter type ptAPI.callbackEvent is
less accessibile than ptAPI.ptRegisterLinkStateCallback. Am I missing a
switch for the compiler? Something wrong with declaration of the delegate in
the library. This delegate construct is a tricky thing to grasp....thanks
again, hopefully I can get past this soon.

Wayne



Dave said:
No problem.

The declaration for your interop call (external method) should also declare the paramter type as the delegate your passing it:
[DllImport("patsapi.dll")]
public static extern int ptRegisterLinkStateCallback(int callbackID, callbackEvent CallBack);

This, of course, means that you have to declare the delegate in your library and not the consuming program.

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Wayne Weeks said:
Thanks Dave,

I was confused on how to use delegates, but I think I understand how they
are used now. I have used your code as the basis for correcting what I had
done. I still have one final issue that perhaps you may know how to fix.
Here is the code:

delegate void callbackEvent();

void MyCallback()
{
MessageBox.Show("Callback Event Received");
}

private void ptRegisterLinkStateCallback_Click(object sender, EventArgs e)
{
callbackEvent cb = new callbackEvent(MyCallback);
status = ptAPI.ptRegisterLinkStateCallback(ptHostLinkStateChange, cb);
MessageBox.Show("Status = " + status.ToString());
}

So when the "ptRegisterLink...." button is clicked a pointer, cb, is created
to MyCallback. The "ptAPI.ptRegister..." function call is to an external DLL
using DLLImport and I am expecting the 'cb' argument to be the pointer to the
MyCallback method. The code in the DLL is:

public class ptAPI
{
[DllImport("patsapi.dll")]
public static extern int ptRegisterLinkStateCallback(int callbackID, int
mtdAddress);
}

The compiler complains that there are invalid arguments and that argument 2
'cannot convert from "...Form1.callbackEvent to int". Clearly I still have
something wrong with my code.

Any ideas as to why this is wrong? Thanks for your help with this, much
appreciated.

Wayne

Dave said:
callbackEvent Event = delegate()
{
MessageBox.Show("Callback Event Received");
}

The above syntax is incorrect.
Treat your callback as a method:

delegate void callbackEvent();

void MyCallBack()
{
MessageBox.Show("Callback Event Received");
}

void ConsumeDelegate()
{
// Create a pointer to MyCallBack using the callbackEvent signature:
callbackEvent cb = new callbackEvent(MyCallBack);

// Use the method pointer in your interop call:
myAPIregister(cb);

// > The parameter in myAPIregister(parameter) is
// > expecting a pointer to the callback handler.
//
// .NET Interop will automattically Marshal the delegate you pass to the external method as a pointer to the method
}

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
I am trying to use delegates for the first time and I am probably missing
something. I have a legacy DLL and I have used DllImport to get to the
functions that it contains...the normal function calls work fine, but I can't
get callbacks to work. The delegate declaration etc is below.

public delegate void callbackEvent();

callbackEvent Event = delegate()
{
MessageBox.Show("Callback Event Received");
}


The function in the DLL to register the callback is

myAPIregister(Event);

So I am expecting that myAPIregister(Event) will provide a pointer to the
Event delegate. When the callback event occurs it should simply display the
MessageBox with the text. The parameter in myAPIregister(parameter) is
expecting a pointer to the callback handler.

I have done a lot of reading about delegates over the past few days to get
this far but I appear to be missing something. The Compiler complains that
the argument is invalid.

Thanks for any help and guidance.
 
you haven't specified an access modifier on the delegate declaration. This error has nothing to do with the fact that your using a
delegate :) My sample code didn't specify it because it was intended for use within class-scope.

Try:

public delegate void callbackEvent();



--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Wayne Weeks said:
OK, thankyou, yes that makes sense, need to make the parameter types the
same. Just one final issue and I hope I can move on with the fun part of
this application. As you note, I added the declaration of the delegate to
the library code as follows:

public class ptAPI
{
delegate void callbackEvent();

[DllImport("patsapi.dll")]
public static extern int ptRegisterLinkStateCallback(int callbackID,
callbackEvent Callback);
}

However when I try to recompile the library the compiler complains that
there is "Inconsistent accessibility", parameter type ptAPI.callbackEvent is
less accessibile than ptAPI.ptRegisterLinkStateCallback. Am I missing a
switch for the compiler? Something wrong with declaration of the delegate in
the library. This delegate construct is a tricky thing to grasp....thanks
again, hopefully I can get past this soon.

Wayne



Dave said:
No problem.

The declaration for your interop call (external method) should also declare the paramter type as the delegate your passing it:
[DllImport("patsapi.dll")]
public static extern int ptRegisterLinkStateCallback(int callbackID, callbackEvent CallBack);

This, of course, means that you have to declare the delegate in your library and not the consuming program.

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Wayne Weeks said:
Thanks Dave,

I was confused on how to use delegates, but I think I understand how they
are used now. I have used your code as the basis for correcting what I had
done. I still have one final issue that perhaps you may know how to fix.
Here is the code:

delegate void callbackEvent();

void MyCallback()
{
MessageBox.Show("Callback Event Received");
}

private void ptRegisterLinkStateCallback_Click(object sender, EventArgs e)
{
callbackEvent cb = new callbackEvent(MyCallback);
status = ptAPI.ptRegisterLinkStateCallback(ptHostLinkStateChange, cb);
MessageBox.Show("Status = " + status.ToString());
}

So when the "ptRegisterLink...." button is clicked a pointer, cb, is created
to MyCallback. The "ptAPI.ptRegister..." function call is to an external DLL
using DLLImport and I am expecting the 'cb' argument to be the pointer to the
MyCallback method. The code in the DLL is:

public class ptAPI
{
[DllImport("patsapi.dll")]
public static extern int ptRegisterLinkStateCallback(int callbackID, int
mtdAddress);
}

The compiler complains that there are invalid arguments and that argument 2
'cannot convert from "...Form1.callbackEvent to int". Clearly I still have
something wrong with my code.

Any ideas as to why this is wrong? Thanks for your help with this, much
appreciated.

Wayne

:

callbackEvent Event = delegate()
{
MessageBox.Show("Callback Event Received");
}

The above syntax is incorrect.
Treat your callback as a method:

delegate void callbackEvent();

void MyCallBack()
{
MessageBox.Show("Callback Event Received");
}

void ConsumeDelegate()
{
// Create a pointer to MyCallBack using the callbackEvent signature:
callbackEvent cb = new callbackEvent(MyCallBack);

// Use the method pointer in your interop call:
myAPIregister(cb);

// > The parameter in myAPIregister(parameter) is
// > expecting a pointer to the callback handler.
//
// .NET Interop will automattically Marshal the delegate you pass to the external method as a pointer to the method
}

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
I am trying to use delegates for the first time and I am probably missing
something. I have a legacy DLL and I have used DllImport to get to the
functions that it contains...the normal function calls work fine, but I can't
get callbacks to work. The delegate declaration etc is below.

public delegate void callbackEvent();

callbackEvent Event = delegate()
{
MessageBox.Show("Callback Event Received");
}


The function in the DLL to register the callback is

myAPIregister(Event);

So I am expecting that myAPIregister(Event) will provide a pointer to the
Event delegate. When the callback event occurs it should simply display the
MessageBox with the text. The parameter in myAPIregister(parameter) is
expecting a pointer to the callback handler.

I have done a lot of reading about delegates over the past few days to get
this far but I appear to be missing something. The Compiler complains that
the argument is invalid.

Thanks for any help and guidance.
 
Dave, a big thank you....finally I can get the code to build and it seems to
work....thanks again for your support getting this resolved...now on to more
interesting development.

Dave said:
you haven't specified an access modifier on the delegate declaration. This error has nothing to do with the fact that your using a
delegate :) My sample code didn't specify it because it was intended for use within class-scope.

Try:

public delegate void callbackEvent();



--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Wayne Weeks said:
OK, thankyou, yes that makes sense, need to make the parameter types the
same. Just one final issue and I hope I can move on with the fun part of
this application. As you note, I added the declaration of the delegate to
the library code as follows:

public class ptAPI
{
delegate void callbackEvent();

[DllImport("patsapi.dll")]
public static extern int ptRegisterLinkStateCallback(int callbackID,
callbackEvent Callback);
}

However when I try to recompile the library the compiler complains that
there is "Inconsistent accessibility", parameter type ptAPI.callbackEvent is
less accessibile than ptAPI.ptRegisterLinkStateCallback. Am I missing a
switch for the compiler? Something wrong with declaration of the delegate in
the library. This delegate construct is a tricky thing to grasp....thanks
again, hopefully I can get past this soon.

Wayne



Dave said:
No problem.

The declaration for your interop call (external method) should also declare the paramter type as the delegate your passing it:

[DllImport("patsapi.dll")]
public static extern int ptRegisterLinkStateCallback(int callbackID, callbackEvent CallBack);

This, of course, means that you have to declare the delegate in your library and not the consuming program.

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Thanks Dave,

I was confused on how to use delegates, but I think I understand how they
are used now. I have used your code as the basis for correcting what I had
done. I still have one final issue that perhaps you may know how to fix.
Here is the code:

delegate void callbackEvent();

void MyCallback()
{
MessageBox.Show("Callback Event Received");
}

private void ptRegisterLinkStateCallback_Click(object sender, EventArgs e)
{
callbackEvent cb = new callbackEvent(MyCallback);
status = ptAPI.ptRegisterLinkStateCallback(ptHostLinkStateChange, cb);
MessageBox.Show("Status = " + status.ToString());
}

So when the "ptRegisterLink...." button is clicked a pointer, cb, is created
to MyCallback. The "ptAPI.ptRegister..." function call is to an external DLL
using DLLImport and I am expecting the 'cb' argument to be the pointer to the
MyCallback method. The code in the DLL is:

public class ptAPI
{
[DllImport("patsapi.dll")]
public static extern int ptRegisterLinkStateCallback(int callbackID, int
mtdAddress);
}

The compiler complains that there are invalid arguments and that argument 2
'cannot convert from "...Form1.callbackEvent to int". Clearly I still have
something wrong with my code.

Any ideas as to why this is wrong? Thanks for your help with this, much
appreciated.

Wayne

:

callbackEvent Event = delegate()
{
MessageBox.Show("Callback Event Received");
}

The above syntax is incorrect.
Treat your callback as a method:

delegate void callbackEvent();

void MyCallBack()
{
MessageBox.Show("Callback Event Received");
}

void ConsumeDelegate()
{
// Create a pointer to MyCallBack using the callbackEvent signature:
callbackEvent cb = new callbackEvent(MyCallBack);

// Use the method pointer in your interop call:
myAPIregister(cb);

// > The parameter in myAPIregister(parameter) is
// > expecting a pointer to the callback handler.
//
// .NET Interop will automattically Marshal the delegate you pass to the external method as a pointer to the method
}

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
I am trying to use delegates for the first time and I am probably missing
something. I have a legacy DLL and I have used DllImport to get to the
functions that it contains...the normal function calls work fine, but I can't
get callbacks to work. The delegate declaration etc is below.

public delegate void callbackEvent();

callbackEvent Event = delegate()
{
MessageBox.Show("Callback Event Received");
}


The function in the DLL to register the callback is

myAPIregister(Event);

So I am expecting that myAPIregister(Event) will provide a pointer to the
Event delegate. When the callback event occurs it should simply display the
MessageBox with the text. The parameter in myAPIregister(parameter) is
expecting a pointer to the callback handler.

I have done a lot of reading about delegates over the past few days to get
this far but I appear to be missing something. The Compiler complains that
the argument is invalid.

Thanks for any help and guidance.
 
Back
Top