|
Thanks Failure is Success If we learn from it!!
|
|
|
|
|
I have an MFC application that allows display of different types of objects. It also allows conversion of these objects to other types. The application is an MDI app. I'd like to be able to script the application. If I pass no command line parameters then it runs as a GUI application. If I type command line arguments then I would like it to run as a console application where it performs some work without returning until its done(basically convert one object to another).
I tried putting code in the init_instance but the program returns to the command prompt immediately and the operation is done by a spawned process.
Basically I want my cake and eat it too. I want a console app if command line is present otherwise a GUI application.
I've tried looking for a number of hours a few different times but always fine people talking about command line parsing for an MFC GUI application.
Can someone help me get started, maybe a few pointers?
Thanks
|
|
|
|
|
I have tried that, and now I convinced myself it can't be done, at least if you refuse any flashing window while running in command mode. The reason why is burried deep in the EXE file format, which holds one bit flagging console apps. When the flag is set, your app will run inside an existing console window and perform I/O in there; or when run outside a DOS window, it will create a new console and use it. When a Windows app runs, it will not send its output to an existing console.
So either you set the bit and the app will create a console window, even when you want Forms; or you don't set the bit, and your app will never use the console that launched it.
The "solution" is to build basically the same code twice, and deliver two almost identical EXE files! Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
I believe you can use the AllocConsole[^] function to create and attach a console.
If there are no command line parameters, you can do DoModal and if there are command line parameters you could do an AllocConsole .
Here is an excerpt from the documentation -
This function is primarily used by graphical user interface (GUI) application to create a console window. GUI applications are initialized without a console.
|
|
|
|
|
Member 381289 wrote: I want a console app if command line is present otherwise a GUI application
It is not an MFC problem: a console APP and a GUI app are linked differently and have different entry point from the kernel, that provides them some stdin/stdout services or not.
You can have a console app that has a GUI (it has a main and then creates windows, but it will retain a console until it run - you can eventually hide it) or you can have a GUI app (it has a WinMain ) that has a console (see <<superman>> post down up here.
Note that in MFC GUI apps, WinMain is linked by the MFC library, but exist: it calls the application object methods.
In any case, if you have to be able to hold and redirect the stdin/stdout streams, you must have a console app.
What I typically do in this case, is to breack the application in thre modules:
- A DLL that describe the applicatiomn data and semantics and related "commands"
- An Exe that links the DLL, that provides all the GUI stuff, invoking the DLL "commands"
- An Exe that links the DLL, that provides all the command line parsing stuff, once again invoking thos DLL "commands".
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Hello, I need my MFC DLL to know what it's current path is. Referencing __targv[0] doesn't help because it only gives me the full path to the exe that links to my DLL. Any suggestions?
|
|
|
|
|
HMODULE hmod = GetModuleHandle(TEXT("OLComDll.dll"));
TCHAR szPath[MAX_PATH + 1] = {0};
DWORD dwLen = GetModuleFileHName(hmod, szPath, MAX_PATH);
|
|
|
|
|
I have an existing C++ project, Visual studio 9, running on Windows 7.
I want to write a non-managed piece of code to open a database.
First time round the database won't exist, so I want an error so that I can then run some code to create it.
So far:
DAO doesn't work (it used to in another project)...
m_pDB = new CDaoDatabase(NULL);// use a temporary workspace
// try to open it, not exclusive, not read only the connect arguements left blank (default)
// failure throws an exception (not nice really - rather it returned a false
// fails if database doesn't exist so catch it and then try a create if the user required that.
try
{
opened = true; // assume success
m_pDB->Open(CString("EventsLog.mdb"), false, false);
}
catch(CDaoException* e)
{
opened = false; // wrong assumption, didn't open
e->Delete();
}
if ((!opened) && (createIfRequired))
{
try
{
opened = true; // assume success
m_pDB->Create(CString("EventsLog.mdb"));
CreateActivityRecordStructure();
CreateActivityRecord("0.1.2.3", "Test sensor 1", "Temp OK", 0);
}
catch(CDaoException* e)
{
opened = false; // wrong assumption, didn't open
LPTSTR errors = new TCHAR[100];
e->GetErrorMessage(errors,100,NULL);
// ALWAYS ENDS HERE COMPLAINING IT CAN'T START DAO
AfxMessageBox(errors);
e->Delete();
}
}
_ConnectionPtr pConnection;
hr = pConnection.CreateInstance(__uuidof(Connection));
if (FAILED(hr))
{
return false;
}
_RecordsetPtr recordset;
hr = recordset.CreateInstance(__uuidof(Recordset));
if (FAILED(hr))
{
return false;
}
hr = pConnection->Open(strConnectionString, "", "", adOpenUnspecified);
// this ALWAYS fails regardless of any connection string I have tried - and I've tried a few.
Anything thats to do with 'wrappers around managed classes' just reveals piles of shite about /clr and what it is incompatible with.
I can't find a piece of example code anywhere on the internet that will compile and work.
Surely it is trivial - after all, what I am trying to do (at this stage) is not really any harder than opening a file!
|
|
|
|
|
What is your question?
The code you posted compiles? Runs? Doesn't behave as expected?
Why don't you use ADO ?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
struct hostent *pHost;
int ip,i,j;
string str;
char ipc[6];
str="";
char ctrl[3] = {'\r','\n','\0'};
pHost = gethostbyname("google.com");
for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
{
for(j=0; j<pHost->h_length; j++)
{
ip = pHost->h_addr_list[i][j];
str += itoa(ip,ipc,10);
str += ".";
}
str+=ctrl;
}
alert(str);
I get an alert with four addresses one after the other. i get negative values. for example
-47.85.-103.103
-47.85.-103.104
-47.85.-103.109
-47.85.-103.99
when i tried ip-adress.com i got 74.125.43.104 for google
mine is a shared hosting and the values are less than 128 and i get only one address entry.
i have other alternates to find ip adress of an host and i want to try this too...
is it ok to get negative values... i haven't seen that...Today's Beautiful Moments are
Tomorrow's Beautiful Memories
modified on Monday, February 15, 2010 3:34 PM
|
|
|
|
|
You're seeing negative values because the hostent structure stores the bytes as signed chars. Take a look at inet_ntoa[^] for one way to turn it into a recognisable address.
|
|
|
|
|
You cannot get the IP address of any host on the internet in this way. The gethostbyname() function only performs a lookup in the local hosts database. IP address resolution for non-local hosts is accomplished via DNS servers.txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
Richard MacCutchan wrote: You cannot get the IP address of any host on the internet in this way.
Sure you can. Try this:
struct hostent *host = gethostbyname("www.google.com");
struct in_addr *ptr = (struct in_addr *) host->h_addr_list[0];
int nOct1 = ptr->S_un.S_un_b.s_b1;
int nOct2 = ptr->S_un.S_un_b.s_b2;
int nOct3 = ptr->S_un.S_un_b.s_b3;
int nOct4 = ptr->S_un.S_un_b.s_b4;
...
char *pIPAddr = inet_ntoa(*ptr); "One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
<br />
hostent* localHost;<br />
char* localIP;<br />
localHost = gethostbyname("vikku.info");<br />
localIP = inet_ntoa (*(struct in_addr *)*localHost->h_addr_list);<br />
alert(localIP);<br />
The above code worked well.
I want to find my ip address so that i can make an HTTP request.
I have been doing this by frequently checking online. since my space is a shared hosting the servers ip changes sometimes. So i want to find the ip of my server to make it automatic.
or i think it is best to use getaddrinfo
anyway will the above code holds good? or my first post?
Thanks to the three of you for your replies.
I am experimenting all.Today's Beautiful Moments are
Tomorrow's Beautiful Memories
|
|
|
|
|
Jayapal Chandran wrote: I want to find my ip address...
Call gethostname() followed by gethostbyname() .
Jayapal Chandran wrote: since my space is a shared hosting the servers ip changes sometimes. So i want to find the ip of my server to make it automatic.
Isn't that the whole reason for using a host name rather than an IP address (to not be affected by IP changes)?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
For now this is all i need. May i know the other purposes?
But yet i want to see in practice the working of all these and the purposes behind.
I want to be aware of Networking from some base point. i have started from sockets... and i hope i have to branch more...Today's Beautiful Moments are
Tomorrow's Beautiful Memories
|
|
|
|
|
Jayapal Chandran wrote: May i know the other purposes?
The other purposes of what?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
You have said the following...
~~~~
Isn't that the whole reason for using a host name rather than an IP address (to not be affected by IP changes)?
~~~~
I was not sure the scope of these functions but i thought you were saying there is more that this.
So, i hope you said the reason for using a host name than an ip address is to find the address.
am i confusing?
I should have said YES. but is there any other reason?Today's Beautiful Moments are
Tomorrow's Beautiful Memories
|
|
|
|
|
unsigned char ip,i,j;<br />
pHost = gethostbyname("google.com");<br />
for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )<br />
{<br />
for(j=0; j<pHost->h_length; j++)<br />
{<br />
ip = (unsigned char)pHost->h_addr_list[i][j]; <br />
fprintf(f,"%d %u\r\n",ip,ip);<br />
str += itoa(ip,ipc,10);<br />
str += ".";<br />
}<br />
str+=ctrl;<br />
}<br />
alert(str);
so i changed unsigned to unsigned char(i did not check it properly)
and i could get googles ip addresses but still the one which i saw in
ip-adress.com did not appear. when i used gethostbyaddr i got bom01s01-in-f147.1e100.net which is
also googles for this ip 209.85.153.147 . why only four appears for gethostbyname?
if this is beyond the scope then we can drop this.Today's Beautiful Moments are
Tomorrow's Beautiful Memories
|
|
|
|
|
Jayapal Chandran wrote: ...i could get googles ip addresses but still the one which i saw in
ip-adress.com did not appear.
Your IP address and Google's IP address are not going to match. Why would you think otherwise?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
was just curious. anyway. i need to learn more to understand these behind the scenes.
Meanwhile, i want to know what IDE has options like designing the user interface like in vb (visual studio 6.0). currently i am using DEV-C++ and the compiler is mingw for win32. I have to run the program to see how the width and height has been set for any control i create. I tried VS 6.0 but couldn't design a window. I am asking something like glade. Or do i have to migrate to VS 2003 or greater. I want to run my programs without the .NET framework. As i said i am a novice w.r.t .NET... a little of c# i have did. ?Today's Beautiful Moments are
Tomorrow's Beautiful Memories
|
|
|
|
|
Jayapal Chandran wrote: Meanwhile, i want to know what IDE has options like designing the user interface like in vb (visual studio 6.0)....I tried VS 6.0 but couldn't design a window.
This makes no sense. Unless you did something just completely wrong, any version of VS will work.
Jayapal Chandran wrote: Or do i have to migrate to VS 2003 or greater.
If you don't require MFC, you might want to download VS2008 Express."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Oh. Thanks. I should consider that untill i tend to program the shell. I hope MFC is ideal for that.
meanwhile, i was looking at your other replies and came across turbo c.
I want to stop file creation in windows. I want to try the interrupts like i did in dos. KBD events, mouse, the screen interrupts. and the most needed int 21. besides these i enjoyed the terrific TSR. But how do i do that in window. I am trying to understand the shell concept so that i can write the registry to make my program appear in all context menus like winzip or notepad++. I am still lagging many topics. If you dont mind then please can you drop me some references. These are the things i was doing in Ms-Dos. At one point i cannot read write the far memory that is beyond 64kb. There were DPMI but i thought better to migrate to 32 bit c programs instead of being in 16 bit and trying beyond the scope topics. phew. sorry.Today's Beautiful Moments are
Tomorrow's Beautiful Memories
|
|
|
|
|
Jayapal Chandran wrote: I want to try the interrupts like i did in dos. KBD events, mouse, the screen interrupts. and the most needed int 21.
Windows is going to keep most of that away from you.
Jayapal Chandran wrote: besides these i enjoyed the terrific TSR. But how do i do that in window.
The concept of a TSR in Windows does not exist, at least not how it was defined way back then. Windows has many processes that run "simultaneously." Creating another process is trivial.
Jayapal Chandran wrote: I am trying to understand the shell concept so that i can write the registry to make my program appear in all context menus like winzip or notepad++.
See here.
Jayapal Chandran wrote: At one point i cannot read write the far memory that is beyond 64kb.
No such thing in Windows. Far pointers went away with the 32-bit OS."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Yes, i read somewhere that TSR (the same way) cannot be done. but still. i want to moniter the processes which creates files and certain other processes. Thank you for the registry link. That is the first concept i did after coming here (32bit). I created tabs and radio buttons to restrict the users using certain options like right click on desktop, changing background, folder options. I want to program the explorer shell.
anyway. am i querying too much? beyond the scope of this thread? but despite that i get the answers and you are gladly helping me. How do you take it when there are lots of queries to your personally. It is going like consultation. Thank you a lot. and you can stop me anytime you want but if you really want to.
Next thing is i am writing a tray popup which will ping my server to see for any recent mails. IMAP.
I have experimented using PHP.(because a single function does a lot of work and easy to see the final result but i want a real window program to do that. so i all the networking stuff with php and then i implement in win32). The one draw back is negotiating with the TLS. IF i want to ping my server then that is fine. But if i want to ping google to see my mail status then i have to work with the certificates. This is the idea i assume that i should do if i have to deal with google. I hope c# could have options for doing this. But how about third party libraries for win32 to deal with TLS. Could you drop me some references? meanwhile i am still searching the internet and will continue searching till it is solved. Interesting all these network stuffs. ain't these? . ha.Today's Beautiful Moments are
Tomorrow's Beautiful Memories
|
|
|
|