I didn't want to fill up a thread with a bunch of code, but I couldn't find a way to attach a file to an e-mail using this site's "send a message" system. I would have inserted the .CPP file.

This is a basic windows program to run tests on what started out to be two problems I had. Comrade Ogilvy solved one of them for me and my large program now is displaying the mouse position in my status bar the way I wanted. I've cleaned out all the unneeded code so only the "CreateMenu" problem code is left. I stepped through SofeIce to try to see what is happening but when SI gets to "SysEnter", if I single step through, my box blue screens. I've had that happen to me before in other programs when the program tries to step through "SysEnter". I don't know why. If you compile and run this you will get a "Get System Error" stating "The system cannot find the file specified." Why not? Isn't CreateMenu suppose to return a new handle?

#include <windows.h>
HINSTANCE hInstance;
HWND hwndMain;
HMENU hMenu;

char szBuff3[128]="TEST";                   

MSG msg;
WNDCLASS  wc;
WPARAM wParam;
LPARAM lParam;

//***** START Prototypes

int WINAPI WinMain (HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nCmdShow);
LRESULT CALLBACK MainWndProc(HWND hwndMain, UINT uMsg , WPARAM wParam , LPARAM lParam);
//HMENU NewMenu(HMENU hMenu);

//*******END Prototypes

int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
    
    (void)UNREFERENCED_PARAMETER(hPrevInstance);
    (void)UNREFERENCED_PARAMETER(lpCmdLine);
    (void)UNREFERENCED_PARAMETER(nCmdShow);

        wc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
        wc.lpfnWndProc = (WNDPROC)MainWndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;       
        wc.hIcon = NULL;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName =  NULL;
        wc.lpszClassName = "szClassName";

        if (!RegisterClass(&wc)) 
            return FALSE;

  hwndMain = CreateWindow("szClassName","szAppName", WS_OVERLAPPEDWINDOW,
             0,0,740,480,NULL,NULL,hInstance,NULL );

  ShowWindow(hwndMain,SW_SHOW);
  UpdateWindow(hwndMain);

  while (GetMessage(&msg, NULL,0,0)) 
  {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
  }
 
  return (msg.wParam);
}

LRESULT WINAPI MainWndProc (HWND hwndMain, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_CREATE:
       
            HMENU CreateMenu(void);

    LPTSTR lpMsgBuf;
    FormatMessage( 
    FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
    NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    (LPTSTR)(&lpMsgBuf),0,NULL);
    MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
    LocalFree( lpMsgBuf );

            IsMenu(hMenu);
          if(hMenu == NULL)
           MessageBox(hwndMain,"No Menu","MBOX",MB_OK);
          else
           MessageBox(hwndMain,"Have Menu","MBOX",MB_OK);

            SetMenu(hwndMain,hMenu);
            AppendMenu(hMenu,MF_ENABLED | MF_STRING,1,szBuff3);
            DrawMenuBar(hwndMain);
            UpdateWindow(hwndMain);

         return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
           
        default:
            return (DefWindowProc(hwndMain, message, wParam, lParam));
    }
}

Recommended Answers

All 5 Replies

I didn't want to fill up a thread with a bunch of code, but I couldn't find a way to attach a file to an e-mail using this site's "send a message" system. I would have inserted the .CPP file.

Hit the Go Advanced button then scroll down the page and you will see an option to attach a file. You do NOT send it to DaniWeb via email, but upload it using the options for the Manage Attachments button.

line 64: That does not call the function but declares a prototype and my compiler VC++ 2005 Express says it is different than that included in windows.h (winuser.h). If your intent was to actually call that function then code it like this:

HMENU hMenu = CreateMenu();

First let me thank you for setting up my code correctly. I read the thread that
told me to put <code> at the top and bottom. I assumed that the system would handle the rest when sent. Now I know about the # sign. I would never have guessed about
the way to send a file was under advanced. I'll bet if I searched long enugh I'd find something about it somewhere on your site. When all else fails, read the directions, right?

The exact way my Borland library reads in winuser.h is:

_declspec (dllimport)HMENU _stdcall CreateMenu(void); Can I assume that yours
reads the same?

I put your suggested way into my program and I get a compile error "bypasses
initialization of a local varible in function". I then cleaned off all code
except the IsMenu block in case the stuff in format message was causing the
error. Still the same error message.

Prior to asking for help, I had read a lot on CreateMenu and tried every
way others had used that I found. I tried ::CreateMenu, CreateMenu(VOID)
which is the way Microsoft printed it in the Win32 Help, CreateMenu(void)
like it is in winuser.h. None gave me a handle. I also compiled using my
DDK compiler which has it's own library. Still no handle. I'm frustrated.

>>"bypasses initialization of a local varible in function"

That error message is just telling you that you declared a variable inside a switch case statement without braces. Two ways to correct that:
(1) add the braces

switch(something)
{
     case 0:
     {
          HMENU hMenu = /* blabla */
      }
}

or declare hMenu above and outside the switch statement.

What you read in the Borland library documentation, and in the online MSDN is called a function prototype. They do not actually get executed but just tell the compiler the parameters and return value. If you are not familar with function prototypes then you need to read the link I posted because they are very basic to the C and C++ languages and you will encounter them thousands of times in the future.

Problem solved. I learned quite a bit from the help I've gotten from this site.

thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.