Click here to Skip to main content
15,906,463 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to suspend a process in c# Pin
Alvaro Mendez29-Oct-04 5:20
Alvaro Mendez29-Oct-04 5:20 
Generaladdress of an object reference variable in C# Pin
Diana Fernandez28-Oct-04 20:16
Diana Fernandez28-Oct-04 20:16 
Generalexception generated from graphics.FromImage Pin
cchere28-Oct-04 18:43
cchere28-Oct-04 18:43 
GeneralRe: exception generated from graphics.FromImage Pin
Maqsood Ahmed28-Oct-04 23:26
Maqsood Ahmed28-Oct-04 23:26 
GeneralRe: exception generated from graphics.FromImage Pin
cchere29-Oct-04 4:19
cchere29-Oct-04 4:19 
Questionhow to make a bmp image editor with grid Pin
tom_dx28-Oct-04 16:42
tom_dx28-Oct-04 16:42 
Generalp/Invoke using sendmessage in C# Pin
ting66828-Oct-04 16:32
ting66828-Oct-04 16:32 
GeneralRe: p/Invoke using sendmessage in C# Pin
Heath Stewart30-Oct-04 2:31
protectorHeath Stewart30-Oct-04 2:31 
MarshalAs isn't a keyword - it's an attribute, as in MarshalAsAttribute. What you have isn't even correct on the full .NET Framework, though. Why do you set CharSet=CharSet.Auto, then try to marshal the string as an UnmanagedType.LPSTR? An LPSTR is a char*. You should marshal as an LPTSTR (TCHAR*), but setting the CharSet field to CharSet.Auto does that automatically.

The only reason overloading the SendMessage function like this would work is because by passing a TCHAR* you pass the address of the first character (rather, the first byte which is 1 character in ANSI and 2 Unicode (typically). Since both an WPARAM and LPARAM are just addresses (32 bits on a 32-bit system, and 64 bits on a 64-bit system, unless you're running under WOW64 to emulate a 32-bit processor for a 32-bit application, which is currently all that's available with .NET 1.x), the following signature should work for you:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
extern static IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, string lParam);
As you may have noticed, I've changed the signature a little more. The return value should be an IntPtr. By not defining the return value correctly you could corrupt the stack (a return value needs to be pushed back onto the stack whether or not you use it). And, as I said, both the WPARAM and LPARAM parameters are processor-dependent so you need to use an IntPtr (which is also a processor-dependent bit width). If you recompiled for 64-bit processors ever, your code would not work (you'd be trying to call the 32-bit SendMessage on the 64-bit implementation and should surely have an execution stack problem since your parameters wouldn't be correct and you'd end up passing something else (uninitialized data, etc. - definitely not your address and most likely ending up with an Access Violation (AV) exception).

PS: To pass 0 as the wParam that's now correctly defined as an IntPtr, use IntPtr.Zero.

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralRe: p/Invoke using sendmessage in C# Pin
ting66831-Oct-04 14:55
ting66831-Oct-04 14:55 
GeneralRe: p/Invoke using sendmessage in C# Pin
Heath Stewart31-Oct-04 20:11
protectorHeath Stewart31-Oct-04 20:11 
GeneralRe: p/Invoke using sendmessage in C# Pin
ting6681-Nov-04 3:45
ting6681-Nov-04 3:45 
GeneralRe: p/Invoke using sendmessage in C# Pin
Heath Stewart1-Nov-04 5:27
protectorHeath Stewart1-Nov-04 5:27 
GeneralRe: p/Invoke using sendmessage in C# Pin
ting6683-Nov-04 19:51
ting6683-Nov-04 19:51 
GeneralRe: p/Invoke using sendmessage in C# Pin
Heath Stewart4-Nov-04 4:41
protectorHeath Stewart4-Nov-04 4:41 
GeneralRe: p/Invoke using sendmessage in C# Pin
ting6684-Nov-04 14:56
ting6684-Nov-04 14:56 
GeneralRe: p/Invoke using sendmessage in C# Pin
Heath Stewart8-Nov-04 5:03
protectorHeath Stewart8-Nov-04 5:03 
GeneralSorting DataGrid from DataView Problems Pin
Rebecca Gao28-Oct-04 16:23
Rebecca Gao28-Oct-04 16:23 
GeneralRe: Sorting DataGrid from DataView Problems Pin
sreejith ss nair28-Oct-04 18:38
sreejith ss nair28-Oct-04 18:38 
GeneralRe: Sorting DataGrid from DataView Problems Pin
Rebecca Gao28-Oct-04 20:39
Rebecca Gao28-Oct-04 20:39 
GeneralRe: Sorting DataGrid from DataView Problems Pin
sreejith ss nair28-Oct-04 22:57
sreejith ss nair28-Oct-04 22:57 
GeneralRe: Sorting DataGrid from DataView Problems Pin
Rebecca Gao29-Oct-04 4:02
Rebecca Gao29-Oct-04 4:02 
Generalforms maximize and minimum Pin
webhay28-Oct-04 16:05
webhay28-Oct-04 16:05 
GeneralRe: forms maximize and minimum Pin
Heath Stewart28-Oct-04 16:16
protectorHeath Stewart28-Oct-04 16:16 
GeneralGDI+ usercontrol problem Pin
bobrad28-Oct-04 15:44
bobrad28-Oct-04 15:44 
GeneralRe: GDI+ usercontrol problem Pin
Heath Stewart28-Oct-04 16:32
protectorHeath Stewart28-Oct-04 16:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.