Help - Search - Members - Calendar
Full Version: C++ classes, overloading, and streams
AC Tools Everything Macro > General Discussion > C++/Delphi/VB Development
drewlt
This sounds like a basic C++ question - any help is appreciated.

I'm using madCodeHook to inject into processes and send messages back to my front end. When the messages are all text or one number it's fine, but if I want to combine them (like "Injected into hWnd:" + hWnd) then the only way I know to do it is create a stringstream, run << on all the data I want, create a char array buffer to run stringstream::read on, and then send the buffer to the IpcSendMessage function.

Now, if that sounds like the best way to do it, here's the main question. (if not, i'd love your ideas)
What I'm shooting for is a class that will handle all this for me. If I could imitate the stream classes (such as std::cout<<"asdf"<<123<<".";) then I could create a class that would compile all the data into a stringstream, create the buffer inside the overloaded << operator and send off the message.

I do not know the syntax or if this is even possible.

Ideas, suggestions, constructive criticism are welcome.

Thanks
smile.gif -DT
DaMOB
Classes is one of my C++ weak spots. Basically I suck in that area so I can't be of much help I am afraid because every single time I have to make a class I whip out my 3 C++ books or use Google to prod me along.
Ahk
I'm in the process of my 2nd attempt to get through a C++ book, this time I'm taking notes and learning far more than I reallized I had skipped over.

I can only comment a little, because my understanding is still quite newbish.

My first attempt to understand even how to use the operators (since I haven't learned that much) and have similar functionality of cout, then maybe looking through the iostream header would be a good idea. I know some libraries look quite confusing, but that's because I don't know that much yet smile.gif.

Also, you've got 123 as an rvalue in your std::cout statement, and sending the literal doesn't make any sense, but I'm sure you already know that since you're hooking API's tongue.gif.

I ran into a site searching for the madCodeHook, but it's not of much help since the code samples are in delphi, and it doesn't really cover how to set up the message to send. I'd say you'd have to research how to create a class, since the rest of us don't know how. Try a dedicated C++ forum...if you haven't already.
drewlt
Of course, header files. Good idea.

edit: I thought this was working, I have multiple versions that use a buffer string and some that dont - but this is the general idea untill I can correctly build a class to do what I want.

Here's what's working right now (rough code):
CODE

//module level:
stringstream ipc;
char buffer[255];

//In each process that needs to send an ipc message
ipc<<"Hello World. #"<<1<<" is number one.";
SendIPC;

//Send IPC function
void SendIPC();
{
   ipc.read(buffer,255);
   ipc.str("");
   SendIpcMessage((LPCSTR)"IPCQueue",buffer,(DWORD)strlen(buffer));
   return;
}


... or something like that.

I'll go find a dedicated c++ forum too one of these days...

smile.gif -DT
Ahk
I'm making my way into some of the parts of classes right now in my C++ book, but it hasn't covered directly constructors or operators. This book jumps all over the freakin' place it seems like. Unlucky for me, the more in depth class info isn't until chapter 7 and I'm just making it to chapter 3. I've got maybe a good couple of weeks until I get that far smile.gif. Hopefully, this thread is still at the top and maybe I'll be able to post on it.

I'm sure a C++ forum would have someone that knows, but it's possible that it could be full of jackasses, so supposing that, this could be the most viable option hehe. Then again, it might not...gtg

segfault
I'm not exactly sure what you want, but this is the general way to overload operators in your class:

CODE

//in the .h file
const class_Name & operator += (arguments);
ostream & operator << ( ostream & os, const class_name & str );
istream & operator >> ( istream & is, class_name & str );

//in the .cpp file something like this
ostream& operator <<(ostream & os, const class_name & str)
{
   return os << str.c_str();
}


So if my class_name was Tool, I could do
CODE

Tool temp_var;
//set some variable in temp_var

cout<<temp_var;
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.