IntPtr and method P/Invoke

  • Thread starter Christian Westerlund
  • Start date
C

Christian Westerlund

Hi!

I'm trying to use P/Invoke and a Method which takes an IntPtr where I am
supposed to put an address to a method which the native method will use
to communicate back to me. How do I convert a method to an IntPtr?

/ Christian
 
C

Christian Westerlund

Hi!

Well, in that sample and as in any sample I have seen the P/Invoke
function takes a delegate object like this:
public static extern int EnumWindows(CallBack x, int y);
Then it is not a problem, you create a delegate object and send it but I
have a function that is more like this:
public static extern int EnumWindows(IntPtr callback, int y)

If I send a delegate object as the first parameter I get an error and I
have no idea how to convert a delegate to an IntPtr.

/Christian
 
A

AlexS

Hi, Christian

I am not sure I understand your issue. You can declare function as in the
sample instead of with IntPtr and test if it works.

HTH
Alex
 
C

Christian Westerlund

Hi!

Thanks for the help but there is no information about the function I am
using there.
Is it even possible to send an adress to a method as an int?

/Christian
 
C

Christian Westerlund

Hi!

Well, the reason I don't want to change the P/Invoke declaration is that
I add a reference to an assembly with this function declaration, that
someone else has written so I don't want to alter the code because then
I'll have to do it all over when a new version is released. I thought
that there was an easy way to accomplish this. I can't find the guy who
has written it, otherwise I would have talked to him about it.

When I change the declaration and replace the IntPtr to my delgate type
the program crashes when the callback is supposed to happen.

/ Christian
 
A

AlexS

Christian

what exactly is crash message or code? If you have available original
definition of function and callback (C++ or whatever was used), possibly
posting it would help to find the reason.
You might need to use DllImportAttribute like CallingConvention too. Now it
is not clear if it crashes because of wrong callback definition, parameter
passing or something else.

HTH
Alex
 
C

Christian Westerlund

Hi!

The message I get is "Unhandled WIN 32 exception".

The delegate :
public delegate byte STREAMCALLBACK(IntPtr stream, IntPtr buff, int len,
int param);

my callback method:
public byte EndSongCallback(IntPtr stream, IntPtr buff, int len, int
param){}

the method which calls the win32 function:
{
STREAMCALLBACK callback = new STREAMCALLBACK(EndSongCallback);
Stream.FSOUND_Stream_SetEndCallback(_currentSong, callback ,(IntPtr) 0);
}

[DllImport ("fmod.dll")]
public static extern bool FSOUND_Stream_SetEndCallback(IntPtr
FSOUND_STREAM_stream,STREAMCALLBACK
FSOUND_STREAMCALLBACK_callback,IntPtr userdata);
 
A

AlexS

Use Marshal.GetLastWin32Error to get more details on the error.
You might have also issue with return type byte for callback.
What is original non-.Net declaration?

Rgds
Alex

Christian Westerlund said:
Hi!

The message I get is "Unhandled WIN 32 exception".

The delegate :
public delegate byte STREAMCALLBACK(IntPtr stream, IntPtr buff, int len,
int param);

my callback method:
public byte EndSongCallback(IntPtr stream, IntPtr buff, int len, int
param){}

the method which calls the win32 function:
{
STREAMCALLBACK callback = new STREAMCALLBACK(EndSongCallback);
Stream.FSOUND_Stream_SetEndCallback(_currentSong, callback ,(IntPtr) 0);
}

[DllImport ("fmod.dll")]
public static extern bool FSOUND_Stream_SetEndCallback(IntPtr
FSOUND_STREAM_stream,STREAMCALLBACK
FSOUND_STREAMCALLBACK_callback,IntPtr userdata);
Christian

what exactly is crash message or code? If you have available original
definition of function and callback (C++ or whatever was used), possibly
posting it would help to find the reason.
You might need to use DllImportAttribute like CallingConvention too. Now it
is not clear if it crashes because of wrong callback definition, parameter
passing or something else.

HTH
Alex


http://msdn.microsoft.com/library/d...cpguide/html/cpconusingcallbackfunctions.asp?
I

am
 
C

Christian Westerlund

Hi!

How do I use GetLastWin32Error? How can I call GetLastWin32Error when I
don't know when the error occurs?

The declaration is:
signed char F_CALLBACKAPI FSOUND_STREAMCALLBACK(FSOUND_STREAM
*stream,void *buff,int len,void *userdata);

/Christian

Use Marshal.GetLastWin32Error to get more details on the error.
You might have also issue with return type byte for callback.
What is original non-.Net declaration?

Rgds
Alex

Hi!

The message I get is "Unhandled WIN 32 exception".

The delegate :
public delegate byte STREAMCALLBACK(IntPtr stream, IntPtr buff, int len,
int param);

my callback method:
public byte EndSongCallback(IntPtr stream, IntPtr buff, int len, int
param){}

the method which calls the win32 function:
{
STREAMCALLBACK callback = new STREAMCALLBACK(EndSongCallback);
Stream.FSOUND_Stream_SetEndCallback(_currentSong, callback ,(IntPtr) 0);
}

[DllImport ("fmod.dll")]
public static extern bool FSOUND_Stream_SetEndCallback(IntPtr
FSOUND_STREAM_stream,STREAMCALLBACK
FSOUND_STREAMCALLBACK_callback,IntPtr userdata);
Christian

what exactly is crash message or code? If you have available original
definition of function and callback (C++ or whatever was used), possibly
posting it would help to find the reason.
You might need to use DllImportAttribute like CallingConvention too. Now
it
is not clear if it crashes because of wrong callback definition,
parameter
passing or something else.

HTH
Alex



Hi!

Well, the reason I don't want to change the P/Invoke declaration is that
I add a reference to an assembly with this function declaration, that
someone else has written so I don't want to alter the code because then
I'll have to do it all over when a new version is released. I thought
that there was an easy way to accomplish this. I can't find the guy who
has written it, otherwise I would have talked to him about it.

When I change the declaration and replace the IntPtr to my delgate type
the program crashes when the callback is supposed to happen.

/ Christian

AlexS wrote:



Hi, Christian

I am not sure I understand your issue. You can declare function as in

the


sample instead of with IntPtr and test if it works.

HTH
Alex

"Christian Westerlund" <[email protected]> wrote in
message
Hi!

Well, in that sample and as in any sample I have seen the P/Invoke
function takes a delegate object like this:
public static extern int EnumWindows(CallBack x, int y);
Then it is not a problem, you create a delegate object and send it but
I
have a function that is more like this:
public static extern int EnumWindows(IntPtr callback, int y)

If I send a delegate object as the first parameter I get an error and
I
have no idea how to convert a delegate to an IntPtr.

/Christian

AlexS wrote:



Hi, Christian

Did you check sample at
http://msdn.microsoft.com/library/d...cpguide/html/cpconusingcallbackfunctions.asp?
HTH
Alex


message






Hi!

I'm trying to use P/Invoke and a Method which takes an IntPtr where
I
am


supposed to put an address to a method which the native method will

use


to communicate back to me. How do I convert a method to an IntPtr?

/ Christian
 
A

AlexS

You might need to check following points:
- what is F_CALLBACKAPI - is it winapi or something else
- do you need to use MarshalAs(UnmanagedType.AsAny) attribute for userdata
parameter or ref if you get back something
- use of HandleRef or GCHandle to prevent garbage collection for delegate
and passed in/out data. Check samples for both to see how and when to use
- use of cordbg to get to exception details
- declared userdata as int and not as IntPtr

HTH
Alex

Christian Westerlund said:
Hi!

How do I use GetLastWin32Error? How can I call GetLastWin32Error when I
don't know when the error occurs?

The declaration is:
signed char F_CALLBACKAPI FSOUND_STREAMCALLBACK(FSOUND_STREAM
*stream,void *buff,int len,void *userdata);

/Christian

Use Marshal.GetLastWin32Error to get more details on the error.
You might have also issue with return type byte for callback.
What is original non-.Net declaration?

Rgds
Alex

Hi!

The message I get is "Unhandled WIN 32 exception".

The delegate :
public delegate byte STREAMCALLBACK(IntPtr stream, IntPtr buff, int len,
int param);

my callback method:
public byte EndSongCallback(IntPtr stream, IntPtr buff, int len, int
param){}

the method which calls the win32 function:
{
STREAMCALLBACK callback = new STREAMCALLBACK(EndSongCallback);
Stream.FSOUND_Stream_SetEndCallback(_currentSong, callback ,(IntPtr) 0);
}

[DllImport ("fmod.dll")]
public static extern bool FSOUND_Stream_SetEndCallback(IntPtr
FSOUND_STREAM_stream,STREAMCALLBACK
FSOUND_STREAMCALLBACK_callback,IntPtr userdata);

AlexS wrote:

Christian

what exactly is crash message or code? If you have available original
definition of function and callback (C++ or whatever was used), possibly
posting it would help to find the reason.
You might need to use DllImportAttribute like CallingConvention too.
Now

it
is not clear if it crashes because of wrong callback definition,
parameter

passing or something else.

HTH
Alex



Hi!

Well, the reason I don't want to change the P/Invoke declaration is that
I add a reference to an assembly with this function declaration, that
someone else has written so I don't want to alter the code because then
I'll have to do it all over when a new version is released. I thought
that there was an easy way to accomplish this. I can't find the guy who
has written it, otherwise I would have talked to him about it.

When I change the declaration and replace the IntPtr to my delgate type
the program crashes when the callback is supposed to happen.

/ Christian

AlexS wrote:



Hi, Christian

I am not sure I understand your issue. You can declare function as in

the


sample instead of with IntPtr and test if it works.

HTH
Alex

message




Hi!

Well, in that sample and as in any sample I have seen the P/Invoke
function takes a delegate object like this:
public static extern int EnumWindows(CallBack x, int y);
Then it is not a problem, you create a delegate object and send it
but

I
have a function that is more like this:
public static extern int EnumWindows(IntPtr callback, int y)

If I send a delegate object as the first parameter I get an error
and

I
have no idea how to convert a delegate to an IntPtr.

/Christian

AlexS wrote:



Hi, Christian

Did you check sample at
http://msdn.microsoft.com/library/d...cpguide/html/cpconusingcallbackfunctions.asp?
HTH
Alex


message






Hi!

I'm trying to use P/Invoke and a Method which takes an IntPtr
where

I
am


supposed to put an address to a method which the native method will

use


to communicate back to me. How do I convert a method to an IntPtr?

/ Christian
 
B

BMermuys

Hi,
inline

Christian Westerlund said:
Hi!

The message I get is "Unhandled WIN 32 exception".

The delegate :
public delegate byte STREAMCALLBACK(IntPtr stream, IntPtr buff, int len,
int param);

my callback method:
public byte EndSongCallback(IntPtr stream, IntPtr buff, int len, int
param){}

the method which calls the win32 function:
{
STREAMCALLBACK callback = new STREAMCALLBACK(EndSongCallback);
Stream.FSOUND_Stream_SetEndCallback(_currentSong, callback ,(IntPtr) 0);
}

Since "callback" is local it will get out of scope after the function
finishes. Passing a delegate to unmanaged code doesn't keep it alive.
You must keep a managed reference. (e.g. put the callback variable as a
class field )

HTH,
greetings

[DllImport ("fmod.dll")]
public static extern bool FSOUND_Stream_SetEndCallback(IntPtr
FSOUND_STREAM_stream,STREAMCALLBACK
FSOUND_STREAMCALLBACK_callback,IntPtr userdata);
Christian

what exactly is crash message or code? If you have available original
definition of function and callback (C++ or whatever was used), possibly
posting it would help to find the reason.
You might need to use DllImportAttribute like CallingConvention too. Now it
is not clear if it crashes because of wrong callback definition, parameter
passing or something else.

HTH
Alex


http://msdn.microsoft.com/library/d...cpguide/html/cpconusingcallbackfunctions.asp?
I

am
 
C

Christian Westerlund

Hi!

Thanks, you are right. I changed so that the callback is a private class
member and then it worked!!
But I still had to change from this:

[DllImport ("fmod.dll")]
public static extern bool FSOUND_Stream_SetEndCallback(IntPtr
FSOUND_STREAM_stream,IntPtr FSOUND_STREAMCALLBACK_callback,IntPtr userdata);

to this:

[DllImport ("fmod.dll")]
public static extern bool FSOUND_Stream_SetEndCallback(IntPtr
FSOUND_STREAM_stream,STREAMCALLBACK
FSOUND_STREAMCALLBACK_callback,IntPtr userdata);

Anyone know how to use the original function declaration?

/Christian
Hi,
inline

Hi!

The message I get is "Unhandled WIN 32 exception".

The delegate :
public delegate byte STREAMCALLBACK(IntPtr stream, IntPtr buff, int len,
int param);

my callback method:
public byte EndSongCallback(IntPtr stream, IntPtr buff, int len, int
param){}

the method which calls the win32 function:
{
STREAMCALLBACK callback = new STREAMCALLBACK(EndSongCallback);
Stream.FSOUND_Stream_SetEndCallback(_currentSong, callback ,(IntPtr) 0);
}


Since "callback" is local it will get out of scope after the function
finishes. Passing a delegate to unmanaged code doesn't keep it alive.
You must keep a managed reference. (e.g. put the callback variable as a
class field )

HTH,
greetings


[DllImport ("fmod.dll")]
public static extern bool FSOUND_Stream_SetEndCallback(IntPtr
FSOUND_STREAM_stream,STREAMCALLBACK
FSOUND_STREAMCALLBACK_callback,IntPtr userdata);
Christian

what exactly is crash message or code? If you have available original
definition of function and callback (C++ or whatever was used), possibly
posting it would help to find the reason.
You might need to use DllImportAttribute like CallingConvention too. Now
it
is not clear if it crashes because of wrong callback definition,
parameter
passing or something else.

HTH
Alex



Hi!

Well, the reason I don't want to change the P/Invoke declaration is that
I add a reference to an assembly with this function declaration, that
someone else has written so I don't want to alter the code because then
I'll have to do it all over when a new version is released. I thought
that there was an easy way to accomplish this. I can't find the guy who
has written it, otherwise I would have talked to him about it.

When I change the declaration and replace the IntPtr to my delgate type
the program crashes when the callback is supposed to happen.

/ Christian

AlexS wrote:



Hi, Christian

I am not sure I understand your issue. You can declare function as in

the


sample instead of with IntPtr and test if it works.

HTH
Alex

"Christian Westerlund" <[email protected]> wrote in
message
Hi!

Well, in that sample and as in any sample I have seen the P/Invoke
function takes a delegate object like this:
public static extern int EnumWindows(CallBack x, int y);
Then it is not a problem, you create a delegate object and send it but
I
have a function that is more like this:
public static extern int EnumWindows(IntPtr callback, int y)

If I send a delegate object as the first parameter I get an error and
I
have no idea how to convert a delegate to an IntPtr.

/Christian

AlexS wrote:



Hi, Christian

Did you check sample at
http://msdn.microsoft.com/library/d...cpguide/html/cpconusingcallbackfunctions.asp?
HTH
Alex


message






Hi!

I'm trying to use P/Invoke and a Method which takes an IntPtr where
I
am


supposed to put an address to a method which the native method will

use


to communicate back to me. How do I convert a method to an IntPtr?

/ Christian
 
P

Philippe PUAUD

Here is how I usually do this

// type of callback
delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

// this is the secret
[StructLayout(LayoutKind.Sequential)]
struct CallbackPtrStruct
{
[MarshalAs(UnmanagedType.FunctionPtr)] public EnumWindowsProc
Callback;
}

// Get a Pointer to our function
// Suppose your member function is Callback(IntPtr hWnd, IntPtr lParam);
CallbackPtrStruct ptrStruct = new CallbackPtrStruct();
ptrStruct.Callback = new EnumWindowsProc(this.Callback);
IntPtr StructPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(ptrStruct));
Marshal.StructureToPtr(ptrStruct,StructPtr,false);
IntPtr FuncPtr = Marshal.ReadIntPtr(StructPtr); // now we have a pointer
Marshal.FreeCoTaskMem(StructPtr);
// voila!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top