PDA

View Full Version : C++ Worthless?


DELOCH
02-10-2009, 10:40 PM
I've been trying to learn c++ many times... not the useless one that can compile a program into a user-mode program that can ask a person for input, display console output, loop...

I want to make applications that actually have a GUI... A custom GUI... A custom title bar, a custom drop down menu.

I want to set a pixel on my monitor to red while my program runs. I want to send information out of ports, I want to crash my computer! not give me a warning message!

I want to make my own drivers!

I want to read memory which is on my computer, any memory, without any warnings yelled at me. I want to create a program to run on startup or detect input without the application being visible.

I want to create a program that can go to internet and list directories of a website. I want to create my own browser without using an activeX control!

I want to control my own program! This is my machine, I should give it commands!

My question is. How can you make c++ stop being a jerk and saying that everything is premade and ugly and I shouldn't do anything.

For god's sake I always wanted to make my own operating system...

In my eyes, nothing that is made is impossible. Perhaps tedious and annoying, but definitely not impossible.

But why do no books, no websites, nothing at all tell you how to do the above things. I looked everywhere. I tried every single language I've heard of, none of which can do any of the above.

---------------

Now that i'm done with random thoughts, I will ask my question in one sentence.

Why can't I, the owner of my system, control my program to do absolutely everything I want?

Thanks ahead of time, this has been bugging me for many many years.

Fou-Lu
02-10-2009, 11:03 PM
Custom GUI: doable
Output handling: doable
Drivers: doable
Memory: DEP prevention. Should still be doable but you'd need to be far more masterful than I
Crashing: Refer to memory.
Internet connections and browsers: doable

The answer to you're question: because Windows likes its protective little shell. Lol.
Oracle Guy I'm sure can give you far better insight, C++ is definitely not one of my major languages. Note I assumed that this is Windows, the C programs I've made on Linux had absolutely no trouble with letting me crash it.

oracleguy
02-10-2009, 11:18 PM
The reason no one tells you how to do these things is that for some of them, you can't. It isn't a limitation of the language, it is a limitation of the operating system. It is the OS protecting you from crashing the computer or messing with another program.

You can't read any memory from any program you want because in modern operating systems, each program runs its own memory space. This isolates it from any other process, this is a security benefit and prevents bad programs from crashing the whole computer.

You can make your own drivers, typically they are done in C and not C++. I've done it before for Linux, for Windows Microsoft has a driver development kit (http://www.microsoft.com/whdc/devtools/wdk/default.mspx).

You can make your own browser in C++ without using an ActiveX control. For example, Firefox isn't using one...

You can make a program run on startup easily as well. As well as get input while the window is not visible. On Windows input can be taken by a program regardless of if it has focus or not with the WM_HOTKEY message (http://msdn.microsoft.com/en-us/library/ms646279(VS.85).aspx). There are also other messages that all programs get such as when devices are connected and disconnected, I used those when I wrote this program in C++: http://www.oracleguy.ws/projects/detail.php?id=11

You can write your own operating system as well, I've written one before in C. I wouldn't recommend writing it for a modern desktop right off only because the sheer amount of hardware complexity will make it more difficult. Using a small embedded system would be easier but you could do it on a desktop. It can be pretty fun, you have to write the entire kernel which includes the memory manager (for dynamic memory allocation), I/O (like the screen, keyboard, etc.), the shell functionality (aka command prompt) and if you want to go crazy the threading manager. That way you can run more than one program at once. In a more complex system you also need to handle things like virtual memory and paging, semaphores, etc..

However also realize too, when you write your own OS, all the functions you take for granted (cin, cout, printf, scanf, etc.) won't exist and you will have to implement them for your OS.

The reason modern operating systems (and processors) have multiple modes (user versus kernel) and virtual memory is because the system is so vast and complex that things need to be broken up. As an example, if each process didn't have it's own memory space, exploiting other programs would be crazy easy or a program bug could mess up your other applications.


I suspect part of the reason for your frustration is that I know you are a Java developer and I bet for a long time people told you that you only use C or C++ when you need to do low level stuff with hardware. Which is true, it can do that but it isn't some end all solution to software development that lets you do anything you please. The language isn't so much the barrier as is the operating system.

DELOCH
02-10-2009, 11:36 PM
But programs do change their look and feel, programs do manage to write directly to the monitor, programs do manage to connect to hardware.

Anything i'll think impossible has been done before. Just recently I was hitting my head against the wall trying to get a program in java to listen to mouse events outside of window, didn't get any result. While many programs such as screen recorders and keyloggers do it.

I don't understand, how are those people better than I am that the operating system forgives them yet spits in my face.

My point is, what should I learn to manage to get my programs to be unique, not blocked off like people trying to enter a plane with scissors...

I believe windows API is a way to tell your operating system to do something, and if you have the right to do the operation it will let you. However this is not the case for writing your own operating system... unless it's running on windows.

I just find it annoying to need to learn the whole complex windows api to be able to write a program that can set a pixel on the monitor.

Especially if I want to recompile that program to run on linux, mac, or another operating system... In which case, my best chance is throwing my computer off a cliff and hope it falls onto a person who is responsible for making it impossible to be creative in programs.

oracleguy
02-11-2009, 12:26 AM
But programs do change their look and feel, programs do manage to write directly to the monitor, programs do manage to connect to hardware.

I never said it was impossible, in the case of hardware you just have to use the drivers to interface with the hardware.


I don't understand, how are those people better than I am that the operating system forgives them yet spits in my face.

My point is, what should I learn to manage to get my programs to be unique, not blocked off like people trying to enter a plane with scissors...


Having custom title bars and such might seem cute but it is annoying. It is like changing the scrollbar color or how push buttons work on a webpage, just because it can be done doesn't mean it is a good idea.

Most of the things you want to do are do-able, you just can't expect it to be easy with everything just laid out for you.


I believe windows API is a way to tell your operating system to do something, and if you have the right to do the operation it will let you. However this is not the case for writing your own operating system... unless it's running on windows.


Windows is an operating system, if you write your own OS it isn't running on anything. Yes, the API is how you can interact with the kernel and the operating system as a whole. I don't quite understand your comment "However this is not the case for writing your own operating system...".


I just find it annoying to need to learn the whole complex windows api to be able to write a program that can set a pixel on the monitor.


You don't, if you want to draw anything inside your program window, there are routines for it using GDI easily. For more advanced 3D stuff, you can use OpenGL or DirectX. As for drawing outside of your program, it would depend on what you are going for but it is do-able. In this C++ app I wrote here: http://www.oracleguy.ws/projects/detail.php?id=10 I am drawing all those blocks and text bubbles myself using GDI. There are functions for drawing rectangles, squares, text, etc. in GDI.


Especially if I want to recompile that program to run on linux, mac, or another operating system... In which case, my best chance is throwing my computer off a cliff and hope it falls onto a person who is responsible for making it impossible to be creative in programs.

I don't totally get what you are saying here. Yes, you have to recompile for a different platform.

I write cross platform apps at work (Windows & Unix) in C++, it isn't a big deal for most stuff except where it interacts with the OS. In which case you write one version for one platform and one for another and use pre-processor statements (#define, #ifdef, #ifndef, etc.) to automatically switch between which one gets compiled. For cross platform apps that use a GUI, your best bet is to use a GUI framework (GTK, Qt, wxWidgets, etc.), it saves a ton of time and a lot of code writing on your part.

DELOCH
02-11-2009, 02:22 AM
By pixel i meant pixel on the screen not on the registered window.

I mean direct pixel onto a monitor.

say, red pixel at pixel 1000, 390... Without any windows present

oracleguy
02-11-2009, 03:07 AM
By pixel i meant pixel on the screen not on the registered window.

I mean direct pixel onto a monitor.

say, red pixel at pixel 1000, 390... Without any windows present

Why would you want to do such a thing? If you want full control over the screen, you can run your app in full screen mode which would give you that ability to color any pixel any which way that you wanted.

DELOCH
02-11-2009, 04:48 AM
I can, but that isn't what I want.

I mean for example there is an easter egg in minesweeper which involves a pixel changing colors from white to black(white being safe). The pixel is located at position 0,0 on the monitor.

I want to know how to do this.

flynch01
02-11-2009, 09:15 PM
I think you're looking at things wrong, I know EXACTLY where you're coming from because I felt the same for a while. After a while I gave up and succumbed to learning how other people do it. As you go along, you see why other people do it like that, and it begins to make sense.

First it's best to understand why OS's lock off so much information. Windows for example, say you're drawing to the screen directly and you decide to minimize your program.

Suddenly it becomes YOUR job to decide whether there are windows behind it to be drawn, whether the windows behind it SHOULD be drawn. If you request windows do it for you, and every other program also asks windows to do it for them then windows knows EVERYTHING about what is going on, it knows what to draw on top of what, when to do it etc etc, you won't have pixels flashing everywhere trying to draw over each other.

To give an example, you asked to draw a pixel outside your window, sure, just ask windows to do it, it knows you want to draw a pixel onto the monitor (NULL DC, the entire screen basically) and because it knows where it's drawing everything else, it can do it smoothly:


#include <windows.h>

int main() {
HDC hdc = GetDC ( NULL );
COLORREF aColor = RGB( 255, 0, 0 );
while( 1 ) {
SetPixel( hdc, 100, 100, aColor );
Sleep( 1 );
}
return 0;
}




Now for what you wanted, to completely bypass windows, tell it to get lost and do it yourself. I gave the reasons why you shouldn't, but who's to say you can't?

By default, your program doesn't have access to low level things, if you try then the OS will step in and kick your ***. There's ways around this, write a driver or there's some very hacky methods I won't go into here to get the privileges you need. But once you do, you can just get the video memory address, and write directly into the pixel you want.

Basically in short, it's not that you CAN'T do xyz, it just makes sense to do it that way. Don't get me wrong, it's still a good idea to reinvent the wheel if you want to understand it.

DELOCH
02-14-2009, 03:10 AM
How do I enter Kernel Mode?

oracleguy
02-14-2009, 03:47 AM
How do I enter Kernel Mode?

The stuff to do that would probably be in the DDK (I assume we are talking about Windows). Though 99% of the time you should never need to enter kernel mode. What are you trying to do?

flynch01
02-14-2009, 02:03 PM
You can write a driver. Or, you could try and patch an interrupt or a call gate, and on older systems you could use an interupt return. But you shouldn't ever need to do any of this, but if you must then as oracleguy said. Try writing a driver. Out of curiosity, what are you trying to do?

(WDK) : http://www.microsoft.com/whdc/devtools/wdk/default.mspx

DELOCH
02-14-2009, 07:15 PM
Honestly, I am trying to learn how to talk directly to hardware by using software.

To do this, I need to bypass windows.

the only two methods to do that is to make windows do it for me, or I can write a kernel-mode application(a driver).

I never written a driver so I want to try making one.
To do this, I need to know how to enter kernel mode.

:D

flynch01
02-14-2009, 08:46 PM
Then go to that link I provided, download the WDK. I'm sure there's some examples or even documentations with it to help you along. But if you want to write a driver on windows. You need the WDK.

Old Pedant
02-15-2009, 10:09 PM
I am trying to learn how to talk directly to hardware by using software.


Then go find a simpler machine to work on. Say one of the old 8-bit machines such as an Apple II or an Atari 800.

Or, if you really are fanatic about it, go ahead and take control of your entire PC and ignore the operating system completely.

But don't expect the operating system--whether it is Windows or Linus or Mac in one of its flavors--to *cooperate* with you in taking over the machine. All the modern operating systems are designed with one principle in minde: KEEP ALL THE VARIOUS PROGRAMS SEPARATE! Don't let program X step on the memory space of program Y. Don't let Window A write to the virtual screen of Window B. Etc.

You are working so very very hard to go AGAINST the tide.

There is really no reason that you can't completely "take over" any PC with your own program. Just toss out the operating system and do *EVERYTHING* yourself. Just remember that "everything" includes reading the keyboard, the mouse, the USB ports. Writing to the screen memory, to the USB ports, to the sound card. Oh...and we didn't even start mentioning the disk drives yet. Yes, you could easily take over the entire disk drive. What kind of file system will you use??? Or maybe you can just chuck the file system and treat the entire drive as one big file and write routines to read and write disk sectors, one at a time.

Hey, I've been there, I've done all that. Heck, I once even wrote a hierarchical file system (you know, directories and subdirectories and sub-sub-directories, etc.). It was for a single user system with no such thing as "file permissions" or "disk contention." It still took me maybe 6 or 8 months to write it. Alone. Without any of the keyboard or screen or sound or whatever routines. On much simpler equipment than a modern PC.

Plus you have the problem with today's PCs that you write it to work on one machine with one sound card and one keyboard type and one mouse type and one disk drive type and one video card. And it's utterly worthless on any machine where any one of those parts is the least bit different. (This is why it was so much easier in the old 8-bit days...ALL machines of a particular brand were identical in capabillities and hardware. Write once, run on any clone.)

Ehhh...I dunno why I'm babbling like this. The only way you are going to be convinced is to go spend the time trying it yourself. Good luck!

oracleguy
02-15-2009, 10:13 PM
Then go find a simpler machine to work on. Say one of the old 8-bit machines such as an Apple II or an Atari 800.

Or you could use a microcontroller, they are computer systems in a chip and fairly easy to work with. Some now days even have RF transceivers and USB controllers in them. Most support writing C code and assembly as well.

Old Pedant
02-16-2009, 12:43 AM
Yes, exactly. A "controlled environment" where you know the hardware available and don't have to worry about having drivers for 17 different kinds of video cards and 9 different kinds of sound cards and...

Life is/was *SO* much simpler on smaller, constrained hardware.

(How about writing for cell phones?? Especially if you write for the Java-based phones, you can usually do pretty much what you want and even semi-portably. I worked for a company that was able to deploy to literally hundreds of different Java-based models in a matter of a day or two. Again, the hardware is so constrained that there isn't too much to worry about. Screen size, mainly. So you were careful about writing screen-drawing stuff, of course. But most everything else is either on the phone or not, not too many variations. The C++ based phones aren't quite as similar, but even on them we could deploy to dozens of different phones in a couple of days.)

DELOCH
02-16-2009, 06:25 AM
I want to but the problem isn't simple.

I mean, I don't want to be typing in caps all the time when i am programming an application that requires reading memory, writing directly to screen, reading input from mouse/keyboard/microphone...

I don't understand why sending 2bytes out of parallel port should take more than 5 lines of code.

I'm always told that assembly is to be used only in times when optimizations are needed.
I view assembly as an exact macros for machine code that are processed by the CPU directly.
The problem is that the operating system acts as customs officer between the CPU and the Assembly and If my Assembly doesn't contain the "Please do this for me as I am not worthy enough" it just doesn't let me do the things.


I did an experiment yesterday involving pointers to absolute memory.
When i dumbly point them to 0x300393, it doesn't say anything. However when I try to read/write from it, the operating system throws an exception. While If I know exactly the memory address of a value I initialized earlier, it allows me to read/write from it).
This means to me that OS simply doesn't allow me to read memory which isn't initialized by my application.

But programs do this, I believe they do it by asking the operating system to check it for you and dump the value onto a variable. This sounds to me like a lot of tedious work which makes my program much slower than it could be.

I've written applications that wrote out alphabet in DEBUG.COM that took less space than storing the alphabet in a file(26bytes) -- my program was under 20 bytes.
The sad thing about it is that I think in runtime it's even heavier than most normal applications as 16bit programs cannot run on 32bit system, so there's some heavy interpreter which makes it run like visual basic.

The point of my babbling is, is there no humane method to enter kernel mode than taking the operating system out and upgrading to DOS? (yes, upgrading...)

:| *BTW does anyone actually sell those 8bit/16bit systems anymore?*

oracleguy
02-16-2009, 07:19 AM
I did an experiment yesterday involving pointers to absolute memory.
When i dumbly point them to 0x300393, it doesn't say anything. However when I try to read/write from it, the operating system throws an exception. While If I know exactly the memory address of a value I initialized earlier, it allows me to read/write from it).
This means to me that OS simply doesn't allow me to read memory which isn't initialized by my application.


You can make pointers have whatever value you want, it won't stop you. The thing you also have to remember is that modern operating systems use virtual memory. Which means each process has its own memory space.

As an example, inside 2 different programs both running at once, you might see a pointer to some allocated memory address at 0x748F0. That doesn't mean they are both sharing the same memory space. When the program requests that address, the OS in concert with the CPU will translate the virtual address into a physical address in the memory.

The memory in your program is managed by the memory manager in the OS for each process. That is how you get memory from the heap. You ask it for a certain number of bytes and it figures out where in your programs memory space to put it and returns the starting address.

The sad thing about it is that I think in runtime it's even heavier than most normal applications as 16bit programs cannot run on 32bit system, so there's some heavy interpreter which makes it run like visual basic.


No, not really, that isn't quite how it works. The processor can still execute 16-bit native code. The OS just still needs to have the other 16-bit dependencies.


The point of my babbling is, is there no humane method to enter kernel mode than taking the operating system out and upgrading to DOS? (yes, upgrading...)


Please read and re-read this: We have told you several times so far that there is absolutely no reason you should need to do this outside of device drivers. That is the reason you cannot do it.

Oh and btw: DOS is an operating system too


:| *BTW does anyone actually sell those 8bit/16bit systems anymore?*

No but the microcontroller idea is one that I would very strongly recommend you doing. You can get little development boards that a microcontroller with some ports that you can connect hardware to and the programming circuit on it. That way you can write C code to run on it and you are the boss. You can do whatever you want.

I think you are about 15 years too late to do the tinkering you want to do with a desktop computer. As Old Pedant pointed out, the hardware has gotten complicated to such an extreme that it is really hard to do what you want. And as flynch01 said much earlier in the thread, there is a very good reason the operating system isn't letting you do what you want: Everything has to be broken up and modularized otherwise things would be a dozen orders of magnitude more complicated.

As for writing the parallel port:

You need to create a driver or use an already made one like I linked you to. That is how you are going to be able to do it. If you don't want to do that, use a serial port instead.

DELOCH
02-16-2009, 06:36 PM
If everything that a programmer would want to do is already available on windows. How would I read memory from another process if all programs have their own virtual memory location (or a sandbox) that prevents me from doing so?

Cheat engine does it.

flynch01
02-16-2009, 11:13 PM
Yes, you can.

http://msdn.microsoft.com/fr-fr/library/ms680553(en-us).aspx

Cheat engine does it differently though obviously, it would be mental to do it like this for that purpose.

Old Pedant
02-17-2009, 07:24 AM
How would I read memory from another process if all programs have their own virtual memory location (or a sandbox) that prevents me from doing so?

Debuggers do it, too. Debuggers can even access processes that they didn't even know existed until they were asked to probe and find the process.

It's all there. It's all possible.

But you need to understand that, at the very same time process 1 is reading and writing to the memory address (say) 0x1C3308, process 2 might be reading and writing to that VERY SAME MEMORY ADDRESS (0x1C3308) but in a DIFFERENT PROCESS SPACE! Each process in a modern machine has it's OWN address space that it does not share with other processes.

Old Pedant
02-17-2009, 07:30 AM
When i dumbly point them to 0x300393, it doesn't say anything. However when I try to read/write from it, the operating system throws an exception.


But you missed the fact that it *IS* possible for YOUR CODE to take control of that exception, allocate a chunk of memory at that address, and then let the program try the instruction again...at which point it will succeed!

This is a truly WONDROUS way to do memory management! It's the basis of an Object Oriented Database System that I created way back in 1993-1994 on WindowsNT. Instead of having to load entire networks of objects into memory from disk, we could just load them 64K at a time. (Windows supports RESERVING memory in 64K chunks and then ALLOCATING from your reserved memory in 4K chunks...or it did at the time...wouldn't be surprised if the chunk sizes have gotten bigger. 16MB was a fair sized machine at that time!) Then, if an object contained a pointer to outside of already allocated memory, we caught the "page fault", allocated the memory, loaded another 64K from disk, and allowed the code to continue! Lazy loading of networks of objects. Truly a WONDERFUL system. And it's all there in Windows, ready to use! We didn't have to re-invent the wheel!

Oh, yes...and we wrote all but a very very very tiny bit of all that code in C++! Just a small tidbit of the actual page fault code was in assembler, as I recall.