Click here to Skip to main content
15,926,959 members
Home / Discussions / C#
   

C#

 
AnswerRe: retrieve text from a currently selected cell of a datagrid Pin
Seishin#16-Jan-07 22:59
Seishin#16-Jan-07 22:59 
GeneralRe: retrieve text from a currently selected cell of a datagrid Pin
Saira Tanwir16-Jan-07 23:04
Saira Tanwir16-Jan-07 23:04 
AnswerRe: Remote Computer Pin
JacquesDP17-Jan-07 0:18
JacquesDP17-Jan-07 0:18 
QuestionHow to Get Current Row Value on DataGirdView Pin
dataminers16-Jan-07 22:20
dataminers16-Jan-07 22:20 
AnswerRe: How to Get Current Row Value on DataGirdView Pin
Seishin#16-Jan-07 22:22
Seishin#16-Jan-07 22:22 
GeneralRe: How to Get Current Row Value on DataGirdView Pin
dataminers16-Jan-07 22:38
dataminers16-Jan-07 22:38 
Questionhow to use SendMassege Pin
freespeed16-Jan-07 21:01
freespeed16-Jan-07 21:01 
AnswerRe: how to use SendMassege Pin
Luc Pattyn17-Jan-07 0:43
sitebuilderLuc Pattyn17-Jan-07 0:43 
You did not explain much about what it is you want to achieve.
So I am guessing you want to send a user-defined message to a window
that does not belong to the same process ?

if so, it is a rather simple PInvoke exercise as long as no pointers need
to be passed.

When the message is to another process AND it needs a pointer to some data,
then it becomes trickym since that pointer now must make sense in the
destination process, which has a completely separate address space.

You can obtain a piece of memory and address space in some other
process by calling the Win32 function VirtualAllocEx.

So that is where you must start.
Then the caller must write into that special memory, using WriteProcessMemory
If the memory is used to return data, it must be read with ReadProcessMemory

And the last thing you should do, once neither process needs
the special memeory any more, is use VirtualFreeEx to release that memory again.

Here are some code snippets that are part of the solution:

[DllImport("kernel32.dll", CallingConvention=CallingConvention.StdCall)] 
private static extern IntPtr VirtualAllocEx(int hProcess, int address,
	int size, uint allocationType, uint protection);
 
[DllImport("kernel32.dll", CallingConvention=CallingConvention.StdCall)] 
private static extern bool VirtualFreeEx(int hProcess, IntPtr address,
	int size, uint freeType);
 
[DllImport("kernel32.dll", CallingConvention=CallingConvention.StdCall)] 
private static extern bool WriteProcessMemory(int hProcess,
	IntPtr otherAddress, IntPtr localAddress, int size,
	ref uint bytesWritten);
 
[DllImport("kernel32.dll", CallingConvention=CallingConvention.StdCall)] 
private static extern bool ReadProcessMemory(int hProcess,
	IntPtr otherAddress, IntPtr localAddress, int size,
	ref uint bytesRead);
 
[DllImport("kernel32.dll", CallingConvention=CallingConvention.StdCall)] 
private static extern bool ReadProcessMemory(int hProcess,
	IntPtr otherAddress, StringBuilder localAddress, int size,
	ref uint bytesRead);
 
/// <summary>
/// Reads an object's data from the process memory at ptr.
/// </summary>
/// <param name="obj"></param>
/// <param name="ptr"></param>
/// <param name="size"></param>
public void Read(object obj, int size, IntPtr ptr) {
	uint bytesRead=0;
	GCHandle handle=GCHandle.Alloc(obj, GCHandleType.Pinned);
	IntPtr objPtr=handle.AddrOfPinnedObject();
	if (!ReadProcessMemory(hProcess, ptr, objPtr, size, ref bytesRead)) {
		int err=GetLastError();
		env.error("Read failed; err="+err+"  bytesRead="+bytesRead);
	}
	handle.Free();
}
 
/// <summary>
/// Reads a string from the process memory at ptr.
/// </summary>
/// <param name="size"></param>
/// <param name="ptr"></param>
public string ReadString(int size, IntPtr ptr) {
	StringBuilder sb=new StringBuilder(size);
	uint bytesRead=0;
	if (!ReadProcessMemory(hProcess, ptr, sb, size, ref bytesRead))
		env.error("Read failed; bytesRead="+bytesRead);
	return sb.ToString();
}
 
/// <summary>
/// Write an object's data to the process memory at ptr.
/// </summary>
/// <param name="obj"></param>
/// <param name="ptr"></param>
/// <param name="size"></param>
public void Write(object obj, int size, IntPtr ptr) {
	uint bytesWritten=0;
	GCHandle handle=GCHandle.Alloc(obj, GCHandleType.Pinned);
	IntPtr objPtr=handle.AddrOfPinnedObject();
	if (!WriteProcessMemory(hProcess, ptr, objPtr, size, ref bytesWritten))
		env.error("Write failed; bytesWritten="+bytesWritten);
	handle.Free();
}


Dont mind the env.log and env.error, those are just log functions. You can substitute
your own if required. And you might want to throw an exception when an error occurs.

Hope this helps.

Smile | :)







Luc Pattyn

AnswerRe: how to use SendMassege Pin
Luc Pattyn17-Jan-07 1:21
sitebuilderLuc Pattyn17-Jan-07 1:21 
QuestionVSS Database Create directory. Pin
Elephant12316-Jan-07 20:46
Elephant12316-Jan-07 20:46 
AnswerRe: VSS Database Create directory. Pin
ednrgc17-Jan-07 4:45
ednrgc17-Jan-07 4:45 
QuestionRemoting compatibility question Pin
schaf1316-Jan-07 20:29
schaf1316-Jan-07 20:29 
QuestionAlter color of tabpage Pin
livez16-Jan-07 20:26
livez16-Jan-07 20:26 
AnswerRe: Alter color of tabpage Pin
il_masacratore16-Jan-07 21:33
il_masacratore16-Jan-07 21:33 
AnswerRe: Alter color of tabpage Pin
H. Neville III16-Jan-07 23:16
H. Neville III16-Jan-07 23:16 
AnswerRe: Alter color of tabpage Pin
Vinay Dornala16-Jan-07 23:48
Vinay Dornala16-Jan-07 23:48 
GeneralRe: Alter color of tabpage Pin
livez17-Jan-07 0:48
livez17-Jan-07 0:48 
QuestionEvents Pin
Monin D.16-Jan-07 20:20
Monin D.16-Jan-07 20:20 
AnswerRe: Events Pin
Christian Graus16-Jan-07 20:51
protectorChristian Graus16-Jan-07 20:51 
GeneralRe: Events [modified] Pin
Monin D.16-Jan-07 21:21
Monin D.16-Jan-07 21:21 
AnswerRe: Events Pin
S. Senthil Kumar17-Jan-07 0:35
S. Senthil Kumar17-Jan-07 0:35 
GeneralRe: Events Pin
Monin D.17-Jan-07 7:00
Monin D.17-Jan-07 7:00 
QuestionCross threading issue [modified] Pin
JacquesDP16-Jan-07 20:08
JacquesDP16-Jan-07 20:08 
AnswerRe: Cross threading issue Pin
Christian Graus16-Jan-07 20:53
protectorChristian Graus16-Jan-07 20:53 
GeneralRe: Cross threading issue Pin
S. Senthil Kumar16-Jan-07 21:39
S. Senthil Kumar16-Jan-07 21:39 

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.