473,503 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IntPtr and method P/Invoke

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
Nov 16 '05 #1
13 7384
Hi, Christian

Did you check sample at
http://msdn.microsoft.com/library/de...functions.asp?

HTH
Alex

"Christian Westerlund" <ch******************@comhem.se> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #2
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/de...functions.asp?

HTH
Alex

"Christian Westerlund" <ch******************@comhem.se> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
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


Nov 16 '05 #3
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" <ch******************@comhem.se> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
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/de...functions.asp?
HTH
Alex

"Christian Westerlund" <ch******************@comhem.se> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
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


Nov 16 '05 #4
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

Danny Crowell wrote:
Christian, I don't have the anwer but I might be able help you find it. Have you checked out the www.pinvoke.net web site? Its purpose is to share P/Invoke information.

Regards,
Danny Crowell

"Christian Westerlund" wrote:

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

Nov 16 '05 #5
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" <ch******************@comhem.se> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
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/de...functions.asp?
HTH
Alex

"Christian Westerlund" <ch******************@comhem.se> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
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


Nov 16 '05 #6
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

"Christian Westerlund" <ch******************@comhem.se> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
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" <ch******************@comhem.se> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
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/de...functions.asp?
HTH
Alex

"Christian Westerlund" <ch******************@comhem.se> wrote in messagenews:ed**************@TK2MSFTNGP10.phx.gbl...
>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


Nov 16 '05 #7
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

"Christian Westerlund" <ch******************@comhem.se> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
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" <ch******************@comhem.se> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
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/de...functions.asp?
HTH
>Alex
>
>"Christian Westerlund" <ch******************@comhem.se> wrote in
message
news:ed**************@TK2MSFTNGP10.phx.gbl. ..
>
>
>
>>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
>
>
>


Nov 16 '05 #8
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" <ch******************@comhem.se> wrote in message
news:OQ**************@TK2MSFTNGP11.phx.gbl...
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

"Christian Westerlund" <ch******************@comhem.se> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
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" <ch******************@comhem.se> wrote in messagenews:O9**************@TK2MSFTNGP10.phx.gbl...
>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/de...functions.asp?
>>HTH
>>Alex
>>
>>"Christian Westerlund" <ch******************@comhem.se> wrote in


message
>>news:ed**************@TK2MSFTNGP10.phx.gbl. ..
>>
>>
>>
>>>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
>>
>>
>>


Nov 16 '05 #9
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
AlexS wrote:
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" <ch******************@comhem.se> wrote in message
news:OQ**************@TK2MSFTNGP11.phx.gbl...
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

"Christian Westerlund" <ch******************@comhem.se> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
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" <ch******************@comhem.se> wrote in
message
news:O9**************@TK2MSFTNGP10.phx.gbl. ..
>
>
>
>>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/de...functions.asp?
>HTH
>>>Alex
>>>
>>>"Christian Westerlund" <ch******************@comhem.se> wrote in

message
>>>news:ed**************@TK2MSFTNGP10.phx.gbl. ..
>>>
>>>
>>>
>>>
>>>>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
>>>
>>>
>>>


Nov 16 '05 #10
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" <ch******************@comhem.se> wrote in message
news:uA**************@TK2MSFTNGP09.phx.gbl...
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
AlexS wrote:
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" <ch******************@comhem.se> wrote in message
news:OQ**************@TK2MSFTNGP11.phx.gbl...
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), possiblyposting 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

"Christian Westerlund" <ch******************@comhem.se> wrote in
messagenews:et**************@TK2MSFTNGP09.phx.gbl...
>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" <ch******************@comhem.se> wrote in


message
>>news:O9**************@TK2MSFTNGP10.phx.gbl. ..
>>
>>
>>
>>>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/de...functions.asp?
>>>>HTH
>>>>Alex
>>>>
>>>>"Christian Westerlund" <ch******************@comhem.se> wrote in

message
>>>>news:ed**************@TK2MSFTNGP10.phx.gbl. ..
>>>>
>>>>
>>>>
>>>>
>>>>>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
>>>>
>>>>
>>>>


Nov 16 '05 #11
Hi,
inline

"Christian Westerlund" <ch******************@comhem.se> wrote in message
news:OQ**************@TK2MSFTNGP11.phx.gbl...
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);

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

"Christian Westerlund" <ch******************@comhem.se> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
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" <ch******************@comhem.se> wrote in messagenews:O9**************@TK2MSFTNGP10.phx.gbl...
>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/de...functions.asp?
>>HTH
>>Alex
>>
>>"Christian Westerlund" <ch******************@comhem.se> wrote in


message
>>news:ed**************@TK2MSFTNGP10.phx.gbl. ..
>>
>>
>>
>>>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
>>
>>
>>


Nov 16 '05 #12
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

BMermuys wrote:
Hi,
inline

"Christian Westerlund" <ch******************@comhem.se> wrote in message
news:OQ**************@TK2MSFTNGP11.phx.gbl...
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);

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

"Christian Westerlund" <ch******************@comhem.se> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
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" <ch******************@comhem.se> wrote in
message
news:O9**************@TK2MSFTNGP10.phx.gbl. ..
>
>
>
>>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/de...functions.asp?
>HTH
>>>Alex
>>>
>>>"Christian Westerlund" <ch******************@comhem.se> wrote in

message
>>>news:ed**************@TK2MSFTNGP10.phx.gbl. ..
>>>
>>>
>>>
>>>
>>>>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
>>>
>>>
>>>


Nov 16 '05 #13
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!
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #14

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

Similar topics

2
3746
by: Marcin | last post by:
Hello! Is there any method to detect parameters values passed to called method? For example: public Guid ApplicationLogin(string userName, string password, int dbId)
2
1864
by: Neil Fedin via .NET 247 | last post by:
I am writing a testing application that uses reflection to open assemblies and call methods. I have a function that looks like this... public void Method1 (int IDArray, bool SomethingElse){...
0
976
by: Stephen Lamb | last post by:
I know users are required to wait for the handle of a control to be created before calling Invoke. But what about when a control is disposed? I used a delegate, attached to my control's...
14
7332
by: stic | last post by:
Hi, I'm in a middle of writing something like 'exception handler wraper' for a set of different methodes. The case is that I have ca. 40 methods form web servicem, with different return values...
3
2225
by: m.posseth | last post by:
Hello does someone know how i can invoke a method in the underlying thread without the usage of a window handle ?? This works perfect in a form Me.Invoke(New MethodInvoker(AddressOf...
1
1900
by: A.M-SG | last post by:
Hi, I have a web service with two soap extensions enabled on it. When I run the default IIS method invoke page, the invoke button bypasses all my soap extensions. But when I call the...
2
3016
by: John Lutz | last post by:
Our app is mainly COM based. We allow .NET plug-ins. When a plug-in is installed, it receives a COM pointer to the app. The plug-in implements our IExtensionImpl interface and gets the app...
4
6803
by: =?Utf-8?B?QWJoaQ==?= | last post by:
I am using Reflection to invoke methods dynamically. I have got a special requirement where I need to pass a value to method by setting the custom method attribute. As I cannot change the...
7
5395
by: codefragment | last post by:
Hi I want to use reflection to call a method which accepts "params string" as an argument I've searched this group and found a better idea than invoke, just cast to an interface and then call...
4
2855
by: san1907 | last post by:
Assembly assembly = Assembly.LoadFrom(@"D:\Work\ex\project5stepwrapper.dll"); foreach (Type Cls in assembly.GetTypes()) { if (Cls.IsInterface) ...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7074
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
7273
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
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6982
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
7451
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...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.