• Welcome to Valhalla Legends Archive.
 

[C++] Questions

Started by Sorc.Polgara, November 17, 2004, 03:22 PM

Previous topic - Next topic

Sorc.Polgara

I'm trying to make a bot in VC++ 6 since I've successfully made one in VB6.  I'm running into a few problems.  I have some questions.

1.  Does VC++ Winsock v2 have Events, separate functions that are triggered when ___ happens lik in VB6 Winsock?  If so, can I get a simple example of using a VC++ Winsock Event?  I could not find anything in the MSDN Library or the C++ Winsock Tutorials to help.
2.  Can a typedef Structure be sent as a readable packet to a socket?
3.  When converting a null-terminating string to a non-null-terminating string, is this an efficient way of doing so?

strncpy(strDest, strSrc, strlen(strSrc) - 1)

4.  Should I avoid using Windows API or MFC classes if I don't plan to use a Windows GUI, but a command line interface?
5.  What is the most efficient way of creating a dynamic string/array?
6.  When making a packet buffer/debuffer is it better to use a external cpp source file to define member functions and functions, or should I put them in the header file itself?
7.  Lastly, Do you dislike VB?  Now I do!  It is the Devil, VB666!


Thanks.

BaDDBLooD

#1
1. You can add support for events i believe
2. no idea
3. That seems like a bad way of doing it in my opinion.
4. Yes
5. no idea
6. I would put them in the header file
7. I Like vb, c is alot better in my opinion.
There are only two kinds of people who are really fascinating: people who know absolutely everything, and people who know absolutely nothing.


Sorc.Polgara

Quote from: UserLoser on November 17, 2004, 03:35 PM
Winsock events:

WSAEventSelect
WSAAsyncSelect
__WSAFDIsSet

ok

Here is the exampe that was given for the WSAEventSelect.
Quote
//-------------------------
// Declare and initialize variables
SOCKET ListenSocket;
WSAEVENT NewEvent;
sockaddr_in InetAddr;

//-------------------------
// Initialize listening socket
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

//-------------------------
// Bind listening socket
InetAddr.sin_family = AF_INET;
InetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InetAddr.sin_port = htons(27015);

bind (ListenSocket, (SOCKADDR *) &InetAddr, sizeof(InetAddr));

//-------------------------
// Create new event
NewEvent = WSACreateEvent();

//-------------------------
// Associate event types FD_ACCEPT and FD_CLOSE
// with the listening socket and NewEvent
WSAEventSelect( ListenSocket, NewEvent, FD_ACCEPT | FD_CLOSE);

I think I understand what its doing.  However in this example it doesn't show the actual "NewEvent" as its own function thingy where the code that is suppose to be executed when it is fired.

hmmm how is the Event's code that is to be executed defined or coded?  You have to make a function to do so?  I know that in VB you can make an Event's function by taking the object's name and then add _ and the event name.
this would be in VB:

Sub TheObject_TheEventName(Args)
'code to be executed
End Sub


but C++?

UserLoser.

Quote from: bethra on November 17, 2004, 04:04 PM
I think I understand what its doing.  However in this example it doesn't show the actual "NewEvent" as its own function thingy where the code that is suppose to be executed when it is fired.

hmmm how is the Event's code that is to be executed defined or coded?  You have to make a function to do so?

Using Events

Note: This is much more complex than using mswinsck.ocx in VB6

Kp

Quote from: BaDDBLooD on November 17, 2004, 03:28 PM2. no idea
3. That seems like a bad way of doing it in my opinion, however it does work.

2. Yes, it's completely possible.  A structure is just a way of laying out data in memory, and you can send any readable memory over a socket.  Whether it achieves your goal is an entirely different question, of course.
3. That is not only a bad way of doing it, it doesn't work.  In fact, it accomplishes absolutely nothing since it copies the string onto itself.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

BaDDBLooD

Quote from: Kp on November 17, 2004, 05:17 PM
Quote from: BaDDBLooD on November 17, 2004, 03:28 PM2. no idea
3. That seems like a bad way of doing it in my opinion, however it does work.

2. Yes, it's completely possible.  A structure is just a way of laying out data in memory, and you can send any readable memory over a socket.  Whether it achieves your goal is an entirely different question, of course.
3. That is not only a bad way of doing it, it doesn't work.  In fact, it accomplishes absolutely nothing since it copies the string onto itself.

Thanks, for correcting my answer.  I did not know that.  I am new to c  :-\
There are only two kinds of people who are really fascinating: people who know absolutely everything, and people who know absolutely nothing.

MyndFyre

Quote from: bethra on November 17, 2004, 03:22 PM
1.  Does VC++ Winsock v2 have Events, separate functions that are triggered when ___ happens lik in VB6 Winsock?  If so, can I get a simple example of using a VC++ Winsock Event?  I could not find anything in the MSDN Library or the C++ Winsock Tutorials to help.
What you'll find is that you'll have to get function pointer callbacks yourself to pass for functions, not like in VB.
Quote from: bethra on November 17, 2004, 03:22 PM
3.  When converting a null-terminating string to a non-null-terminating string, is this an efficient way of doing so?

strncpy(strDest, strSrc, strlen(strSrc) - 1)

Most every use for non-null-terminating strings in Battle.net communication are those four-byte DWORDs.  You can define them instead as:

// assumed typedef long int DWORD;
DWORD myProductId = 'STAR';

Note the single quotes.
Quote from: bethra on November 17, 2004, 03:22 PM
4.  Should I avoid using Windows API or MFC classes if I don't plan to use a Windows GUI, but a command line interface?
You don't need to avoid the API unless you want to be able to compile it cross-platform.  As for MFC, if you want your bot to be bloated and contain loads of meaningless nothings, don't use it.
Quote from: bethra on November 17, 2004, 03:22 PM
5.  What is the most efficient way of creating a dynamic string/array?
This would be a question better suited to others.  :)  Sorry.
Quote from: bethra on November 17, 2004, 03:22 PM
6.  When making a packet buffer/debuffer is it better to use a external cpp source file to define member functions and functions, or should I put them in the header file itself?
This seems to miss the point of header files and external files.  When making *any* class it is good practice to define function interfaces in the header and make the actual functions in the code CPP files.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mephisto

#8
Quote from: bethra on November 17, 2004, 03:22 PM
I'm trying to make a bot in VC++ 6 since I've successfully made one in VB6.  I'm running into a few problems.  I have some questions.

1.  Does VC++ Winsock v2 have Events, separate functions that are triggered when ___ happens lik in VB6 Winsock?  If so, can I get a simple example of using a VC++ Winsock Event?  I could not find anything in the MSDN Library or the C++ Winsock Tutorials to help.
2.  Can a typedef Structure be sent as a readable packet to a socket?
3.  When converting a null-terminating string to a non-null-terminating string, is this an efficient way of doing so?

strncpy(strDest, strSrc, strlen(strSrc) - 1)

4.  Should I avoid using Windows API or MFC classes if I don't plan to use a Windows GUI, but a command line interface?
5.  What is the most efficient way of creating a dynamic string/array?
6.  When making a packet buffer/debuffer is it better to use a external cpp source file to define member functions and functions, or should I put them in the header file itself?
7.  Lastly, Do you dislike VB?  Now I do!  It is the Devil, VB666!


Thanks.

Hello, and welcome to the vast world of C++.  You'll be introduced to plenty of headaches, frustrations, and annoyances as you program your bot, but in the end with good design quality, your application will be superior to your VB application and you will feel a great sense of accomplishment.  :)

Let's answer your questions:

Question: Does Winsock2 support events in C++?
Answer: Yes; with WSAEventSelect (Or WSAsyncSelect [note: not truely asynchronous]) you can create socket events.  You can use these events in a number of functions you may want to look into using when you begin your network programming such as WSAEnumNetworkEvents & WaitForMultipleObjectsEx.  Keeping in mind socket events are somewhat more complicated and differ in complexity to VB6's implementation through mswinsck.ocx.

Question: Can you insert a typedef structure into your packet buffer and send it to a server as a valid/readable packet?
Answer: Yes; you can send any valid data to a server.  However, it'll be up to you to determine whether the data is valid for that particular packet and how much space the structure is going to take up.

Question: Is this code an efficient way of transforming a NULL terminated string to a non-NULL terminated string? strncpy(strDest, strSrc, strlen(strSrc) - 1)?
Answer: No; you should look at your code over again.  What you are doing (providing char *strSrc = strDest) is copying strSrc to strDest which is the same thing subtracting 1 from the strlen.  So you are essentially copying the first three letters of the string over the first three letters of the string, leaving elements 3 & 4 ('whatever' and '\0') remaining how they were; thus you have accomplished nothing.  There is also no logical reason I can think of as to why you would want to eliminiate the NULL terminator from a string.

Question: Should using MFC or the Win32 API be avoided if I'm going to use this bot as a moderation bot from the command-line or console?
Answer: Yes and no; you won't be needing MFC if you're going to be using it as a console as MFC is a wrapper for the GUI functions of the Win32 API.  However, you should definitely use the Win32 API to benefit your program if it's going to run as an application on Windows.  In fact, if you're going to use winsock2.h you'll likely be using windows.h for some thing (for instance, if you're goind to use socket events, and you'll be waiting on them for activity, you'll be using WaitForMultipleObjects which is in windows.h).

Question: What is the most efficient way of dynamically allocating an array of string?
Answer: char *buf = new char[SOME_SIZE];  And of course, when you're finished with it: delete [] buf;  That's about as efficient of a way as I can think of (really, the only way as malloc is called from new anyhow).

Question: When creating a packetbuffer, should I put the definitions in a seperate source file and the declarations in a header file or all of them in one?
Answer: Typically, you'll want to create a generic class packet buffer where you can use it in other applications without changing anything.  You can then derive from that class to implement a Battle.net specific packet buffer with little work as most of it is done in the generic base class.  You'll declare/create the class in a header file, and define the member functions/methods in a source file.  The exception to this would be inline functions, which usually always go in the header file.

Question: VB sucks, C++ owns, what do you think?
Answer: VB6 is fine as long as you use it for what it was meant for.  C++ the same; though obviously C++'s boundries are quite limitless.  :)

And as always before you ask a question here: Go to google.com (or search.msn.com) and type in the name of the function you want to know about and it should come up on the top 1-10 search a link to MSDN about the function.  Unless of course the function is like 'copy' where hundreds of thousands of other Websites will come up; in those cases you'll have to narrow your search to something like "C++ copy()."  If you have any questions you can message me on MSN at [email protected] or AIM at SoR Mephisto zL.

Zakath

#9
Quote from: Mephisto on November 17, 2004, 10:38 PMchar *buf = new char[SOME_SIZE];  And of course, when you're finished with it: delete buf;  That's about as efficient of a way as I can think of (really, the only way as malloc is called from new anyhow).

Note the one glaring problem with that: it should be delete [] buf;Using operator delete and not operator delete [] on an array will cause a memory leak.

Quote from: bethra on November 17, 2004, 03:22 PM
I'm trying to make a bot in VC++ 6 since I've successfully made one in VB6.  I'm running into a few problems.  I have some questions.

1.  Does VC++ Winsock v2 have Events, separate functions that are triggered when ___ happens lik in VB6 Winsock?  If so, can I get a simple example of using a VC++ Winsock Event?  I could not find anything in the MSDN Library or the C++ Winsock Tutorials to help.
People have already mentioned WSAEventSelect() and WSAAsyncSelect(), but there is also the method of polling the socket regularly via the select() function.
Quote from: bethra on November 17, 2004, 03:22 PM
2.  Can a typedef Structure be sent as a readable packet to a socket?
The send() function expects the data in the form of a const char *. It might be interesting to create a class with an overloaded operator char * that assembles the pertinent data into such a buffer.
Quote from: bethra on November 17, 2004, 03:22 PM
3.  When converting a null-terminating string to a non-null-terminating string, is this an efficient way of doing so?

strncpy(strDest, strSrc, strlen(strSrc) - 1)
As has been mentioned, in C/C++ there is really no such thing as a non-null-terminated string.
Quote from: bethra on November 17, 2004, 03:22 PM
4.  Should I avoid using Windows API or MFC classes if I don't plan to use a Windows GUI, but a command line interface?
If you're using a command line, the API won't be much use even if you WANT to use it somehow...there are some functions in Windows.h that probably will come in handy, though.
Quote from: bethra on November 17, 2004, 03:22 PM
5.  What is the most efficient way of creating a dynamic string/array?
See above.
Quote from: bethra on November 17, 2004, 03:22 PM
6.  When making a packet buffer/debuffer is it better to use a external cpp source file to define member functions and functions, or should I put them in the header file itself?
It comes down to programming standards and readability. If you wanted, you could put an entire bot into one source file...but would you want to?
Quote from: bethra on November 17, 2004, 03:22 PM
7.  Lastly, Do you dislike VB?  Now I do!  It is the Devil, VB666!
I've never been a fan of VB.
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

Sargera

Ah, didn't even think about it when I was typing it to delete [] buf;  Thanks for pointing that out Zakath.  :)

Sorc.Polgara

bleh

Yeah, the winsock .ocx in VB6 is soooo much easier!  Atleast the Event aspects of it

I still can't seem to figure out how to make events.  I look at all the stuff I find in google and none of it really doesn't help.

I'm just probably stupid.

doh.

Kp

Quote from: bethra on November 19, 2004, 02:54 PMI'm just probably stupid.

It'd be more efficient to use overlapped I/O.  See GetQueuedCompletionStatus, CreateIoCompletionPort, WriteFile, and ReadFile.  Not necessarily in that order.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Mephisto

Quote from: Kp on November 19, 2004, 06:05 PM
Quote from: bethra on November 19, 2004, 02:54 PMI'm just probably stupid.

It'd be more efficient to use overlapped I/O.  See GetQueuedCompletionStatus, CreateIoCompletionPort, WriteFile, and ReadFile.  Not necessarily in that order.

Also WSARecv & WSASend & WSABUF structure.

HANDLE SocketEvent = CreateEvent(0, 0, 0, 0); // the parameters are generally not necessary in most cases
WSAEventSelect(YourSocket, SocketEvent, FD_CONNECT|FD_CLOSE); // you can look up the flags in the last parameter on MSDN
You now have a socket event bound to your socket.  You can use it how you need to.

Kp

Quote from: Mephisto on November 19, 2004, 06:07 PMAlso WSARecv & WSASend & WSABUF structure.

HANDLE SocketEvent = CreateEvent(0, 0, 0, 0); // the parameters are generally not necessary in most cases
WSAEventSelect(YourSocket, SocketEvent, FD_CONNECT|FD_CLOSE); // you can look up the flags in the last parameter on MSDN
You now have a socket event bound to your socket.  You can use it how you need to

No, don't look at those.  Using those isn't as efficient or convenient as overlapped I/O.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!