473,714 Members | 2,562 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 7426
Hi, Christian

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

HTH
Alex

"Christian Westerlund" <ch************ ******@comhem.s e> wrote in message
news:ed******** ******@TK2MSFTN GP10.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(Cal lBack 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(Int Ptr 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.s e> wrote in message
news:ed******** ******@TK2MSFTN GP10.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.s e> wrote in message
news:O9******** ******@TK2MSFTN GP10.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(Cal lBack 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(Int Ptr 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.s e> wrote in message
news:ed******** ******@TK2MSFTN GP10.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.s e> wrote in message
news:O9******** ******@TK2MSFTN GP10.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(Cal lBack 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(Int Ptr 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.s e> wrote in message
news:ed***** *********@TK2MS FTNGP10.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 DllImportAttrib ute like CallingConventi on 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.s e> wrote in message
news:et******** ******@TK2MSFTN GP09.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.s e> wrote in message
news:O9******** ******@TK2MSFTN GP10.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(Cal lBack 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(Int Ptr 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.s e> wrote in messagenews:ed***** *********@TK2MS FTNGP10.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_S tream_SetEndCal lback(_currentS ong, callback ,(IntPtr) 0);
}

[DllImport ("fmod.dll")]
public static extern bool FSOUND_Stream_S etEndCallback(I ntPtr
FSOUND_STREAM_s tream,STREAMCAL LBACK
FSOUND_STREAMCA LLBACK_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 DllImportAttrib ute like CallingConventi on 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.s e> wrote in message
news:et******** ******@TK2MSFTN GP09.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.s e> wrote in message
news:O9***** *********@TK2MS FTNGP10.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(Cal lBack 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(Int Ptr 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
>
>"Christi an Westerlund" <ch************ ******@comhem.s e> wrote in
message
news:ed*** ***********@TK2 MSFTNGP10.phx.g bl...
>
>
>
>>Hi!
>>
>>I'm trying to use P/Invoke and a Method which takes an IntPtr where I
am
suppose d 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.GetLast Win32Error 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.s e> wrote in message
news:OQ******** ******@TK2MSFTN GP11.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_S tream_SetEndCal lback(_currentS ong, callback ,(IntPtr) 0);
}

[DllImport ("fmod.dll")]
public static extern bool FSOUND_Stream_S etEndCallback(I ntPtr
FSOUND_STREAM_s tream,STREAMCAL LBACK
FSOUND_STREAMCA LLBACK_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 DllImportAttrib ute like CallingConventi on 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.s e> wrote in message
news:et******** ******@TK2MSFTN GP09.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.s e> wrote in messagenews:O9***** *********@TK2MS FTNGP10.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(Cal lBack 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(Int Ptr 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
>>
>>"Christi an Westerlund" <ch************ ******@comhem.s e> wrote in


message
>>news:ed*** ***********@TK2 MSFTNGP10.phx.g bl...
>>
>>
>>
>>>Hi!
>>>
>>>I'm trying to use P/Invoke and a Method which takes an IntPtr where
I
am
>>>suppose d 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 GetLastWin32Err or? How can I call GetLastWin32Err or when I
don't know when the error occurs?

The declaration is:
signed char F_CALLBACKAPI FSOUND_STREAMCA LLBACK(FSOUND_S TREAM
*stream,void *buff,int len,void *userdata);

/Christian
AlexS wrote:
Use Marshal.GetLast Win32Error 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.s e> wrote in message
news:OQ******** ******@TK2MSFTN GP11.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:
{
STREAMCALLBAC K callback = new STREAMCALLBACK( EndSongCallback );
Stream.FSOUND _Stream_SetEndC allback(_curren tSong, callback ,(IntPtr) 0);
}

[DllImport ("fmod.dll")]
public static extern bool FSOUND_Stream_S etEndCallback(I ntPtr
FSOUND_STREAM _stream,STREAMC ALLBACK
FSOUND_STREAM CALLBACK_callba ck,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 DllImportAttrib ute like CallingConventi on 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.s e> wrote in message
news:et***** *********@TK2MS FTNGP09.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
>
>"Christi an Westerlund" <ch************ ******@comhem.s e> wrote in
message
news:O9*** ***********@TK2 MSFTNGP10.phx.g bl...
>
>
>
>>Hi!
>>
>>Well, in that sample and as in any sample I have seen the P/Invoke
>>functio n takes a delegate object like this:
>>public static extern int EnumWindows(Cal lBack 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(Int Ptr 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
>>>
>>>"Christi an Westerlund" <ch************ ******@comhem.s e> wrote in

message
>>>news:ed* *************@T K2MSFTNGP10.phx .gbl...
>>>
>>>
>>>
>>>
>>>>Hi!
>>>>
>>>>I'm trying to use P/Invoke and a Method which takes an IntPtr where
I
am
>>>>suppose d 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

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

Similar topics

2
3765
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
1890
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){ ... } When I prepare the parameters for the above function, I create and integer array and give it some values
0
988
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 HandleDestroyed event, to signal that I shouldn't call Invoke anymore but I still get an ObjectDisposedException. So what signals when Invoke shouldn't be called?
14
7348
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 (and types), and with out parmeters. What I want to do is to support each method call with exception (http 404, soap exception, and other types of exceptions) and wrap it with try & catch (a lots of catch ;-)
3
2240
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 ShowRecvdMessage))
1
1925
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 web method through another
2
3044
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 pointer through the OnCreate method... public void OnCreate(IApplication pApp)... The plug-in that I'm working on also allows sub-plug-ins. These sub-plug-ins are simple DLLs that provide methods that are accessed via Reflection.
4
6812
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 signature of method to pass a new parameter, I am setting the custom attribute of a given method and then accessing the attribute from method. Since attribute value is decided at runtime I want to change the attribute of particular method at runtime....
7
5423
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 the method directly, but I would still like to know if this is possible, e.g. something like the below ta
4
2864
by: san1907 | last post by:
Assembly assembly = Assembly.LoadFrom(@"D:\Work\ex\project5stepwrapper.dll"); foreach (Type Cls in assembly.GetTypes()) { if (Cls.IsInterface) continue; object instance = Activator.CreateInstance(Cls); foreach (MethodInfo Method in Cls.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) ...
0
8704
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9071
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7946
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6627
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4462
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4715
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2105
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.