Computing.Net > Forums > Programming > Win API

Win API

Reply to Message Icon

Original Message
Name: Ninwa
Date: August 1, 2002 at 23:35:12 Pacific
Subject: Win API
Comment:

I'm a novice C++ Programmer, I've recently gotten into the windows API. I understand it quite well, how messages are sent and how event driven programming works...

My major resource has been the msdn library that microsoft offers... http://msdn.micrsofot.com/library ... but they dont go indepth about different structures like, hdc, pc, rect, etc etc... I know how to use them, but I dont know what makes them work...and it just bugs me, I need to know HOW they work... Im that kind of person..

I cant stand using these structures, only knowing what they can do, I want to know how they work... ( I think ive made my point) ... so if anyone knows any good resources that goes in-depth on these things...please email it to me, or reply to this thread.


Report Offensive Message For Removal

Response Number 1
Name: Dan
Date: August 2, 2002 at 10:16:41 Pacific
Subject: Win API
Reply: (edit)

The structures don't do any work at all, they're simply a means of holding like items together in a sort of collection, so that when you use them, all related stuff is there.
If you declare your own structures, it can make it more convenient to access data:
struct sHDR {
char szJob[20];
char szDescr[30];
int ID;
char szAddr[30];
}
struct sHDR sHeader;
then,
strcpy( sHeader.szJob, "My first" );
sHeader.ID = 22;
etc...

the programmers for Windows simply did the same thing.


Report Offensive Follow Up For Removal

Response Number 2
Name: Jeff J
Date: August 2, 2002 at 10:28:36 Pacific
Subject: Win API
Reply: (edit)

You are plagued with the same bug I am ;) I'm sure there is information out there on this sort of thing, but I have never seen anything specific on how stuff works. Hopefully, someone else can post something they've come across, but I doubt there's much of a market for such books.

Much of this knowledge comes from studying computer architecture, and not from higher languages or programming technologies. For better or worse, the trend for some time has been away from the basics, such as assembler, and more towards "easier" languages.

I can touch on a few things, however (naturally, I'll carry on for a ways :). Structures are actually simple, and form the basis for most "complex", "compound", or "user-defined" types. They are the basis for classes (in C++, structs are classes, with few differences). In C, structs contain only data, and hence the phrase "plain old data" (POD). In C++, functions can also be associated with a struct of data, but the Win32 API is traditionally only in C.

The often-used RECT struct is a good example. Forgetting its actual C-style declaration for the moment, in C++ it looks like:

struct RECT {
  LONG left;
  LONG top;
  LONG right;
  LONG bottom;
};

In this case, since all member data (fields) are the same size, it's similar to an array (sequence of bytes) in memory:

| left | top | right | bottom |

where each name (identifier) is associated with a memory offset (on a 32-bit machine, these members are each 4-bytes in size). This could just as easily be done in an array, and it would look virtually identical in memory. If this were an array called rcArr (with 4 elements), we would have rcArr[0] for left, rcArr[1] for top, rcArr[2] for right, and rcArr[3] for bottom. As you can tell, one advantage of structs, is that each element has a unique name, rather than a numeric index (offset). Very programmer-friendly, even though the compiler translates top to mean '4 bytes in from the starting address of the RECT'. That's the same way the compiler would translate rcArr[1]

| rcArr[0] | rcArr[1] | rcArr[2] | rcArr[3] |

So we know all data types, including structs (compound data) and arrays (regular sequences of the same data type), are really just strips of memory, where the compiler has already calculated the position of each member, and we don't have to think about it (unlike in assembler). The real beauty of structs is that they can be a mix of all sorts of types:

struct MyStruct {
  int int_;
  char c;
  short sh;
  double double__;
};

Ignoring the realities of padding (I really don't have room for that here, pardon the pun), in memory it would look something like this:

| int_ | c | sh | double__ |

The size of each member is different (on a 32-bit machine, 4, 1, 2, and 8 bytes, respectively). The compiler again allocates a strip of memory, and calculates the position (offset) of each named member, except the members are not evenly sized and spaced as with arrays.

Like an array, structs/classes are usually too big to pass (copy) in their entirety (by value), so a pointer to one (its address) is usually passed (by reference). This makes a RECT, for example, more efficient to use than 4 separate longs. Passing a RECT pointer takes only 4 bytes, and all the data stays in memory you already allocated (why copy everything?). Passing 4 longs means 4*4=16 bytes must be copied (passed).

Here's a fun snippet to "see" the byte mapping of structs/classes...

#include
#include //for getch
#include //for RECT

int main() {

  const int iSize = sizeof(RECT);
  char buf[32];

  //anonymous union
  union {
   RECT rc;
   char chArr[iSize];
  };

  rc.left = 0;
  rc.top = 1;
  rc.right = 2;
  rc.bottom = 3;

  for (int i=0; i < iSize; i++) {

   printf("%d,", chArr[i]);
  }

  printf("\naddress of RECT = %X\n", (int)&rc);
  printf("address of left = %X\n", (int)&rc.left);
  printf("address of top = %X\n", (int)&rc.top);
  printf("address of right = %X\n", (int)&rc.right);
  printf("address of bottom = %X\n", (int)&rc.bottom);

  getch(); //pause
  return 0;
}

You can substitute any struct/class for RECT and rc, and change the printf statements as appropriate. This is easier than staring at memory dumps, though that can be useful too. For those not familiar with unions, they just allow differently named things to occupy the same memory strip, so that they overlap, and anonymous means unnamed.

In VC++, it's easy to get to type definitions. Place the cursor on the named thing, press F12, and you will jump to the file and definition. Often definitions are made up of other definitions, so repeat the process for those in turn. Many handles in Win32 wind up being void pointers or structs with a single int member, for example.


Report Offensive Follow Up For Removal

Response Number 3
Name: Jeff J
Date: August 2, 2002 at 14:23:31 Pacific
Subject: Win API
Reply: (edit)

Forgot to re-paste on the "confirm post" page (#£%!@*&). That should have been:

#include < stdio.h >
#include < conio.h > //for getch
#include < windows.h > //for RECT

and ignore buf, since I dropped itoa, so it is no longer needed.


Report Offensive Follow Up For Removal

Response Number 4
Name: Ninwa
Date: August 4, 2002 at 19:42:19 Pacific
Subject: Win API
Reply: (edit)

Thankyou for the information, I have a better understanding of that now. I still thrive to go deeper into what actually makes every last thing tick.... I suppose it goes behind High level programmign and more into engineering then ;P

We take so much for granted, and I want to just know how every last thing works, but I know there is far to much to learn... :D ...


Report Offensive Follow Up For Removal

Response Number 5
Name: Ninwa
Date: August 4, 2002 at 19:42:54 Pacific
Subject: Win API
Reply: (edit)

Beyond Rather, not Behind ;D Lol


Report Offensive Follow Up For Removal


Response Number 6
Name: gowtham
Date: August 8, 2002 at 01:25:18 Pacific
Subject: Win API
Reply: (edit)

how to shutdown the system please send reply for me


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Win API

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software