Click here to Skip to main content
15,895,709 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to tell GUI to update controls from another thread? Pin
K_S4-Sep-06 3:48
K_S4-Sep-06 3:48 
GeneralRe: How to tell GUI to update controls from another thread? Pin
ovidiucucu4-Sep-06 4:25
ovidiucucu4-Sep-06 4:25 
AnswerRe: How to tell GUI to update controls from another thread? Pin
K_S4-Sep-06 21:41
K_S4-Sep-06 21:41 
AnswerRe: How to tell GUI to update controls from another thread? Pin
Viorel.4-Sep-06 5:28
Viorel.4-Sep-06 5:28 
AnswerRe: How to tell GUI to update controls from another thread? Pin
cmk4-Sep-06 13:54
cmk4-Sep-06 13:54 
QuestionHow I can get control's id property via api Pin
Marco22504-Sep-06 3:27
Marco22504-Sep-06 3:27 
QuestionRe: How I can get control's id property via api Pin
Programm3r4-Sep-06 4:08
Programm3r4-Sep-06 4:08 
AnswerRe: How I can get control's id property via api [modified] Pin
Marco22504-Sep-06 4:14
Marco22504-Sep-06 4:14 
first, thank you for your reply.

Sorry, because I've a bad english. But trying to do my best. Smile | :)

Okay, suppose I have a VB6 app. I put a listview on my form and name it lvwProducts. Now, I compile and run this simple app.

Now, in another app (a C# .NET 2005 app) I want do get the name of that listview, i.e., the name of the listview that is on another process (my vb6 app). First, I got the handle. with the listview handle I want to get its name.

get it?

Ah, I've downloaded a code from http://blogs.msdn.com/brianmcm/ but it only works if the listview is on an .NET process. but, in my case, the other process was written in vb6.

to use that code I do something like that:
string controlName = GetWinFormsId.WinFormsUtilities.GetWinFormsId(hWnd) // where hWnd is the handle of my listview.

the code is here (sorry, I don't know how to format the code here):

**********************
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Text;

namespace GetWinFormsId
{
///
/// Summary description for WinFormsUtilities.
///

public class WinFormsUtilities
{
private static int GetControlNameMessage = 0;

static WinFormsUtilities()
{
GetControlNameMessage = NativeMethods.RegisterWindowMessage("WM_GETCONTROLNAME");
}

public static string GetWinFormsId(IntPtr hWnd)
{
return XProcGetControlName(hWnd, GetControlNameMessage);
}

protected static string XProcGetControlName(IntPtr hwnd, int msg)
{
//define the buffer that will eventually contain the desired window's WinFormsId

byte[] bytearray = new byte[65535];

//allocate space in the target process for the buffer as shared memory
IntPtr bufferMem = IntPtr.Zero; //base address of the allocated region for the buffer
IntPtr written = IntPtr.Zero; //number of bytes written to memory
IntPtr retHandle = IntPtr.Zero;
bool retVal;


//creating and reading from a shared memory region is done differently in Win9x then in newer OSs
IntPtr processHandle = IntPtr.Zero;
IntPtr fileHandle = IntPtr.Zero;

if (!(Environment.OSVersion.Platform == PlatformID.Win32Windows))
{
try
{
uint size; //the amount of memory to be allocated
size = 65536;

processHandle = NativeMethods.OpenProcess(NativeMethods.PROCESS_VM_OPERATION | NativeMethods.PROCESS_VM_READ | NativeMethods.PROCESS_VM_WRITE, false, GetProcessIdFromHWnd(hwnd));

if (processHandle.ToInt64() == 0)
{
throw new Win32Exception();
}

bufferMem = NativeMethods.VirtualAllocEx(processHandle, IntPtr.Zero, new UIntPtr(size), NativeMethods.MEM_RESERVE | NativeMethods.MEM_COMMIT, PageProtection.ReadWrite);

if (bufferMem.ToInt64() == 0)
{
throw new Win32Exception();
}

//send message to the control's hWnd for getting the specified control name
retHandle = NativeMethods.SendMessage(hwnd, msg, new IntPtr(size), bufferMem);

//now read the TVITEM's info from the shared memory location
retVal = NativeMethods.ReadProcessMemory(processHandle, bufferMem, bytearray, new UIntPtr(size), written);
if (!retVal)
{
throw new Win32Exception();
}
}
finally
{
//free the memory that was allocated
retVal = NativeMethods.VirtualFreeEx(processHandle, bufferMem, new UIntPtr(0), NativeMethods.MEM_RELEASE);
if (!retVal)
{
throw new Win32Exception();
}
NativeMethods.CloseHandle(processHandle);
}
}
else
{
try
{
int size2; //the amount of memory to be allocated
size2 = 65536;

fileHandle = NativeMethods.CreateFileMapping(new IntPtr(NativeMethods.INVALID_HANDLE_VALUE), IntPtr.Zero, PageProtection.ReadWrite, 0, size2, null);
if (fileHandle.ToInt64() == 0)
{
throw new Win32Exception();
}
bufferMem = NativeMethods.MapViewOfFile(fileHandle, NativeMethods.FILE_MAP_ALL_ACCESS, 0, 0, new UIntPtr(0));
if (bufferMem.ToInt64() == 0)
{
throw new Win32Exception();
}
NativeMethods.MoveMemoryFromByte(bufferMem, ref bytearray[0], size2);

retHandle = NativeMethods.SendMessage(hwnd, msg, new IntPtr(size2), bufferMem);

//read the control's name from the specific shared memory for the buffer
NativeMethods.MoveMemoryToByte(ref bytearray[0], bufferMem, 1024);

}
finally
{
//unmap and close the file
NativeMethods.UnmapViewOfFile(bufferMem);
NativeMethods.CloseHandle(fileHandle);
}
}

//get the string value for the Control name
return ByteArrayToString(bytearray);

}

private static uint GetProcessIdFromHWnd(IntPtr hwnd)
{
uint pid;

NativeMethods.GetWindowThreadProcessId(hwnd, out pid);

return pid;
}

private static string ByteArrayToString(byte[] bytes)
{
if (Environment.OSVersion.Platform == PlatformID.Win32Windows)
{
// Use the Ansii encoder
return Encoding.Default.GetString(bytes).TrimEnd('0');
}
else
{
// use Unicode
return Encoding.Unicode.GetString(bytes).TrimEnd('0');
}
}
}

///
/// Summary description for NativeMethods.
///

public class NativeMethods
{

[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle,
uint dwProcessId);
[DllImport("kernel32.dll")]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,
UIntPtr dwSize, uint flAllocationType, PageProtection flProtect);
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("kernel32.dll")]
public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,
UIntPtr dwSize, uint dwFreeType);
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
public static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, uint
dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow,
UIntPtr dwNumberOfBytesToMap);
[DllImport("kernel32.dll")]
public static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateFileMapping(IntPtr hFile,
IntPtr lpFileMappingAttributes, PageProtection flProtect, int dwMaximumSizeHigh,
int dwMaximumSizeLow, string lpName);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
[Out] byte[] lpBuffer, UIntPtr nSize, IntPtr lpNumberOfBytesRead);
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)]
public static extern void MoveMemoryFromByte(IntPtr dest, ref byte src, int size);
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)]
public static extern void MoveMemoryToByte(ref byte dest, IntPtr src, int size);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int RegisterWindowMessage(string lpString);



//=========== Win95/98/ME Shared memory staff===============
public const int STANDARD_RIGHTS_REQUIRED = 0xF0000;
public const short SECTION_QUERY = 0x1;
public const short SECTION_MAP_WRITE = 0x2;
public const short SECTION_MAP_READ = 0x4;
public const short SECTION_MAP_EXECUTE = 0x8;
public const short SECTION_EXTEND_SIZE = 0x10;
public const int SECTION_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_WRITE | SECTION_MAP_READ | SECTION_MAP_EXECUTE | SECTION_EXTEND_SIZE;
public const int FILE_MAP_ALL_ACCESS = SECTION_ALL_ACCESS;

//============NT Shared memory constant======================
public const short PROCESS_VM_OPERATION = 0x8;
public const short PROCESS_VM_READ = 0x10;
public const short PROCESS_VM_WRITE = 0x20;
public const long PROCESS_ALL_ACCESS = 0x1F0FFF;
public const short MEM_COMMIT = 0x1000;
public const short MEM_RESERVE = 0x2000;
public const short MEM_DECOMMIT = 0x4000;
public const int MEM_RELEASE = 0x8000;
public const int MEM_FREE = 0x10000;
public const int MEM_PRIVATE = 0x20000;
public const int MEM_MAPPED = 0x40000;
public const int MEM_TOP_DOWN = 0x100000;


public const int INVALID_HANDLE_VALUE = -1;



}

[Flags]
public enum PageProtection : uint
{
NoAccess = 0x01,
Readonly = 0x02,
ReadWrite = 0x04,
WriteCopy = 0x08,
Execute = 0x10,
ExecuteRead = 0x20,
ExecuteReadWrite = 0x40,
ExecuteWriteCopy = 0x80,
Guard = 0x100,
NoCache = 0x200,
WriteCombine = 0x400,
}
}
**********************



best regards,
marco alves.


-- modified at 10:20 Monday 4th September, 2006
GeneralRe: How I can get control's id property via api Pin
Programm3r4-Sep-06 4:20
Programm3r4-Sep-06 4:20 
GeneralRe: How I can get control's id property via api Pin
Mila0254-Sep-06 5:13
Mila0254-Sep-06 5:13 
GeneralRe: How I can get control's id property via api Pin
Marco22504-Sep-06 5:16
Marco22504-Sep-06 5:16 
GeneralRe: How I can get control's id property via api Pin
David Crow21-Sep-06 8:43
David Crow21-Sep-06 8:43 
Questionconfuse CString::Replace() Pin
Max++4-Sep-06 3:17
Max++4-Sep-06 3:17 
AnswerRe: confuse CString::Replace() Pin
kakan4-Sep-06 3:24
professionalkakan4-Sep-06 3:24 
AnswerRe: confuse CString::Replace() Pin
Rob Caldecott4-Sep-06 3:24
Rob Caldecott4-Sep-06 3:24 
GeneralRe: confuse CString::Replace() Pin
Max++4-Sep-06 3:56
Max++4-Sep-06 3:56 
GeneralRe: confuse CString::Replace() Pin
Hamid_RT4-Sep-06 22:30
Hamid_RT4-Sep-06 22:30 
QuestionCaption Bar with a Close Button in Dynamic Splitter Window Pin
rkshdixit4-Sep-06 2:14
rkshdixit4-Sep-06 2:14 
AnswerRe: Caption Bar with a Close Button in Dynamic Splitter Window Pin
gregdolley4-Sep-06 22:40
gregdolley4-Sep-06 22:40 
Questionput the tool tips to emptions in chat window Pin
johnalek4-Sep-06 2:10
johnalek4-Sep-06 2:10 
QuestionHow to use use Inline function? Pin
Andy Rama4-Sep-06 1:41
Andy Rama4-Sep-06 1:41 
AnswerRe: How to use use Inline function? Pin
Waldermort4-Sep-06 1:51
Waldermort4-Sep-06 1:51 
AnswerRe: How to use use Inline function? [modified] Pin
Emilio Garavaglia4-Sep-06 2:11
Emilio Garavaglia4-Sep-06 2:11 
QuestionGetting Current Desktop Name Pin
MohammadAmiry4-Sep-06 1:32
MohammadAmiry4-Sep-06 1:32 
AnswerRe: Getting Current Desktop Name Pin
gregdolley4-Sep-06 22:47
gregdolley4-Sep-06 22:47 

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.