Download a binary, close the current one and launch the new one - c++

I'm sorry if this question exists, I can't specifically find this case when googling it up, and to be frank I'm even unsure what tags to use.
I am developping a solution, with a launcher. There is a problem however: my team doesn't want to make the launcher and the actual program two different binary files, which I believe is a conception mistake.
Either way they won't change their mind so we're going with that.
My "launcher" part of the program currently succesfully fetches new versions on our server.
I however want to be able to, as soon as the new version is downloaded, quit the current binary, delete the old one, launch the new one.
It has, ontop of that, to be multiplatform.
Now, if I recall correctly I think that on linux, it's very possible to delete a binary on the hard drive as it runs in memory. I'm really, really unsure if it can be done on Windows however.
Is it possible to delete a program that is currently running on windows, with an instruction to launch the new one somehow?
Tell me what you think. In my mind it really sounds unfeasable, but if it can be done, I would really like to know how.
At the very least, if it can't be done on either OS, I'll just show my team the answers.
Thank you in advance, sorry about the very vague "what if" questions and such.

I agree with you, it's probably not a good idea. The question was raised before.
How can a C++ binary replace itself?
The answers includes a description how to do this on Windows, where the use of "shim" allows you to do what you described with a launcher, without actually having to develop this launcher yourself.

Related

Any way or ideas to protect or sign source code?

This is probably a strange question. My project involves a few other people that need to work on the code too. I'm not sure how careful they would be with it and I don't want it to leak. For this reason I split it into 2 parts, one is in the form of a library, the rest just plain source code. There is one other guy that needs everything so he also has the source to the library. I don't want this guy to make any changes to the library. I put in a version number that gets printed when everything is running but I have no way of knowing (from looking at logs) if the library was authentic (from me only).
I was hoping there is some way I can use a public-private-key signature or something like this but against what? I probably can't just calculate an MD5 hash either because the linker probably puts the library function in different places all the time.
I realize it's probably not feasible to sign and verify source code but I would be curious to hear if anybody has any ideas.
You can use one of the VCS (version control systems) listed here.
By my experience you can use Github, it is easy to work with.

How to make a screensaver for windows 7 c++

I realize the above is not so much a specific question, and I have no code to speak of, but I feel that others also have been unable to accomplish this feat - it strikes me as one of those problems where case can be everything.
So here lies my problem. I have made the screensaver, per say, of what actually runs. Essentially what I want to do is, make it run, by itself, off the timer... and perhaps some of the associated preview window features, because we need to get it selected. Problem is, either the resources regarding this are scare, by case, or my Google-fu is just not working today. (virtually all screensaver tagged questions I found on SO were about users who either knew how to make a screensaver, or those who wanted to not make one but check on one, i.e if it started running during program execution.
Some info: Trying to make it for windows 7 (using windows 7 if it matters) using C++.
What I am asking is, how to get from my current point, where I have a normal program (i.e, start in main, do some graphics...) which loops fine by itself, but I am trying to figure out, how to make it an actual screensaver - which, unfortunately, cannot be solved simply by renaming it .scr.
Thanks if you can provide any information.
PS: I am pretty new here, and don't know to much about most things, including using SO. My last question was downvoted (I will admit, it was bad), and I am now determined to ask good questions.
this should help:
http://www.cityintherain.com/howtoscr.html
and another (with downloadable example code)
http://www.codeguru.com/cpp/g-m/opengl/article.php/c2695/OpenGL-screen-savers.htm
i hope you're familiar with the win32 api

Is there anything to spell check code that is displayed in the GUI?

I am a student programmer using Qt to build several GUI applications for my company and embarrassingly I have a tendency to misspell words from time to time. Obviously it's not really much of an issue for my code as the compiler would be the first to let me know but is there any way to automatically check the spelling of strings or data that is displayed?
I really would like to know if Qt offers anything; boy that would be nice. I didn't see anything in the documentation though. I don't often have this problem but when it does happen it's so embarrassing.
Ever since I installed VisualAssist, it highlights spelling, even in comments.
Not sure what it looks like or if it only works with VS... I am using it in plain c++.
http://blog.qt.nokia.com/2012/05/22/qt-4-8-2-and-visual-studio-add-in-1-1-11-released/
I hope this helps.

C++ - writing directly to memory(Kernel)

I have asked pretty much the same thing before, but my post got deleted for some reason.
Anyway, I'm trying to use C++ and write a program that allows me to access directly to the memory and write stuff to it. I heard I need to do something with the kernel as that is a "bridge" that connects the OS and application (I think). Is there anyway I can download some sdk for the kernel?
I've said this in my previous post (which got deleted after some time) that the reason to this is because I want to try and crash windows 7 as my professor at university asked me to. (so please don't ask me stuff like "why do you want to crash windows?" or something along those lines...)
Any help will be greatly appreciated.
If you're interesting in working with the kernel, you're likely looking for the Windows Driver Kit, found here:
http://msdn.microsoft.com/en-us/windows/hardware/gg487428
It has a variety of lower-level tools and headers to help you write drivers and other kernel-mode code.
Typical programs obviously don't have carte blanche access to memory, while drivers have more control (although I would guess they use the system's memory management as well, not entirely sure). You'll find more information in the WDK.
Write a driver, make it crash. Of course, in only very specific circumstances will this actually make Windows 7 crash (because, unlike the public opinion, it wasn't written by total idiots).
You can use the RtlSetProcessIsCritical function in order to flag that your process is critical for system operations.
If your process is terminated (for example if your application calls ExitProcess) a bluescreen will appear. In order to use this function you need to enable the SE_DEBUG_NAME privilege.

Super Robust as chrome c++ and portable - tips - help - comments [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
We are producing a portable code (win+macOs) and we are looking at how to make the code more rubust as it crashes every so often... (overflows or bad initializations usually) :-(
I was reading that Google Chrome uses a process for every tab so if something goes wrong then the program does not crash compleatelly, only that tab. I think that is quite neat, so i might give it a go!
So i was wondering if someone has some tips, help, reading list, comment, or something that can help me build more rubust c++ code (portable is always better).
In the same topic i was also wondering if there is a portable library for processes (like boost)?
Well many Thanks.
I've developed on numerous multi-platform C++ apps (the largest being 1.5M lines of code and running on 7 platforms -- AIX, HP-UX PA-RISC, HP-UX Itanium, Solaris, Linux, Windows, OS X). You actually have two entirely different issues in your post.
Instability. Your code is not stable. Fix it.
Use unit tests to find logic problems before they kill you.
Use debuggers to find out what's causing the crashes if it's not obvious.
Use boost and similar libraries. In particular, the pointer types will help you avoid memory leaks.
Cross-platform coding.
Again, use libraries that are designed for this when possible. Particularly for any GUI bits.
Use standards (e.g. ANSI vs gcc/MSVC, POSIX threads vs Unix-specific thread models, etc) as much as possible, even if it requires a bit more work. Minimizing your platform specific code means less overall work, and fewer APIs to learn.
Isolate, isolate, isolate. Avoid in-line #ifdefs for different platforms as much as possible. Instead, stick platform specific code into its own header/source/class and use your build system and #includes to get the right code. This helps keep the code clean and readable.
Use the C99 integer types if at all possible instead of "long", "int", "short", etc -- otherwise it will bite you when you move from a 32-bit platform to a 64-bit one and longs suddenly change from 4 bytes to 8 bytes. And if that's ever written to the network/disk/etc then you'll run into incompatibility between platforms.
Personally, I'd stabilize the code first (without adding any more features) and then deal with the cross-platform issues, but that's up to you. Note that Visual Studio has an excellent debugger (the code base mentioned above was ported to Windows just for that reason).
The Chrome answer is more about failure mitigation and not about code quality. Doing what Chrome is doing is admitting defeat.
Better QA that is more than just programmer testing their own work.
Unit testing
Regression testing
Read up on best practices that other
companies use.
To be blunt, if your software is crashing often due to overflows and bad initializations, then you have a very basic programming quality problem that isn't going to be easily fixed. That sounds a hash and mean, that isn't my intent. My point is that the problem with the bad code has to be your primary concern (which I'm sure it is). Things like Chrome or liberal use to exception handling to catch program flaw are only distracting you from the real problem.
You don't mention what the target project is; having a process per-tab does not necessarily mean more "robust" code at all. You should aim to write solid code with tests regardless of portability - just read about writing good C++ code :)
As for the portability section, make sure you are testing on both platforms from day one and ensure that no new code is written until platform-specific problems are solved.
You really, really don't want to do what Chrome is doing, it requires a process manager which is probably WAY overkill for what you want.
You should investigate using smart pointers from Boost or another tool that will provide reference counting or garbage collection for C++.
Alternatively, if you are frequently crashing you might want to perhaps consider writing non-performance critical parts of your application in a scripting language that has C++ bindings.
Scott Meyers' Effective C++ and More Effective C++ are very good, and fun to read.
Steve McConnell's Code Complete is a favorite of many, including Jeff Atwood.
The Boost libraries are probably an excellent choice. One project where I work uses them. I've only used WIN32 threading myself.
I agree with Torlack.
Bad initialization or overflows are signs of poor quality code.
Google did it that way because sometimes, there was no way to control the code that was executed in a page (because of faulty plugins, etc.). So if you're using low quality plug ins (it happens), perhaps the Google solution will be good for you.
But a program without plugins that crashes often is just badly written, or very very complex, or very old (and missing a lot of maintenance time). You must stop the development, and investigate each and every crash. On Windows, compile the modules with PDBs (program databases), and each time it crashes, attach a debugger to it.
You must add internal tests, too. Avoid the pattern:
doSomethingBad(T * t)
{
if(t == NULL) return ;
// do the processing.
}
This is very bad design because the error is there, and you just avoid it, this time. But the next function without this guard will crash. Better to crash sooner to be nearer from the error.
Instead, on Windows (there must be a similar API on MacOS)
doSomethingBad(T * t)
{
if(t == NULL) ::DebugBreak() ; // it will call the debugger
// do the processing.
}
(don't use this code directly... Put it in a define to avoid delivering it to a client...)
You can choose the error API that suits you (exceptions, DebugBreak, assert, etc.), but use it to stop the moment the code knows something's wrong.
Avoid the C API whenever possible. Use C++ idioms (RAII, etc.) and libraries.
Etc..
P.S.: If you use exceptions (which is a good choice), don't hide them inside a catch. You'll only make your problem worse because the error is there, but the program will try to continue and will probably crash sometimes after, and corrupt anything it touches in the mean time.
You can always add exception handling to your program to catch these kinds of faults and ignore them (though the details are platform specific) ... but that is very much a two edged sword. Instead consider having the program catch the exceptions and create dump files for analysis.
If your program has behaved in an unexpected way, what do you know about your internal state? Maybe the routine/thread that crashed has corrupted some key data structure? Maybe if you catch the error and try to continue the user will save whatever they are working on and commit the corruption to disk?
Beside writing more stable code, here's one idea that answers your question.
Whether you are using processes or threads. You can write a small / simple watchdog program. Then your other programs register with that watchdog. If any process dies, or a thread dies, it can be restarted by the watchdog. Of course you'll want to put in some test to make sure you don't keep restarting the same buggy thread. ie: restart it 5 times, then after the 5th, shutdown the whole program and log to file / syslog.
Build your app with debug symbols, then either add an exception handler or configure Dr Watson to generate crash dumps (run drwtsn32.exe /i to install it as the debugger, without the /i to pop the config dialog). When your app crashes, you can inspect where it went wrong in windbg or visual studio by seeing a callstack and variables.
google for symbol server for more info.
Obviously you can use exception handling to make it more robust and use smart pointers, but fixing the bugs is best.
I would recommend that you compile up a linux version and run it under Valgrind.
Valgrind will track memory leaks, uninitialized memory reads and many other code problems. I highly recommend it.
After over 15 years of Windows development I recently wrote my first cross-platform C++ app (Windows/Linux). Here's how:
STL
Boost. In particular the filesystem and thread libraries.
A browser based UI. The app 'does' HTTP, with the UI consisting of XHTML/CSS/JavaScript (Ajax style). These resources are embedded in the server code and served to the browser when required.
Copious unit testing. Not quite TDD, but close. This actually changed the way I develop.
I used NetBeans C++ for the Linux build and had a full Linux port in no time at all.
Build it with the idea that the only way to quit is for the program to crash and that it can crash at any time. When you build it that way, crashing will never/almost never lose any data. I read an article about it a year or two ago. Sadly, I don't have a link to it.
Combine that with some sort of crash dump and have it email you it so you can fix the problem.