Results 1 to 3 of 3

Thread: Win32 Getting Down and Dirty

  1. #1
    Senior Member
    Join Date
    Feb 2003
    Posts
    282

    Win32 Getting Down and Dirty

    Win32 Getting Down and Dirty

    This the sequel to Win32 under the hood:
    http://www.antionline.com/showthread...949#post646949

    Side Note: To my knowledge *nix gurus can code and run Win32 apps in linux. I believe a package for gcc is needed. Wine I believe allows native Win32 to execute on *nix. Don't quote me but if you're interested investigate.

    Last we looked at how to include the Win32API in your program code. Discussed WinMain the entry point for a Win32 application. We also briefly discussed its parameters, how Windows fills in these parameters and some predefined typedefs.

    We very briefly discussed windows.h last, lets take a look inside. Open up windows.h with your text editor. Recall that the Win32API is composed mainly of (Kernel32.dll, User32.dll, GDI32.dll and System32.dll). Amongst the first few includes we see inside windows.h are winbase.h winuser.h and wingdi.h. We also mentioned advapi32.dll is used for the registry API, and sure enough winreg.h also appears. We had our look, enough gocking, get out your win32.hlp file you downloaded from our last tutorial, (link at top)

    There are quite a few typedefs made by windows.h, you will become familiar with them as we begin to use them. However a few I would like to mention. LPSTR LP is long pointer, so LPSTR is a long pointer to a string. When a LPSTR or LPTSTR is required as an argument to a function, we declare as a character array:

    char *string = "This is a string";

    This satisfies any function that requires a LPSTR to be passed to it. This brings us to pointers, as we will refer to them as handles in win32. these typedefs being with H, so HWND is a handle to a window, HICON is a handle to an icon and HINSTANCE is a handle to an instance.

    HINSTANCE in particular is popular, it is a handle to our currently running executable, so if your programs output executable is Test.exe when you execute it, HINSTANCE hInst, hInst becomes a handle to the location in memory where Test.exe is located. This is used as an argument to many Win32 functions.

    Before we get into drawing windows and making GUIs and all the good stuff, we will introduce a few functions that may be of interest to you.

    We last talked a bit of MessageBox() and I suggest you look it up in your win32.hlp to get a better feel for it. Now we will introduce GetComputerName(), this is of particular interest to those familiar with netstat in DOS. We know under the local column in netstat, we see out computer name followed by the local port

    anon:7941

    In this case my computers name is anon, well GetComputerName is a Win32 function that can give you your computer name.

    BOOL GetComputerName(

    LPTSTR lpBuffer, // address of name buffer
    LPDWORD nSize // address of size of name buffer
    );

    BOOL is a Boolean value, always TRUE or FALSE, 0 or 1. If we give it an address for a buffer to hold the computer name, it will fill it in for us. LPDWORD is a long pointer to a double word. The size tells Windows the max characters your buffer can hold so it doesn't try to overload your buffer

    char CompName[MAX_COMPUTERNAME_LENGTH + 1];
    DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;

    if (GetComputerName(CompName, &dwSize) == TRUE){
    MessageBox(NULL, CompName, "Your Computer's Name is...", MB_OK | MB_ICONINFORMATION);
    } else {
    MessageBox(NULL, "Failed to retrieve your Computer's name", "Error", MB_OK | MB_ICONERROR);
    return 1;
    }

    char CompName gives us an array of type char, this is our character array or buffer, and satisfies the required type LPTSTR. MAX_COMPUTERNAME_LENGTH is defined in windows.h, it's a number representing as its name says. +1 for terminating null character. &dwSize again remember its a LPDWORD LP = Long pointer, & as we know from C is the address of operator, providing it with the address not the value, thus satisfying LPDWORD criteria. If you remember MessageBox the rest is childs play.

    The complexity of these short tutorials will increase as we progress. For now I want to introduce simple functions, and as I do so cover the fundamentals. As a substitute between waiting for another tutorial, check out www.winprog.org

    Windows programming overview: http://www.antionline.com/showthread...419#post645419
    Win32 Under the hood:
    http://www.antionline.com/showthread...hreadid=246597

    Check also mathgirl32's Win32 API Programming C++
    http://www.antionline.com/showthread...hreadid=238176

  2. #2
    rebmeM roineS enilnOitnA steve.milner's Avatar
    Join Date
    Jul 2003
    Posts
    1,021

    Re: Win32 Getting Down and Dirty

    Originally posted here by journy101
    Side Note: To my knowledge *nix gurus can code and run Win32 apps in linux. I believe a package for gcc is needed. Wine I believe allows native Win32 to execute on *nix. Don't quote me but if you're interested investigate.
    I've never used Wine directly, but those awfully nice Borland folks use some Wine libs to make Delphi availaible in a *nix environment. They call it Kylix & you can get an open source version free from Borland by filling in a few forms. Absolutely superb RAD language for *nix

    As for running windows apps under *nix I use a netraverse product Win4Lin - It's a VM. You have to buy it, but if you must use windows apps it runs everything that I've ever thrown at under 95/98/ME. It doesn't do the NT/200? thing tho.

    For that you need a product called VMWare (good but more expensive)

    HTH

    Steve
    IT, e-commerce, Retail, Programme & Project Management, EPoS, Supply Chain and Logistic Services. Yorkshire. http://www.bigi.uk.com

  3. #3
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    Strangely enough, I believe it's possible to have a compiler which produces native win32 code that is native to Linux (by native I mean without using Wine or other such trickery)

    I think mingw can actually be built under Linux.

    The main target of win32 code is always going to be Windows even if it can be run under wine.

    However if you had a big project with a very strange build system (perhaps for embedded Windows systems) you might want to build under Linux and run under Windows.

    Suppose you were targetting a large number of different platforms (for instance in embedded) and wanted to have a consistent Linux-based build system. It might be easier to get a Windows cross compiler on Linux than to port your entire build system to Win.

    Slarty

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •