Click here to Skip to main content
13,899,972 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hello World again, I´m working with my first C++ project, even without a previous experience in this language. So far by now I´ve read that a C++ project MUST have an initial function name "int Main", but this specific one just doesn´t have any.

Why ?. Is it posible to set another entry point for a C++ app ?. How can I do it ?, where can I find this particular function, if it´s a standard method ?

Thanks in advance.

What I have tried:

I have not tried nothing at all about this matter, the app works fine, so I´m very intrigued.
Posted
Updated 29-Jun-16 12:32pm
Comments
Dave Kreskowiak 29-Jun-16 13:49pm
   
Library projects (compile down to .DLL) don't have a Main method.
Miguel Altamirano Morales 29-Jun-16 15:20pm
   
Thanks a lot for answering me, Dave. The App is a native C++ project, developed with Visual Studio 2010, and it builds an .exe file, so I guess it´s not a Library Project, or is it ?.

Greetings from mexico City, sorry about my english.
Jochen Arndt 29-Jun-16 14:28pm
   
You should tell us about your app, the platform / OS, and the used development tools.
Maybe the main function is hidden in some way (it is automatically added by your dev environment and calls another function which is presented as template).
Miguel Altamirano Morales 29-Jun-16 15:23pm
   
Thanks a lot for your advice Jochen. The App is a native C++ project, developed with Visual Studio 2010, runs in Windows 7, and it builds an .exe file.

Greetings from mexico City, sorry about my english.
jeron1 29-Jun-16 15:49pm
   
Have you tried searching for WinMain?
Miguel Altamirano Morales 29-Jun-16 17:29pm
   
The only reference to WinMain I found is this comment:

// Advanced initialization: for overriding default WinMain

followed by a function which I suppose is the one that´s avoiding WinMain. Maybe this is my answer.

Thanks
Ratul Thakur 2-Jul-16 9:33am
   
is it a GUI app or a console application?
the entry point of a simple console app is main() and that of a win32 gui app is WinMain()

and please check your question, its main ,not Main.
Regards
Ratul
Miguel Altamirano Morales 4-Jul-16 10:32am
   
Thanks a lot for your answer, Ratul, and sorry for my grammatical mistake. The app is a MFC project and now I know it has its own starting class. I just achieved what I needed.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

With Windows GUI applications, there is the WinMain entry point (Windows)[^] (see also Walkthrough: Creating Windows Desktop Applications (C++)[^]). This will be called by a hidden main function implementation that is added to your program upon building.

When creating a MFC application even a WinMain implementation is automatically added to your program. The implementation calls InitInstance of your CWinApp derived class which is then the "visible" entry point for MFC applications.

If you are going to write a Windows console program, you must implement a C/C++ style main function. However, there are the Microsoft specific extensions _tmain and wmain which can be used instead (they are again called by a hidden implementation that provides conversions between Unicode and multi byte character strings). See also main: Program Startup[^].
   
Comments
jeron1 29-Jun-16 15:58pm
   
Good post. +5
Miguel Altamirano Morales 29-Jun-16 17:42pm
   
Thanks a lot for your second answer Jochen, it´s very compleat. I´m going to see everything on it. You are a good person and I´m very grateful.

I didn´t see a Reply section for this answer, so I´m sending you this message expecting you´ll be able to see it.
phil.o 30-Jun-16 1:57am
   
Making a comment to one's answer, or replying to an existing comment, and the author will automatically be warned of your message; first in the top bar on this site, and second by mail, if user has activated this option.
So, you did well in replying to Mr Arndt's comment.
Jochen Arndt 30-Jun-16 2:47am
   
Thank you for your feedback and accepting the solution.

As already said by phil.o, you have replied to my answer. With quick answer questions and solutions the button is just named "Have a question or comment".
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

First of all, please see: Entry point — Wikipedia, the free encyclopedia.

Your question suggests that your concern is the return int value. Even if it is not the case, knowing what it does is something good to know.

This is mostly a matter of history. There is a tradition for entry-level functions to return 0 in all cases except the cases of abnormal termination. In all (or almost all) OS, there are the libraries uses to check up the status of the process. When a process is terminated, your code can read this value from the status. Also, when the application is executed synchronously in one or another kind of a batch file, the starting command can return this value, which can be assigned to some kind of the batch/script variable which can further be used, for example, to fork further execution. At the same time, this is just the tradition or culture, not any kind of standard. The tradition to return 0 is stable enough, and all other values are totally application-dependent; if they are essential, they can be described in application-specific documentation. If the entry point function used the return is void, 0 is still returned to the runtime system; this is the default.

With time, the importance of this technique goes down; most applications simply ignore the possibility to return anything. There are two important reasons for that: structural exception handling and then graphics UI, and, to certain extent, threading. The dreaded technique of returning "status" from the function is eliminated; using it would be just silly. Exceptions are much more powerful. Normally, all applications catch all exceptions in all threads and show much more informative diagnostics of the problems in the application itself. Throwing the exceptions into the runtime system, execution environment, is relatively rare, used only in the simplest application or are indicative of low-tech. Moreover, event-oriented UI applications (that means, nearly all of them, if you take only the decent ones) catch all exceptions in the UI main event and show the problem diagnostics immediately.

—SA
   
Comments
Miguel Altamirano Morales 29-Jun-16 18:46pm
   
Thank you very much Sergey, and yes, it´s good and very valuable to know what the return value of the entry point does.
My very detailed concern in what I´m doing is to know the right flow (or thread ?) of execution of this app. What I have to do is to display an image, like a full portrait on the screen that should appear right before the first GUI dialog. And I say GUI dialog because I can see now that this kind of projects don´t have windows forms.
As you suppose, I don´t know WHERE to display this portrait (PORTADA in spanish) or WHERE to include it in order to be properly displayed at runtime.
Miguel Altamirano Morales 29-Jun-16 18:55pm
   
I feel a litlle bit ashamed because maybe I´m asking a very naive question, specially for people like you, but please, understand this is my very first assignation with a native C++ project. I am lucky I could be able to solve a few things in two weeks by now, but it gets more complicated for me each day. At least I honestly gave a warning about it to my clientes.
Maybe they hired me because they could´t find anybody else, but I know for sure I´ll get what they want, I just only need time for investigation.
Sergey Alexandrovich Kryukov 29-Jun-16 20:20pm
   
There is nothing wrong with this question. We have a lot of stupid questions, but they are usually stupid due to the logical contradiction, most typically wrong baseless assumptions and inadequate ambitions; in other way, false knowledge is stupid, but lack of knowledge isn't. Your question is nothing like that; it's natural on the certain level of learning. Please don't hesitate to ask.

Here is the thing: answering your question heavily depends on 1) your exact platform; 2) UI/graphics framework/library you are using. So answering in too much detail won't make sense until you specify that. Most usual approach is this: the UI is event oriented; the OS provides an event used to handle some graphics rendering event. You handle this event, use the graphical context reference/pointer/handler to draw an image or something else. The OS makes sure there are no redundant calls. For example, if you hide your window behind some some other window or an edge of the screen and then show again, the re-rendering event may be triggered, but not if you simply move the window around. This mechanism may seem overcomplicated, but this is not so; it's very reasonable and efficient. Now, some libraries may do it internally, so you only need to place some object in some container, then it will be rendered. It seems to be much simpler, but it's not necessarily better. Also, modern libraries transparently use hardware acceleration.

With these hints of the background, you would need to ask a separate question, as soon as you provide the required information. If you want me to participate, please give me a link to your new question by adding a comment to this comment.

—SA
Miguel Altamirano Morales 30-Jun-16 10:25am
   
Good morning Sergey (it´s 9:14 am in México City).

The platform I´m working with is Visual Studio 2010, the App is a native C++ one, but I still cannot determine what kind of project is, (if it´s a MFC or a Win32), but it generates an .EXE file, not a .DLL, though it seems to have MFC classes. How can I check this for sure ?

About what you ask for the UI/graphics framework, I just don´t know, let me see if I can solve this and I´ll tell you.

Thank you Sergey.

Спасибо !!!
Sergey Alexandrovich Kryukov 30-Jun-16 10:42am
   
That is enough, thank you.

In this environment, you use raw Windows API (perhaps with a very thing MFC wrapper (see MFC documentation: CImage), or combining it with raw Windows API). The approach I described in my previous comment fully applies. The role of the event handler is played by your handler of the event WM_PAINT. Look the documentation on this event and handling it. See also the Windows API methods Invalidate*..., for completeness.

If you need more detail or cannot find all ends, please post a separate question, on a separate question page. You can give me a link to this question by commenting on this comment; I'll try to answer quickly.

—SA
Miguel Altamirano Morales 30-Jun-16 11:01am
   
Thank you, I´´ll do so
Miguel Altamirano Morales 30-Jun-16 12:26pm
   
It´s a MFC project, it´s a fact.
Miguel Altamirano Morales 30-Jun-16 13:48pm
   
You´re not going to believe this: I already solved the problem !!!.

My mistake (I believe) is to think in "flow" and "NET" terms about this app. You know: "Where and when do I insert the image".

What I did was to create a new app using the wizard for a Win32 project and see all the files that it generated automatically. Reviewing them and the "Readme.text" I took a good idea about how does it work and what was what I should consider the entry point for the app. Then I looked into the app I´m working on and noticed that is a MFC project, so everything relies on the "nameofproject..h" and .cpp files. I only uncommented a few lines commented by another programmer and included a header and a source file to handle the class.

Thanks a lot for your kind support and I hope we could get in touch again, it´s very likely I continue doing researching via the CodeProject, it´s a very valuable resource for programmers in an understanding process like me in this particular topic (C++).

God Bless you Sergey.
Sergey Alexandrovich Kryukov 30-Jun-16 15:54pm
   
You are very welcome.
Good luck, call again.
—SA
Karthik_Mahalingam 26-Jul-16 5:40am
   
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190306.1 | Last Updated 29 Jun 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100