Windows and C

This is a discussion on Windows and C within the Windows Programming forums, part of the Platform Specific Boards category; I am just starting to learn to program windows. I've scanned the FAQ and the links and didn't see what ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Location
    Central NC
    Posts
    3

    Windows and C

    I am just starting to learn to program windows. I've scanned the FAQ and the links and didn't see what I was looking for, so I hope I didn't overlook anything.

    Trouble I am having is this: If I am writing a windows program and want to use c style functions for string processing or whatever, I don't know how to put it all together (or maybe it doesn't).

    I've got a couple of windows books that program through c or c++, but I don't see any examples or explanations about using c functions in a windows program.

    Can anyone point me int he right direction?

    Thanks for any and all advice.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Assuming you are writing your windows function in C, then of course you can use C functions. I don't really know whether you mean standard library functions (like, say, sqrt or strcmp) or your own functions (or even the Windows API), but I can almost promise you that your books use both.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Location
    Shamokin PA (middle of nowhere)
    Posts
    15
    If you #include "string.h" in addition to "windows.h" you'll be able to use any of the Standard C Library's string primitives, e.g., strlen(), strcat(), strcpy(), etc. However, I'd recommend you also #include "tchar.h" and use rather the tchar macros.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Location
    Central NC
    Posts
    3
    Thanks for the replies.

    I guess to clarify, my windows books do show the use of API functions in a c environment, but they don't demonstrate adding in user defined functions (or I've overlooked them. but I skimmed through again and all I see is API functinos in the code, for windows I/O, file access, GDI and all).

    What I really want to know right now is where in the code would I insert my functions? or does it matter? in winmain? I would like to see some sample code that mixes API calls and functions with C and user-defined functions.

    Any tips or links to sample code is greatly appreciated.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    For links to sample code: start clicking topics in the forum. (You are allowed to read other people's topics!) The VirtualEx one has complete code, although you do have to click the link to the MSDN forum to get it.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    winprog.org is a good resource for windows programming in C.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Location
    Shamokin PA (middle of nowhere)
    Posts
    15
    You seem to be making a distinction that doesn't really exist. Gdi calls, Api calls, they are all just ordinary C functions that will work compiled as C or C++. No special rules apply if your environment is Microsoft Windows and you are writing a GUI program, other than if the function call is external to your code, you'll need an include file and the underlying preogram library has to be in your link path. If you create your own C function such as...

    Code:
    void Prnt(char* pChar)
    {
     printf("pChar = %s\n", pChar);
    }
    ...you can put the function anywhere you like in your source code, as long as that anywhere has an #include <stdio.h> or <stdio>, because the function uses a C library function named printf. Actually, you wouldn't even need a prototype for that Prnt function, if its located above in your source code before the call, which would look like this...

    Prnt("I'm theDoctor");

    Here's a link to a program (5th post in thread) that uses sprintf in a Win32 Gui program...

    ProgEx37 -- Windows GUI Programming; Basic Template Program With Discussion

  8. #8
    Registered User
    Join Date
    Aug 2011
    Location
    Central NC
    Posts
    3
    Freddie,

    Thanks alot for the info and the link. I just glanced over it, and it looks like that's exactly what I needed to see. I'm headed back to study it. I'm grateful for your help.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by theDoctor View Post
    Freddie,

    Thanks alot for the info and the link. I just glanced over it, and it looks like that's exactly what I needed to see. I'm headed back to study it. I'm grateful for your help.
    ... already suggested .... theForger's Win32 API Tutorial ... pure C, pure WinAPI.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by theDoctor View Post
    What I really want to know right now is where in the code would I insert my functions? or does it matter? in winmain? I would like to see some sample code that mixes API calls and functions with C and user-defined functions.

    Any tips or links to sample code is greatly appreciated.
    Ok... here's a complete (although painfully simple) unicode text editor written in C/WinAPI...

    Code:
    /* 
    
    Tiny Unicode Editor Example
    
    */
    // for the compiler
    #define UNICODE
    #define _UNICODE
    #define WIN32_DEFAULT_LIBS
    #define WIN32_LEAN_AND_MEAN
    #define _WIN32_WINNT 0x0502
    #define _X86_
    
    // Windows headers
    #include <windows.h>
    #include <commdlg.h>
    
    //  PellesC headers
    #include <stdlib.h>
    #include <wchar.h>
    #include <tchar.h>
    
    // Window handles
    HWND      Wind[5];  
    HINSTANCE Inst;
    TCHAR     FileName[MAX_PATH]; // filename when opened
    BOOL      RevBytes = 0;
    
    
    // save a file
    void SaveToFile(void)
      { OPENFILENAME  ofn;        // filename struct
        HANDLE        fh;         // handle for opening files
        DWORD         fs;         // size of the data
        PTCHAR        fd;         // pointer to data
        // get the filename
        memset(&ofn,0,sizeof(ofn));
        ofn.lStructSize = sizeof(ofn);
        ofn.hwndOwner    = Wind[0];
        ofn.hInstance    = Inst;
        ofn.lpstrFilter  = _T("All Files\0*.*\0\0");    
        ofn.lpstrFile    = FileName;
        ofn.nMaxFile     = MAX_PATH;
        ofn.lpstrTitle   = L"Save your work";
        ofn.Flags        = OFN_NONETWORKBUTTON |
                           OFN_HIDEREADONLY |
                           OFN_NOTESTFILECREATE |
                           OFN_OVERWRITEPROMPT;
        if (!GetSaveFileName(&ofn))
          return;
        // get unicode adjusted file size from edit control
        fs = (SendMessage(Wind[4],WM_GETTEXTLENGTH,0,0) * sizeof(TCHAR)); 
        if (fs < 1)
          return; 
        // create text buffer
        fd = malloc(fs);
        // get the text from the control
        SendMessage(Wind[4],WM_GETTEXT,fs,(LPARAM)fd);
        // open the file
        fh = CreateFile(FileName,GENERIC_WRITE,0,NULL,
                                CREATE_ALWAYS,
                                FILE_ATTRIBUTE_NORMAL |
                                FILE_FLAG_WRITE_THROUGH,NULL);
        // save the file
        if (fh != INVALID_HANDLE_VALUE)
          { WriteFile(fh,fd,fs,&fs,NULL);
            CloseHandle(fh); }
        free(fd); }
    
    
    
    // open a file
    void OpenFromFile(void)
      { OPENFILENAME  ofn;        // filename struct
        HANDLE        fh;         // handle for opening files
        DWORD         fs;         // size of the data
        PTCHAR        fd;         // pointer to data
        // get the filename
        memset(&ofn,0,sizeof(ofn));
        ofn.lStructSize = sizeof(ofn);
        ofn.hwndOwner    = Wind[0];
        ofn.hInstance    = Inst;
        ofn.lpstrFilter  = L"All Files\0*.*\0\0";    
        ofn.lpstrFile    = FileName;
        ofn.nMaxFile     = MAX_PATH;
        ofn.lpstrTitle   = L"Open a file";
        ofn.Flags        = OFN_NONETWORKBUTTON |
                           OFN_HIDEREADONLY |
                           OFN_NOTESTFILECREATE |
                           OFN_OVERWRITEPROMPT;
        if (!GetOpenFileName(&ofn))
          return;
        // open the file 
        fh = CreateFile(FileName,GENERIC_READ,FILE_SHARE_READ,NULL,
                                OPEN_EXISTING,
                                FILE_ATTRIBUTE_NORMAL,NULL);
        if (fh == INVALID_HANDLE_VALUE)
          { MessageBox(Wind[0],L"Error opening the file",L"OOPS!",0);
            return; }
        // get the file size
        fs = GetFileSize(fh,NULL) + sizeof(TCHAR);
        // create text buffer
        fd = malloc(fs);
        // clear to 0
        memset(fd,0,fs);
        // read from disk  
        ReadFile(fh,fd,fs,&fs,NULL);
        // close the file
        CloseHandle(fh);
        // put the text in the control
        SendMessage(Wind[4],WM_SETTEXT,fs,(LPARAM)fd);
        free(fd); }
    
    
    
    // resize the window
    VOID ResizeWindow(LPARAM lParm)
      { MoveWindow(Wind[4],60,2,LOWORD(lParm) - 62,HIWORD(lParm) - 4,1); }
    
    
    
    // Message Loop
    LRESULT CALLBACK MsgProc(HWND wnd,UINT msg,WPARAM wparm,LPARAM lparm)
      { switch (msg)
          { case WM_COMMAND :
              switch (LOWORD(wparm))
                { case 1001 :
                    OpenFromFile();
                    return 0;
                  case 1002 :
                    SaveToFile();
                    return 0;
                  case 1003 :
                    PostMessage(Wind[0],WM_CLOSE,0,0);
                    return 0;
                  default :
                    return DefWindowProc(wnd,msg,wparm,lparm); }
            case WM_SIZE  :
              ResizeWindow(lparm);
              return 0;
            case WM_CLOSE :         // close window
              DestroyWindow(Wind[0]);  
              return 0;
            case WM_DESTROY :       // NC Exit button
              PostQuitMessage(0); 
              return 0; 
            default :
              return DefWindowProc(wnd,msg,wparm,lparm); } }
    
    
    
    // create the window
    VOID CreateMainWindow(void)
      { WNDCLASS  wc;
        // register App Class
        memset(&wc,0,sizeof(wc));
        wc.style          = CS_CLASSDC;
        wc.hInstance      = Inst;
        wc.hCursor        = LoadCursor(NULL,IDC_ARROW);
        wc.hbrBackground  = CreateSolidBrush(GetSysColor(COLOR_3DFACE));
        wc.lpfnWndProc    = &MsgProc;
        wc.lpszClassName  = L"TINY_UNICODE";
        RegisterClass(&wc);
    
        // create the main window
        Wind[0] = CreateWindowEx( WS_EX_CONTROLPARENT,
                        L"TINY_UNICODE",L"Tiny Unicode Editor",
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT,0,500,300,NULL,NULL,Inst,NULL);
        // buttons
        Wind[1] = CreateWindow(L"BUTTON",L"Open",
                        WS_CHILD | WS_VISIBLE | BS_FLAT,
                        2,2,50,25,Wind[0],(HMENU) 1001,Inst,NULL);
        Wind[2] = CreateWindow(L"BUTTON",L"Save",
                        WS_CHILD | WS_VISIBLE | BS_FLAT,
                        2,30,50,25,Wind[0],(HMENU) 1002,Inst,NULL);
        Wind[3] = CreateWindow(L"BUTTON",L"Quit",
                        WS_CHILD | WS_VISIBLE | BS_FLAT,
                        2,60,50,25,Wind[0],(HMENU) 1003,Inst,NULL);
        // edit window
        Wind[4] = CreateWindowEx(WS_EX_CLIENTEDGE,L"EDIT",NULL,
                        WS_CHILD | WS_VISIBLE |
                        ES_MULTILINE,
                        60,2,200,200,Wind[0],NULL,Inst,NULL);  
        UpdateWindow(Wind[0]);
        ShowWindow(Wind[0],SW_SHOWNORMAL);    }
    
    
    
             
    // Program Entry Procedure
    int WINAPI WinMain(HINSTANCE hinst, HINSTANCE pinst, LPSTR cmdl, int show)
      { MSG wmsg;
        // save instance handle
        Inst =    hinst;    
        // make the window
        CreateMainWindow();
        // dispatch window messages
        while (GetMessage(&wmsg,NULL,0,0))
          { TranslateMessage(&wmsg);
            DispatchMessage(&wmsg); }
    
        return 0; }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Windows directory with OPENFILENAME in Windows mobile
    By nikhilesh1987 in forum Windows Programming
    Replies: 2
    Last Post: 05-31-2011, 02:49 AM
  2. GetDiskFreeSpace Windows API fails on windows 2000
    By dnyampawar in forum Windows Programming
    Replies: 7
    Last Post: 07-09-2009, 03:39 AM
  3. Open an excel file from a windows service under Windows Vista
    By AdrianaLuby in forum C# Programming
    Replies: 1
    Last Post: 06-05-2007, 03:55 AM
  4. Replies: 4
    Last Post: 10-03-2005, 04:44 PM
  5. Replies: 6
    Last Post: 01-07-2002, 01:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21