parameters to windows exe

May 11, 2008
22,149
1,401
126
Helllo everybody.

I have a question. I want to pass arguments to a program.
I want to do this because i want to write a small program that i can call upon from another program.
This is not a console program but a win32 gui program.
For a console version i know what to do, but i want to pass arguments of type string to a windows win32 executable. I am programming in C/C++.

I am sure there is some win32 function for it or some very simple way, but i have overlooked it, cause i cannot find it :'(

Anybody have an idea ?

Thank you in advance...
 
May 11, 2008
22,149
1,401
126
If it is any help :

I have sz_commmandline_arguments at a fixed size for now. But i plan to use malloc and free to make sure no wrong situations can occur.

The bold part is to do at least a little bit of parsing for now.



//

Global variable
char sz_commmandline_arguments[256];

Code

more code




**********************************************************************************************************************
// MAIN PROGRAMLOOP

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
MSG msg;
WNDCLASSEX wndclass ;
int i_text_length = 0;
int i = 0;


// Main window class declaratie.
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = 0;
wndclass.lpszClassName = szAppName ;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if (!RegisterClassEx (&wndclass))
{
MessageBox (NULL,"This program requires Windows NT!",szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindowEx (
WS_EX_APPWINDOW,
szAppName, // window class name
"LEDFUNCTIONS", // window caption
WS_OVERLAPPEDWINDOW,// | WS_VSCROLL | WS_HSCROLL, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL // creation parameters
);


if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",MB_ICONEXCLAMATION | MB_OK);
return 0;
}
DeleteObject ((HGDIOBJ) SetClassLong (hwnd, GCL_HBRBACKGROUND,(LONG) CreateSolidBrush (RGB (212,208 ,200))));
InvalidateRect (hwnd, NULL, TRUE);


ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);

for(i = 0; i < 256;i++); //Init array.
{
sz_commmandline_arguments = 0;
ch_textbuffer = 0;
}

i_text_length = strlen(szCmdLine); //If no arguments, no need to copy data.
if( i_text_length > 0)
{
for(i = 0;i < i_text_length;i++)
{
sz_commmandline_arguments = szCmdLine;
}
}



//**********************************************************************************************************************
// messageloop .
while (GetMessage (&msg, NULL, 0, 0) > 0) // > 0 means window messages.
{ // -1 equals error. 0 is de result of the MQ : PostQuitMessage.
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
 
Last edited:

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Additionally, though somewhat unrelated to your question, in the first bolded loop at the bottom, you mention that you were going to malloc() the buffer and then instantiate.

To save yourself some time and keep the code clean, you should look into using calloc().

-Kevin
 
May 11, 2008
22,149
1,401
126
Additionally, though somewhat unrelated to your question, in the first bolded loop at the bottom, you mention that you were going to malloc() the buffer and then instantiate.

To save yourself some time and keep the code clean, you should look into using calloc().

-Kevin


Indeed, thank you for the suggestion. :)
It does save some time and has some advantages over malloc.
I have used it and am busy changing all mallocs to callocs in my code.
My program is getting close to the finish :hmm:. Pretty soon i should have a beta version that i can use. It generates makefile and linker scripts, i need to be able to program my ARM microcontroller . It is now still pretty basic but when i understand more of the typical linker/compiler functions i will expand the program with more usefull functions.


But what i do not get, is why borland has borland c instead of just ansi c.

I use borland c ++ builder 5.5 for x86 to write the program ( It is my second program :awe:) (yeah i know it's old but it is all i have and legally ok , meaning it is a purchased student version.) It's either that or CVI and with CVI i have to supply a runtime which i do not want. The .net language i find for the moment way to slow, so i will stick to c for a long time (and because i want to program microcontrollers in c).

The thing is, when i put the advanced compiler settings to ansi c compliant, i get a massive amount of errors and warnings about for example commsctrl.h and other supplied windows header files. Why is this done ? Why are these header files not ansi c compliant ? Bad programming practices or a limitation to ansi c that has to creatively solved ? Do you perhaps know why or anybody does ?


I added a picture for convenience...

http://i37.photobucket.com/albums/e81/WilliamGaatjes/w-arm.jpg

The name is still changing though...
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,284
138
106
look into using mingw, or even visual C++ express edition (it is not .net based.) Both will generate better code then that old version of borderland (or really, just about any version of borderland) and both are free.
 
May 11, 2008
22,149
1,401
126
look into using mingw, or even visual C++ express edition (it is not .net based.) Both will generate better code then that old version of borderland (or really, just about any version of borderland) and both are free.

I have heard of visual c++ express. But then it will be very likely that my current code needs to be partially rewritten. Although it will not be much. I have totally refused that T-form from borderland :).
I created everything from within the program. I have heard that visual c++ is a lot like CVI. just drag and drop all your controls and you are ready to go...

1 other question, why do i have to register the visual c++ studio express within 30 days if it is free ?
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
I have heard of visual c++ express. But then it will be very likely that my current code needs to be partially rewritten. Although it will not be much. I have totally refused that T-form from borderland :).
I created everything from within the program. I have heard that visual c++ is a lot like CVI. just drag and drop all your controls and you are ready to go...

1 other question, why do i have to register the visual c++ studio express within 30 days if it is free ?
Registration is free. Just microsofts way of seeing how many people are using their products.

The visual C++ compiler is (mostly) ansi compilant. So, if you created windows using the "CreateWindow" function, they should still work with Visual C++ Express doesn't comes with MFC support, but does have a limited visual forms creation utility.

If you, for the most part, avoided using T"whatever" from borderland, then a switch should be fairly straight forward.

<- Not a big borderland fan. I don't like their compilers.
 
May 11, 2008
22,149
1,401
126
Registration is free. Just microsofts way of seeing how many people are using their products.

The visual C++ compiler is (mostly) ansi compilant. So, if you created windows using the "CreateWindow" function, they should still work with Visual C++ Express doesn't comes with MFC support, but does have a limited visual forms creation utility.

If you, for the most part, avoided using T"whatever" from borderland, then a switch should be fairly straight forward.

<- Not a big borderland fan. I don't like their compilers.


Thank you for answering.

When i finish this program (almost done ) i will make the transition to visual c++. I bought the book "programming windows 5th edition" from Charles Petzold.
But unfortunately the examples do not work under borland. I had to do a lot of casting and fixing to get examples working in the past. Ever since that disappointment i just try to understand the program and then use the described win32 function. The win32 help program i have is a great help though.
 
May 11, 2008
22,149
1,401
126
Hello COGMAN,

I have installed visual C++ express edition.

But i have a question.
With visual c++ express i get the exclamation that i cannot edit the resource with this visual studio express SKU.

I find it kind of typical microsoft( giving something for free that is very crippled) that i cannot edit the resource rc file directly. I tried to edit the resource file in a different text editor and i can at least add or change menu items meaning that at the moment it will still compile. I hope i do not find more cripple action that can cause my program not to compile. Do you know of any other limitations besides MFC , ATL and the resource file.

Thank you in advance...
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Hello COGMAN,

I have installed visual C++ express edition.

But i have a question.
With visual c++ express i get the exclamation that i cannot edit the resource with this visual studio express SKU.

I find it kind of typical microsoft( giving something for free that is very crippled) that i cannot edit the resource rc file directly. I tried to edit the resource file in a different text editor and i can at least add or change menu items meaning that at the moment it will still compile. I hope i do not find more cripple action that can cause my program not to compile. Do you know of any other limitations besides MFC , ATL and the resource file.

Thank you in advance...

Besides that which you've mentioned, I don't know any other cripples that the express edition introduces (But hey, it is free, Why should the free edition be uncrippled compared to the full edition?)

Either way, no free compiler/IDE that I know of supports RC editing, so really, it isn't that big of an issue. Making menus by hand, while kind of hard to find information on, isn't THAT bad.
 
May 11, 2008
22,149
1,401
126
Besides that which you've mentioned, I don't know any other cripples that the express edition introduces (But hey, it is free, Why should the free edition be uncrippled compared to the full edition?)

I am not saying that it should be full functional, i just find it amusing that i cannot edit the rc file in the VS 2008 editor but i can edit in an another editor like pspad and any change i make still works. It is just a visual limitation. But i will gladly pay 50 euro's for an express version if it leaves out artificial limitations like this one. Besides, if you change the icon, the new icon is added to your program as well. Thus it seems it is only the editor that inhibits you to edit the rc file directly. A minor inconvenience though looking with hindsight.


Either way, no free compiler/IDE that I know of supports RC editing, so really, it isn't that big of an issue. Making menus by hand, while kind of hard to find information on, isn't THAT bad.

Afcourse it is not. It seems to work great. But i already made an rc file where i still have to make small adjustments.

I tried it so far. I noticed that the winmain is changed. Why is that ?
I will have to do a lot of reading to get it all working. I have at the moment an avalanche of errors. I changed the setting of the precompiled headers to : Not creating precompiled headers. But i still get the same errors. The compiler seems to desire some different format for the header files. The error is :

"mafigen_filesystem.cpp(83) : error C2228: left of '.lpstrFile' must have class/struct/union"

I changed again the setting of the precompiled headers to :

But now i get all these kind of errors :
drawfunctions.cpp(738) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
"

I did add the .h file to the stdafx.h but it still does not work. The same error.
What am i doing wrong here ?


i have also an insane amount of typedef errors. And that is funny, because all errors are about types defined in windows.h and other windows header files. For instance, HWND and HANDLE are the same yet it does not understand HWND, another is HGDIOBJ and HBRUSH. I am using the specified handles. Yet HBRUSH is unknown... What am i doing wrong here ?

Another question , I only want to use 8 bit characters. I assume from my WIN32 Petzold book that i use 8 bit characters by default. Is this still the case with VS C++ 2008 ?
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,284
138
106
ah yes, I forgot about good ole stdafx.h, I have NO clue why MS forces it, but they do, you have to have stdafx.h at the top of your first compiled program, ms ignores everything until after stdafx.h

Its arbitrary, non-standard, but not really too big of an issue.
 
May 11, 2008
22,149
1,401
126
ah yes, I forgot about good ole stdafx.h, I have NO clue why MS forces it, but they do, you have to have stdafx.h at the top of your first compiled program, ms ignores everything until after stdafx.h

Its arbitrary, non-standard, but not really too big of an issue.

Well, unfortunately i already did replace everything from my headers to that header.

Here is the stdafx.h :

Code:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>

// C RunTime Header Files

#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

// TODO: reference additional headers your program requires here

#include <stdio.h>
#include <commctrl.h>
#include <shlobj.h>

#include "resource.h"
#include "drawfunctions.h"
#include "linkerscript_generator.h"
#include "makefilescript_generator.h"
#include "mafigen_filesystem.h"
#include "menubar_controls.h"
#include "mafigen_cfg.h"
#include "WARMbit_definitions.h"

and placed the stdafx.h at my main.cpp as first header (tried dozens of combinations but i do not know why it just does not work.)
I will have to see why it does not work, otherwise i am maybe better of finishing my program in borland if i have to learn all about visual studio. I thought all the code would just work but the winmain is different too.
I get all these errors about unkown types. All windows types. my own functions all seem to be okay. Kind of frustating to be honest. Oh well, i better sleep over it if i decide to figure it out or stay with borland.
 
Last edited:
May 11, 2008
22,149
1,401
126
I looked into it a little more :

It seems stdafx.h is not really needed. If you want to use precompiled headers because you have a very large build every time then prcompiled header are handy. If you do not need precompiled headers, just turn it off in the configuration of your project. I have turned it off again, since my program is not that big. And i do not need to recompile for every line of code. At the moment i am looking if i can exclude it all together with target ver.h. I always had that stuff in the main.h header. If i can figure out my last 9 errors i will see if it still work without stdafx.h and i am betting it will if not using precompiled headers..

I removed all the "#pragma once" . I do not see the use of it.
I use and prefer ifdef / ifndef to determine if a header is already compiled.

I managed to reduce the amount of errors from 46 to 15 with removing
WIN32_LEAN_AND_MEAN. I remember i had this problem also in borland. With this define i cannot get any of the example programs to run. I assume it leaves out rarely used win32 api functions. Functions i seem to use.

Then i started replacing the HWND types to HANDLE types and the HBRUSH types to HGDIOBJ and i managed to get the errors down to 9.

This is an example of the errors i am still working on.
All errors are alike.
w-arm.cpp(253) : error C2664: 'UpdateWindow' : cannot convert parameter 1 from 'HANDLE' to 'HWND'

There is 1 thing i really do not understand. It is the win32 api . That api supposed to be a standard. Then why do al these errors pop up about not knowing it's own types ? If it does not function directly, the programmers at microsoft should have made some conditional code in the windows headers to maintain compatibility...

EDIT :

You are right, indeed the VS editor can only do it's magic with the stdafx.h file.

When it does not see it, it cannot interpret it's own windows.h files. o_O
 
Last edited:
May 11, 2008
22,149
1,401
126
Yahooo.

After doing groceries and breakfast, i found the errors i made.

In borland , the type checking is not that specific and i by accident passed a HANDLE while in the called function a HWND was expected. After correcting this, i only have to figure out what goes wrong with the headerfiles and what is up with HGDIOBJ and HBRUSH. Probably the same issue as with the HANDLE errors.. :D


EDIT:
I have 1 error left. All i need to do now is convert my old winmain to the new version and it should function.

Code:
    //  Main window class declaration.
    wndclass.cbSize         = sizeof(WNDCLASSEX);
    wndclass.style          = CS_HREDRAW | CS_VREDRAW ;
    wndclass.lpfnWndProc    = [B]WndProc[/B] ;
    wndclass.cbClsExtra     = 0 ;
    wndclass.cbWndExtra     = 0 ;
    wndclass.hInstance      = hInstance ;
    wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground  = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndclass.lpszMenuName   = MAKEINTRESOURCE(ID_MAINMENU);
    wndclass.lpszClassName  = szAppName ;
    wndclass.hIcon          = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_CEICONlarge));
    wndclass.hIconSm        = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_CEICONsmall), IMAGE_ICON, 16, 16, 0);

VS does not seem to understand the WndProc.
 
Last edited:

Kirby

Lifer
Apr 10, 2006
12,028
2
0
I learned c++ on Borland C++ 5.01 back in high school and did some stuff in BGI. Good times, although I can't say I miss it. :p
 
May 11, 2008
22,149
1,401
126
It saddens me that i have to do a large rewrite to be able to compile my program under VS2008. But what must be done must be done. The differences are to big under borland5.5 and visual studio 2008.
Oh well, at least it will be easier to make it work under windows 7...

There is so much difference... Planning is not the strongest point of microsoft.
Their way is to just make everything obsolete and sell the same wine with 1 new ingredient more expensive...

A big advantage from borland over VS is that borland actually notifies you that you need a certain xxx.h file for a certain function to work. VS does not do this. Is there an option to turn this on in visual studio ?

With borland when you need a certain library, you get a notification. With visual studio google is your only friend. When you want to use the common controls, do not forget to add comctl32.lib to the linker input.

And unicode is really annoying. I have a lot of ascii code. When i turn of unicode in the project properties, i get an obscure error "MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
" Why o why...
 
Last edited:
May 11, 2008
22,149
1,401
126
Great, When i turn unicode on or off, VS keeps generating the same error while before the error only appeared when i turned unicode off in the general properties of the project.

MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
E:\My docs\Visual Studio 2008\Projects\W-ARMV2\Debug\W-ARMV2.exe : fatal error LNK1120: 1 unresolved externals


A nice tip i read from a msdn moderator, apparently somebody else had the same issue :

For these types of errors, it's easier to just start all over again, choose the right type of project from the New Project Wizard and copy your old code into there.
:eek:

It is the same old R*R*R from some windows "administrators" : Retry, Reset, Reinstall . :D:D:D

Does anybody know how to solve this without having to start all over again ?
 
May 11, 2008
22,149
1,401
126
I checked my linker settings, it is a windows application and not a console application as i have correctly selected when i started the new project.

My linker settings are : SubSystem [Windows/Subsystem:windows]

How is it possible that changing 1 setting a few times in a row with respect to unicode causes these kind of errors for the linker ? I did a clean project a few times in between and afcourse the build project button but that should not be cause of such an obscure error about winmain. What is going wrong here ?

I am not alone, the internet is filled with people having problems with this error. The only problem is that the cause is for all those people different.
 
May 11, 2008
22,149
1,401
126
I have started from scratch.

The following amazing thing happened. I started a new win 32 application project as i did before. And checked if it would compile and run(It did). I copied the contents of my original resource *.rc file to the new project, but made 1 type error. Because of this type error i was able to edit the resource file in the visual studio editor(It was invoked by the IDE ), while this is impossible to do it directly in the visual studio editor. However, after editing and saving the resource file *.rc. I got the same error :

MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
E:\My docs\Visual Studio 2008\Projects\W-ARMV4\Debug\W-ARMV4.exe : fatal error LNK1120: 1 unresolved externals

This makes me almost believe it is a forced error because i have edited the resource file. For some reason the linker makes an error after editing the resource file. But why i get this now and before (as posted above) i get this error after changing a unicode setting, well that surprises me.

When i started another new project i noticed that when i change the project settings, even while pressing apply and ok, these settings are not reflected in the build.

Visual studio C++ express 2008 is worthless it seems when it behaves like this. Does anybody know where this erratic behaviour comes from ?

My computer is virus free and stable as a horse for 2 years now. I had 2 crashes( blue screen) a few months ago that happened because of some richvideo.exe error combined with a usb remote from microsoft combined with cyberlink dvd. Since i have disabled the richvideo.exe service the crashes where solved...

But i have installed visual studio just a couple of weeks ago. Why do i get this erratic behaviour from visual studio ? Is it really that unstable or is there another answer ? I have used borland and never had this behaviour. I used other compilers for embedded controllers and have never experienced these kinds of problems. If i am doing something wrong, what am i doing wrong ?
Or is visual studio 2008 C++ express just a (forced because it is free) load of crap ?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Visual Studio is not unstable, and it's not a load of crap, even in the free versions. You've always been able to edit resource files. They're just xml of a sort, and previously they were just text files that got fed to the resource compiler.

The short answer is you screwed something up, but I can't tell from your message what that is.

However, any time you get an unresolved external from the linker the problem is that you're missing a library or have a misspelled import name. This can happen in C++ when name mangling is performed inconsistently, and there are other reasons short of simply missing a link directive (though it could be that as well).
 
May 11, 2008
22,149
1,401
126
Visual Studio is not unstable, and it's not a load of crap, even in the free versions. You've always been able to edit resource files. They're just xml of a sort, and previously they were just text files that got fed to the resource compiler.

Then why when i want to edit it it shows the message that editing is not supported in that VS SKU ? I am using the Visual Studio 2008 C++ express you know. I think you have the different versions mixed up. You can only edit the resource file with an external text editor. That will work but as i have encountered and written above can cause strange errors.

The short answer is you screwed something up, but I can't tell from your message what that is.
According to non standard c logic i am sure i have.


However, any time you get an unresolved external from the linker the problem is that you're missing a library or have a misspelled import name. This can happen in C++ when name mangling is performed inconsistently, and there are other reasons short of simply missing a link directive (though it could be that as well).

Well, this is something i am missing in the VS version. When i use a certain function, and i have not added the header, borland builder asks me if i want to include the header. I can then assume borland also imports the libraries, because it just works. And why does VS not just provide me with clues what i have forgotten ? If i get an obscure message i really do not know what i have done wrong. Especially since this message is used for a lot of different errors. To use a higher programming language is to release yourself from the burden of having to keep track of every bit of code.

Where do i turn this function on in visual studio ? Or where can i find more detailed information what the linker seems to mis ? I find it kind of strange that i have to keep track of which win32 api libraries i use on a windows IDE from microsoft and that i have to include them manually?


http://stackoverflow.com/questions/86562/what-is-missing-in-the-visual-studio-express-editions


http://www.microsoft.com/downloads/...B0-B575-47AB-9FD8-4EE067BB3A37&displaylang=en

EDIT : By the way, the borland builder i use is an old V5.5 student version.
 
Last edited: