Thread: Win32 API Implementation

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    9

    Win32 API Implementation

    Hello everyone, what i am trying to do is implement a Win32 API code to my main program, and create as a function to call when i want in the main.I have the window code implemented in different project running normally as i want, but when i try to have it on my main project, i just can't create a function for it to choose when to call it.The Win32 API code is the following

    Code:
    #include <windows.h>
    #include "resource.h"
    
    const char g_szClassName[] = "myWindowClass";
    
    HBITMAP g_hbmBall = NULL;
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    		case WM_CREATE:
    			g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
    			if(g_hbmBall == NULL)
    				MessageBox(hwnd, L"Could not load IDB_BALL!", L"Error", MB_OK | MB_ICONEXCLAMATION);
    		break;
    		case WM_CLOSE:
    			DestroyWindow(hwnd);
    		break;
    		case WM_PAINT:
    		{
    			// Just a note, never use a MessageBox from inside WM_PAINT
    			// The box will cause more WM_PAINT messages and you'll probably end up
    			// stuck in a loop
    
    			BITMAP bm;
    			PAINTSTRUCT ps;
    
    			HDC hdc = BeginPaint(hwnd, &ps);
    
    			HDC hdcMem = CreateCompatibleDC(hdc);
    			HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmBall);
    
    			GetObject(g_hbmBall, sizeof(bm), &bm);
    
    			BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    
    			SelectObject(hdcMem, hbmOld);
    			DeleteDC(hdcMem);
    
    			EndPaint(hwnd, &ps);
    			Sleep(500);
    		}
    		break;
    		case WM_DESTROY:
    			DeleteObject(g_hbmBall);
    			PostQuitMessage(0);
    		break;
    		default:
    			PostQuitMessage(0);
    			return DefWindowProc(hwnd, msg, wParam, lParam);
    	}
    	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	WNDCLASSEX wc;
    	HWND hwnd;
    	MSG Msg;
    
    	wc.cbSize		 = sizeof(WNDCLASSEX);
    	wc.style		 = 0;
    	wc.lpfnWndProc	 = WndProc;
    	wc.cbClsExtra	 = 0;
    	wc.cbWndExtra	 = 0;
    	wc.hInstance	 = hInstance;
    	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    	wc.lpszMenuName  = NULL;
    	wc.lpszClassName = g_szClassName;
    	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, L"Window Registration Failed!", L"Error!",MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,g_szClassName,L"A Bitmap Program",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, 1440, 900,NULL, NULL, hInstance, NULL);
    
    	if(hwnd == NULL)
    	{
    		MessageBox(NULL, L"Window Creation Failed!", L"Error!",MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	ShowWindow(hwnd, nCmdShow);
    	UpdateWindow(hwnd);
    
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    	return Msg.wParam;
    }
    Any help will be appreciated!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Win32 API Implementation - C And C++ | Dream.In.Code

    Not spamming the same question across multiple forums would also be appreciated.
    How To Ask Questions The Smart Way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    well i am in need of a help that is why my questions is on a different forum as well

  4. #4
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    what errors are you getting? before posting again check whether you have the same resource file from your other project in this project. you'll need the resource file as it stores all your ID's, etc...
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    "Hello everyone, what i am trying to do is implement a Win32 API code to my main program, and create as a function to call when i want in the main."

    Do I understand correctly that you are working with a console program and you want to open a window on demand?

    If so, that's not even close to the way to do it... Your winapi code is a free standing program not a function call.

    If, as you say the winapi code is working properly the way to load it up is through a system() or ShellExecute() call... Not as a subroutine of your main program.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    ok let me give you the story
    i am using visual studio C++ 2010 coding in C and created a Win32 console project.
    then i created another one which i used the code posted above to create a window using Win32 API.
    Now i want to use the code for the window in a function in the first console project.
    But when i tried to put that code in my project doesnt work
    Am i clear now?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep... that's what I thought you were trying to do.

    Won't work... not without serious modification to both the console program and the window program... Console programs can run message loops and they can open windows, but you can't just take a complete windows program and treat it as a subroutine of a console program.

    Hense my suggestion that you use system() or ShellExecute() to launch your windows program from the console program. You can use it intact, as a free standing exe file with a whole lot less muss and fuss than trying to get a console program to open windows.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    really?????
    oh my god, because i wanted to run that and close after a specific sleep time
    i allready tried as you said running the exe file it goes really well, but how do i change the time of sleep every time??
    you get my point? if yes pls advice thank you

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You will have to create a dialog in your Windows program to ask how long to run for then have it close itself after the stated time with a timer.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    do u suggest any ways of finding and closing that specific exe file?

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Did you actually read the documentation on system() and ShellExecute() ?

    Really if you don't know where the file is, you can't open it.

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    iReturn = (int) ShellExecute(NULL, L"open", L"N:\\Documents\\NewBall.exe", NULL,NULL,SW_SHOWNORMAL);
    // If ShellExecute returns an error code, let the user know.
    if (iReturn <= 32)
    printf("Cannot open file. File may have been moved or deleted.", "Error!", MB_OK | MB_ICONEXCLAMATION) ;

    i used this, and it opens the exe file
    now how to kill it?

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You don't kill it from the console program... it is a free standing program, not part of your console at all.

    If you want the windows program to close after a time delay you have two choices...

    1) have it pop up a dialog and ask the user how long to stay open for
    2) feed it some signal on it's command line to tell it how long to stay open for.

    In either case 1 or 2 you will have to rewrite the windows program to close on a timer... from inside itself.

    The only external option is to send it a WM_CLOSE message from the console program... But that too will require the windows program to respond to the WM_CLOSE message in the appropriate manner.

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    with code provided on this thread it automatically closes after sleep(500)
    but the thing is i have to change that after every loop i do, and that is what troubles me
    and this means im lost :s

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Did you actually read what I said in post 13?

    I gave you two proper ways and one less than proper way of closing the window...
    Take your pick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 API - List of commands
    By Bill Agnor in forum Windows Programming
    Replies: 9
    Last Post: 06-23-2010, 11:54 AM
  2. Domain for Win32 API
    By maxorator in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-28-2006, 10:16 AM
  3. Win32 API Questions
    By CPP-Null in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2003, 04:26 PM
  4. win32 api
    By mikeghet in forum Windows Programming
    Replies: 13
    Last Post: 06-02-2002, 04:30 PM
  5. Win32 API Tutorials?
    By c++_n00b in forum C++ Programming
    Replies: 9
    Last Post: 05-09-2002, 03:51 PM