Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Sharing Variables Between Win32 Executables


March 1997/Sharing Variables Between Win32 Executables/Sidebar

Every graphical component of the Win32/16 GUI (Windows, edit controls, list boxes, etc.) has a "handle" assigned to it by the operating system upon instantiation. This handle is somewhat like a pointer in that it contains a dynamic, integral value that designates some resource. But unlike a pointer, a handle's value is valid across process boundaries. If a process A gets the handle of a list box in Process B, then A can fill B's list box B by calling SendMessage with that handle:


SendMessage(<LB_HANDLE> , LB_ADDSTRING, 0, (LPARAM)
    (LPCTSTR)"New Item")

In Win32, almost every function or procedure that influences a Windows control (and almost everything on your screen is a windows control) ultimately boils down to a SendMessage function. SendMessage's first argument is the handle of the destination control/window and its second argument is the type of message you want to send. The third argument is a generic word length parameter (unused in this case). The fourth argument is a generic-32 bit value which can be used as a long, or cast to a pointer to a data type appropriate for the message being sent.

In the above example, the fourth parameter is cast to a pointer to a string object, since that's what the list box will expect when it interprets an LB_ADDSTRING message. It is this fourth parameter that severely limits a process's ability to influence handles whose associated controls are outside the process's space. For many types of messages the fourth parameter is a pointer to a structure, and this creates a problem. What typically happens is as follows: 1) Process B has the handle of a window or control that resides in Process A's address space; 2) Process B calls SendMessage to send a message that requires a pointer to a structure as the fourth parameter. 3) The window or control in Process A receives this message, and attempts to dereference the pointer in the fourth parameter. However, that address is only valid in Process B's address space, so the operating system raises an Access Violation and terminates Process A for trying to dereference a bad pointer. o


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.