Win32 API pointer question
-
Guys, I've seen this type of question before but my search did not turn up anything. Here is the C# API definition: [DllImport"user32.dll",EntryPoint="SendMessage",ExactSpelling=false,SetLastError=true)] public static extern int SendMessage(int hwnd,int wMsg,int wParam,int lParam); All the places where I call this function I use simple integers. But now I have a spot where I want to call this function and for lParam I need to pass a pointer to a null-terminated string. How do I do that?
-
Guys, I've seen this type of question before but my search did not turn up anything. Here is the C# API definition: [DllImport"user32.dll",EntryPoint="SendMessage",ExactSpelling=false,SetLastError=true)] public static extern int SendMessage(int hwnd,int wMsg,int wParam,int lParam); All the places where I call this function I use simple integers. But now I have a spot where I want to call this function and for lParam I need to pass a pointer to a null-terminated string. How do I do that?
This link should give you some valuable info: http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20030424NETFXSK/manifest.xml[^] John
"We want to be alone when we hear too many words and we feel alone when it has been a while since anyone has spoken to us." Paul David Tripp -- War of Words -
Guys, I've seen this type of question before but my search did not turn up anything. Here is the C# API definition: [DllImport"user32.dll",EntryPoint="SendMessage",ExactSpelling=false,SetLastError=true)] public static extern int SendMessage(int hwnd,int wMsg,int wParam,int lParam); All the places where I call this function I use simple integers. But now I have a spot where I want to call this function and for lParam I need to pass a pointer to a null-terminated string. How do I do that?
For recieving a string:
//put your string in the szText member of this struct
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct STRINGBUFFER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=512)]
public string szText;
}[DllImport"user32.dll",EntryPoint="SendMessage", ExactSpelling=false,
SetLastError=true,CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd,int wMsg,int wParam,out STRINGBUFFER lParam);For passing a string out:
[DllImport"user32.dll",EntryPoint="SendMessage",ExactSpelling=false,
SetLastError=true, CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd,int wMsg,int wParam,text lParam);"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi -
For recieving a string:
//put your string in the szText member of this struct
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct STRINGBUFFER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=512)]
public string szText;
}[DllImport"user32.dll",EntryPoint="SendMessage", ExactSpelling=false,
SetLastError=true,CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd,int wMsg,int wParam,out STRINGBUFFER lParam);For passing a string out:
[DllImport"user32.dll",EntryPoint="SendMessage",ExactSpelling=false,
SetLastError=true, CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd,int wMsg,int wParam,text lParam);"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma GandhiThanks J, I was sort of looking for a way to do it without having to change the import. I'm able to do what I need by using "..., String lParam)". But I am wondering if there was some way I could still use the "..., int lParam)" definition and do something special in the calling statment.
-
Thanks J, I was sort of looking for a way to do it without having to change the import. I'm able to do what I need by using "..., String lParam)". But I am wondering if there was some way I could still use the "..., int lParam)" definition and do something special in the calling statment.
albean wrote: I was sort of looking for a way to do it without having to change the import. Well, I don't know of a way, short of using pointers (which I think is more time-consuming). But you can have multiple defs for the same API with the same name. All you have to do is copy the original SendMessage each time and change the lParam parameter's data type.
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi