#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    MessageBox and C


    my understanding of programming is quite near to the ground – may be a little over the most illustrious axiom 'hello world'.
    coming straight to the point that i like to learn is - on the line no:16 in the code below, the printf function is called to output certain string.

    How to get that very output in a MessageBox, using code something like, as in line no:18. Of course it doesn’t compile as such – please help me...!

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    
    int main()
    {
       HANDLE hFile;
       LPSTR  fname  = "abc.dat";
    
    hFile = CreateFile(fname,GENERIC_READ,0,NULL,OPEN_EXISTING,0, NULL);
    
       if(hFile!=INVALID_HANDLE_VALUE)
       printf(_T("\nFile %s is found\nhFile is a valid Handle\n"),fname);
       else
       {
       printf(_T("\nFile %s NOT found\nhFile is INVALID Handle\n"),fname);
    
       //MessageBox(NULL,"File Not Found","Invalid handle",MB_OK);
       }
       CloseHandle(hFile);
       return 0;
    }
    i would like the using of main() instead of WinMain as used in the code fragment below:
    Code:
    WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int CmdShow)
    hFile = CreateFileA(fname,GENERIC_READ,0,NULL, OPEN_EXISTING, 0, NULL);
    
    if(hFile!=INVALID_HANDLE_VALUE)
    MessageBox(NULL, _T("returns valid handle"), _T("abc.dat is FOUND "), MB_OK);
    else
    MessageBox(NULL, _T("handle is NOT valid"), _T("File is NOT found "), MB_OK);
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    *printf is a family of functions. Among other things, one prints to the standard output file, stdout, another prints to a file, and yet another prints to a string. Use the one that prints to a string, sprintf, and then give that string to MessageBox().

    Be forewarned that Visual Studio will complain loudly about sprintf being unsafe (you do, after all, have to ensure that the char array you are writing to is large enough to contain the largest string you'd produce, plus one more for the null-terminator) and tell you to use a safer form of that function.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0
    Originally Posted by dwise1_aol
    *printf is a family of functions. Among other things, one prints to the standard output file, stdout, another prints to a file, and yet another prints to a string. Use the one that prints to a string, sprintf, and then give that string to MessageBox().
    Thank you for your prompt response..!
    your explanation was indeed informative and useful to me.

    i tried to give the string to the MessageBox(). but it returned error LNK2019: unresolved external symbol __imp__MessageBoxA@16

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
    char buff[100];
    
    sprintf(buff,"\nfor thisString been moved to the buff, by the sprintf\n");
    
    //MessageBox(NULL,buff,0,MB_OK); 
    //error LNK2019: unresolved external symbol __imp__MessageBoxA@16
    
    printf("printf prints thisString as it is present in the buff %s",buff);
    return 0;
    }
    output:
    printf prints thisString, just as it is present in the buff
    for thisString been moved to the buff, by the sprintf

    i suppose i got the sprintf clear, but not the MessageBox right Yet..
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    When you get an error message like that from the linker, that means that it couldn't find the code for that function in any of the libraries it was told to use -- usually, the standard libraries are automatically configured to be linked in. You need to find out which library contains MessageBox and explicitly add it to the linker's library list in your project. Since I have no idea what compiler you're using,

    That said, I am not at all sure that you could call MessageBox from within a console app. I could be wrong, but I've never heard of it being done.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0
    Originally Posted by dwise1_aol
    when you get an error message like that from the linker, that means that it couldn't find the code for that function in any of the libraries it was told to use...
    what you have stated seems to be wholly right and i like to keep on, more or less along the same lines.

    with visual studio2008 installed in my system, i normally am using its compiler cl.exe for compiling console applications –
    and i find the link.exe within the bin folder, so presumably that could be the linker being used to link obj file to executable.

    Originally Posted by dwise1_aol
    -usually, the standard libraries are automatically configured to be linked in. You need to find out which library contains MessageBox and explicitly add it to the linker's library list in your project. Since I have no idea what compiler you're using..
    i suppose MessageBox function is within user32.dll but how to go about feeding that info to linker to make it create a MessageBox in this context?

    Regards..!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    i suppose MessageBox function is within user32.dll but how to go about feeding that info to linker to make it create a MessageBox in this context?
    You would use the pragma command in your source file as follows:

    Code:
    #pragma comment(lib, "user32.lib")
    One way to solve your problem is as follows....

    Code:
    #pragma comment(lib, "user32.lib")
    
    #include <windows.h>
    #include <tchar.h>
    #include <strsafe.h>
    
    void DisplayMessage(char *msg)
    {
        LPVOID lpDisplayBuf;
    
        lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
                                          (lstrlen((LPCTSTR) msg) + 1) * sizeof(TCHAR));
    
        StringCchPrintf((LPTSTR)lpDisplayBuf,
                        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
                        TEXT("%s"),
                        msg);
    
        MessageBox(NULL, (LPCTSTR) lpDisplayBuf, TEXT("myMessage"), MB_OK);
    
        LocalFree(lpDisplayBuf);
    }
    
    int main(void)
    {
        DisplayMessage("Testing") ;
        return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo