My Experiments with Life

December 27, 2010

libwinmm in mingw using C-Free IDE

Filed under: Tech — alanabraham @ 6:12 am

It was Joe who started using C-Free IDE first in our friend circle. We were so much depended on the Turbo C++ for console programming earlier. When we migrated to Windows Vista and later, to Windows 7, we looked for some pretty Editor + Compiler for small programs in C/C++. We dont need Visual Studio because that is so big. All others werr also rejected due to some reason or another. So Joe came with C-Free with an old mingw inside it. But we were ok with it unless someone is using a conio.h which was there in our Turbo C++ library.

C-Free also facilitated using windows API to create some GUI Forms. This day, i started to play some WAV file from a form using PlaySound() function in the API ending up in lots of errors like

error : PlaysoundA@12

Then i added


#include<mmsystem.h>

#pragma comment(lib,"winmm.lib")

But error remains. I saw that in mingw of c-free, instead of winmm.lib,  libwinmm.a is used in Code::Blocks, DevC++ etc. When checked, i saw the same is true as mingw\lib had a libwinmm.a file. So changed to

#pragma comment(lib,”libwinmm.a”)

The error remains the same.

After taking a whole night, i found the real cause of all evil as a linker option

-lwinmm

If you need only for that Project go

Project->Settings->Linker

or for all C-Free programs

Build->Build Options->Linker

After reaching to this point

Linker->Linker Options

Add -lwinmm

The code main.cpp is as follows

#include
#include
#pragma comment(lib,"libwinmm.a")
const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            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;

    //Step 1: Registering the Window Class
    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, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    PlaySound(TEXT("acidrain.wav"),NULL,SND_FILENAME);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

This doesnt really qualify for a post in a blog, but done this is the first step in another experiment. Wait for more.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: