I do not want to misguide anyone responding, I am a newbie to the C++ arena.
I am just doing little starter programs to figure things out, but for the life of me I don't get the whole hInstance thing.
The objective I have is to learn C++, write a program for windows that will connect to and use an SQL server database.
My confusion starts in the
int WinMain (??, ??, ??) function...
So could someone explain in newbie english for me, thanks.
hopalongcassidy 35 Junior Poster
If you need help with some code, it would help a lot if you would post an example of the code you are having problems with. Otherwise, I don't see how anyone can help you!
Hoppy
twooften 0 Newbie Poster
////////All but a few lines are from an online thing that I have been learning from.
#include <windows.h>
LPSTR szClassName = "MyClass";
HINSTANCE hInstance;
LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
WNDCLASS wnd;
MSG msg;
HWND hwnd;
HMENU hmenu;
hInstance = hInst;
hmenu = LoadMenu(hInst, MAKEINTRESOURCE(IDM_MY_MENU));
wnd.style = CS_HREDRAW | CS_VREDRAW; //we will explain this later
wnd.lpfnWndProc = MyWndProc;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.hInstance = hInstance;
wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); //default icon
wnd.hCursor = LoadCursor(NULL, IDC_ARROW); //default arrow mouse cursor
wnd.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1);
wnd.lpszMenuName = MAKEINTRESOURCE(IDM_MY_MENU);
wnd.lpszClassName = szClassName;
if(!RegisterClass(&wnd)) //register the WNDCLASS
{
MessageBox(NULL, "This Program Requires Windows NT",
"Error", MB_OK);
return 0;
}
hwnd = CreateWindow(szClassName,
"Murrays Sharpening & Tool Repair Service Command Center",
WS_OVERLAPPEDWINDOW, //basic window style
CW_USEDEFAULT,
CW_USEDEFAULT, //set starting point to default value
CW_USEDEFAULT,
CW_USEDEFAULT, //set all the dimensions to default value
NULL, //no parent window
hMenu, //no menu
hInstance,
NULL); //no parameters to pass
ShowWindow(hwnd, iCmdShow); //display the window on the screen
UpdateWindow(hwnd); //make sure the window is updated correctly
while(GetMessage(&msg, NULL, 0, 0)) //message loop
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
So again, what is the "HINSTANCE hInstance;" "HINSTANCE hThisInstance, HINSTANCE hPrevInstance, "
"wnd.hInstance = hInstance;" etc.
doing?
hopalongcassidy 35 Junior Poster
OK. Frankly, I don't really know what this code is intended to do, because I can't see the entire program. But, I'll try to respond to the questions you have posed.
Firstly, the statement "HINSTANCE hInstance;" simply declares that hInstance is an object of type HINSTANCE. HINSTANCE is probably a class. Classes typically contain properties (variables of either some atomic type (int, char, etc. or complex variables which are classes themselves) and methods (functions that do work on behalf of the class.
From the code, it looks like hInstance is being initialized from hInst. The code that sets hInst is not shown.
hPrevInstance and hThisInstance are parameters of type HINSTANCE that are being passed to WinMain from the code that calls it.
Again, the purpose of all this (what it is intended to accomplish) is unclear. The module that you showed in your example is probably part of a set of modules that make up the entire program. Without seeing all of the code from all of the code from all of the modules, I can't really tell what's going on.
I sort of got, from the tone of your request, that you are really trying to get some basic answers about how C or C++ works and not trying to analyze the inner workings of some complex application. If my hunch is right, I think that you should start by looking at some much simpler code.
Hoppy
twooften 0 Newbie Poster
get some basic answers about how C or C++ works and not trying to analyze the inner workings of some complex application.
Hoppy
You my friend are absolutely right, and with your help you just brought me that little bit closer to understanding.
HINSTANCE is the variable type AND handle
let me play with this...
twooften 0 Newbie Poster
OK, things are lookin' good, I am down to 1 error.
///resource.h
#define IDC_FILE_OPEN 200
#define IDC_FILE_SAVE 201
#define IDC_FILE_CLOSE 202
#define IDC_EDIT_COPY 203
#define IDC_EDIT_CUT 204
#define IDC_EDIT_PASTE 205
#define IDC_VIEW_STANDARD 206
#define IDC_VIEW_CUSTOM 207
#define IDM_MY_MENU 208
I am getting a parse error before '208' error...
Can I not define my menu variable in the resource file...?
If not where?
hopalongcassidy 35 Junior Poster
If you want to learn C or C++, I suggest you look at "www.cprogramming.com". I have not gone through the site, but judging from the table of contents, it might be just what you want. Let me know if it helps you.
Hoppy
twooften 0 Newbie Poster
Well, that site is ok, but I couldn't find ANY info on using "windows.h"
I am always searching for answers on the net, but do you have any idea why I would be getting
///error
c:\Untitled.rc:1: parse error before `208'
///Untitled.rc
#define IDC_FILE_OPEN 200
#define IDC_FILE_SAVE 201
#define IDC_FILE_CLOSE 202
#define IDC_EDIT_COPY 203
#define IDC_EDIT_CUT 204
#define IDC_EDIT_PASTE 205
#define IDC_VIEW_STANDARD 206
#define IDC_VIEW_CUSTOM 207
#define IDM_MY_MENU 208
is there a file that I need to #include in a resource file?
hopalongcassidy 35 Junior Poster
That code looks just fine to me. But sometimes looks can be deceiving. Are you sure that what looks like a space between "IDM_MY_MENU" and "208" is really a space. I remember once having a similar problem. It turned out, after hours of desperation that there was actually an undisplayable character there. If found this out by looking at the file in hex using the debug program in a DOS box. I really can't think of anything else other than that.
Hoppy
GreenDay2001
The program you are discussing is a Windows program which uses Win32 API library.
In resource.h you may get error if you havn't left a blank line at the end.
If you dont know what hInstance or WinMain is, then I doubt you know nothing in Windows programming, so start searching for a tutorial on web or get a nice book.
Here's a nice starting thing for windows api. Go to this link and click the the third one. You need to register before, so I cant give you direct link.
Here are another two:
www.winprog.org/tutorial/
www.relisoft.com/win32/index.htm
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
>> am a newbie to the C++ arena
attempting to write a windows program is NOT a good way to learn c++ language because it is too advanced. Start with something a lot simpler and work your way up from there.
twooften 0 Newbie Poster
I have worked my way up, I can make a dos like prog and what not....
Anyhow, just doing is how I learn.
//main.cpp
#include <windows.h>
#include "resource.h"
#include "Untitled.rc"
LPSTR szClassName = "MyClass";
HINSTANCE hInstance;
LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, HINSTANCE hInst, LPSTR szCmdLine, int iCmdShow)
{
WNDCLASS wnd;
MSG msg;
HWND hwnd;
HMENU hmenu;
hInstance = hInst;
hmenu = LoadMenu(hInst, MAKEINTRESOURCE(ID_MY_MENU));
wnd.style = CS_HREDRAW | CS_VREDRAW; //we will explain this later
wnd.lpfnWndProc = MyWndProc;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.hInstance = hInstance;
wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); //default icon
wnd.hCursor = LoadCursor(NULL, IDC_ARROW); //default arrow mouse cursor
wnd.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1);
wnd.lpszMenuName = MAKEINTRESOURCE(ID_MY_MENU);
wnd.lpszClassName = "MyClass";
if(!RegisterClass(&wnd)) //register the WNDCLASS
{
MessageBox(NULL, "This Program Requires Windows NT",
"Error", MB_OK);
return 0;
}
hwnd = CreateWindow("MyClass",
"A&K's Command Center",
WS_OVERLAPPEDWINDOW, //basic window style
CW_USEDEFAULT,
CW_USEDEFAULT, //set starting point to default value
CW_USEDEFAULT,
CW_USEDEFAULT, //set all the dimensions to default value
NULL, //no parent window
hmenu, //no menu
hInstance,
NULL); //no parameters to pass
ShowWindow(hwnd, iCmdShow); //display the window on the screen
UpdateWindow(hwnd); //make sure the window is updated correctly
while(GetMessage(&msg, NULL, 0, 0)) //message loop
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//resource.h
#define ID_MY_MENU 501
#define IDC_FILE_OPEN 200
#define IDC_FILE_SAVE 201
#define IDC_FILE_CLOSE 202
#define IDC_EDIT_COPY 203
#define IDC_EDIT_CUT 204
#define IDC_EDIT_PASTE 205
#define IDC_VIEW_STANDARD 206
#define IDC_VIEW_CUSTOM 207
//Untitled.rc
#include <windows.h>
#include "resource.h"
ID_MY_MENU MENU DISCARDABLE
BEGIN
POPUP "File"
BEGIN
MENUITEM "Open", IDC_FILE_OPEN
MENUITEM "Save", IDC_FILE_SAVE
MENUITEM "Close", IDC_FILE_CLOSE
END
POPUP "Edit"
BEGIN
MENUITEM "Cut", IDC_EDIT_CUT
MENUITEM "Copy", IDC_EDIT_COPY
MENUITEM "Paste", IDC_EDIT_PASTE
END
POPUP "View"
BEGIN
POPUP "Toolbars"
BEGIN
"Standard", IDC_VIEW_STANDARD
"Custom", IDC_VIEW_CUSTOM
END
END
MENUITEM "Help", IDC_HELP
END
//Errors
Untitled.rc:4: error: expected unqualified-id before numeric constant
Untitled.rc:4: error: expected `,' or `;' before numeric constant
please hellllllp me...?!?
vijayan121 1,152 Posting Virtuoso
remove the #include of the resource script from your main.cpp
//main.cpp
#include <windows.h>
#include "resource.h"
//#include "Untitled.rc"
// ...
twooften 0 Newbie Poster
Brilliant!
I would like to just take the time to show my gratitude, thank you!
I still have other errors, but at least I am over a hump.
twooften 0 Newbie Poster
OK, I have tried, I really have, ever since that last post I have been trying to figure this out on my own, but again... I need help.
//main.cpp
#include <windows.h>
#include "Untitled1.rc"
LPSTR szClassName = "MyClass";
HINSTANCE hInstance;
LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, HINSTANCE hInst, LPSTR szCmdLine, int iCmdShow)
{
WNDCLASS wnd;
MSG msg;
HWND hwnd;
HMENU hmenu;
hInstance = hInst;
hmenu = LoadMenu(hInst, MAKEINTRESOURCE(ID_MENU));
wnd.style = CS_HREDRAW | CS_VREDRAW; //we will explain this later
wnd.lpfnWndProc = MyWndProc;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.hInstance = hInstance;
wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); //default icon
wnd.hCursor = LoadCursor(NULL, IDC_ARROW); //default arrow mouse cursor
wnd.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1);
wnd.lpszMenuName = MAKEINTRESOURCE(ID_MENU);
wnd.lpszClassName = "MyClass";
if(!RegisterClass(&wnd)) //register the WNDCLASS
{
MessageBox(NULL, "This Program Requires Windows NT",
"Error", MB_OK);
return 0;
}
hwnd = CreateWindow(szClassName = "MyClass",
"A&K's Command Center",
WS_OVERLAPPEDWINDOW, //basic window style
CW_USEDEFAULT,
CW_USEDEFAULT, //set starting point to default value
CW_USEDEFAULT,
CW_USEDEFAULT, //set all the dimensions to default value
NULL, //no parent window
hmenu, //no menu
hInstance,
NULL); //no parameters to pass
ShowWindow(hwnd, iCmdShow); //display the window on the screen
UpdateWindow(hwnd); //make sure the window is updated correctly
while(GetMessage(&msg, NULL, 0, 0)) //message loop
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
#include <windows.h>
#include "Untitled1.rc"
ID_MENU MENU DISCARDABLE
BEGIN
POPUP "File"
BEGIN
MENUITEM "Open", IDC_FILE_OPEN
MENUITEM "Save", IDC_FILE_SAVE
MENUITEM "Close", IDC_FILE_CLOSE
END
POPUP "Edit"
BEGIN
MENUITEM "Cut", IDC_EDIT_CUT
MENUITEM "Copy", IDC_EDIT_COPY
MENUITEM "Paste", IDC_EDIT_PASTE
END
POPUP "View"
BEGIN
POPUP "Toolbars"
BEGIN
MENUITEM "Standard", IDC_VIEW_STANDARD
MENUITEM "Custom", IDC_VIEW_CUSTOM
END
END
END
#define ID_MENU 501
#define IDC_FILE_OPEN 200
#define IDC_FILE_SAVE 201
#define IDC_FILE_CLOSE 202
#define IDC_EDIT_COPY 203
#define IDC_EDIT_CUT 204
#define IDC_EDIT_PASTE 205
#define IDC_VIEW_STANDARD 206
#define IDC_VIEW_CUSTOM 207
//Compiler Log
C:/Dev-Cpp/lib/libmingw32.a(main.o)(.text+0x106):main.c: undefined reference to `WinMain@16'
collect2: ld returned 1 exit status
make.exe: *** [Project1.exe] Error 1
Execution terminated
twooften 0 Newbie Poster
Has everyone given up on me?
Cummon! PlEaSe!!
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
are you doing this on your own or are you reading some tutorial? Read chaper 5 of this tutorial for help on how to include resource files in a project.
twooften 0 Newbie Poster
yeah, I am reading the "windows programing" wikibook.
But I am also going through all kinds of other tutorials to find answers.
I just found another one that is good.
http://winprog.org/tutorial/start.html
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.