|
Not only access, I want to access it in a special order.
If you translate index values to x and y coordinates of an array, you normally do this using the mod and div stuff. But I want to access the elements like a snail, so for increasing index value we should get more and more to the center of the array (as in the table I posted).
-Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
|
|
|
|
|
Hi, i found this code somewhere to open a raw socket under WIN XP and to send a packet with your own TCP/UDP header. The author posted it on a forum because the sendto() function failed at runtime. Someone then modified the code slightly (replaced sizeof() in sendto() to strlen()) and for them it worked, but i still get the same error. Here's the code:
<code>
#pragma comment( lib, "Ws2_32.lib" )
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <conio.h>
#define IP "192.168.200.18"
#define PORT 123
#define IP_DEST "192.168.200.19"
#define PORT_DEST 123
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
#define MAX_ADDR_LEN 16
#define MAX_HOSTNAME_LAN 255
USHORT checksum(USHORT *buffer, int size);
void checkPacket(char * pacote);
struct tcpheader {
unsigned short int th_sport;
unsigned short int th_dport;
unsigned int th_seq;
unsigned int th_ack;
unsigned char th_x2:4;
unsigned char th_off:4;
unsigned char th_flags;
unsigned short int th_win;
unsigned short int th_sum;
unsigned short int th_urp;
};
struct ipheader {
unsigned char ip_hl:4, ip_v:4;
unsigned char ip_tos;
unsigned short int ip_len;
unsigned short int ip_id;
unsigned short int ip_off;
unsigned char ip_ttl;
unsigned char ip_proto;
unsigned short int ip_sum;
unsigned int ip_src;
unsigned int ip_dst;
};
struct udpheader {
unsigned short int uh_sport;
unsigned short int uh_dport;
unsigned short int uh_len;
unsigned short int uh_sum;
};
int main (void)
{
WSADATA wsd;
char datagram[5000];
unsigned int bOpt=1;
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0){
printf("WSAStartup() failed: %d\n", GetLastError());
return -1;
}
SOCKET s = WSASocket(AF_INET, SOCK_RAW, IPPROTO_UDP, NULL, 0,0);
if (s == INVALID_SOCKET){
printf("WSASocket() failed: %d\n", WSAGetLastError());
return -1;
}
if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&bOpt, sizeof(bOpt)) == SOCKET_ERROR){
printf("setsockopt(IP_HDRINCL) failed: %d\n", WSAGetLastError());
return -1;
}
struct ipheader *iph = (struct ipheader *) datagram;
struct udpheader *udph = (struct udpheader *) (datagram + (4*4));
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons (PORT_DEST);
sin.sin_addr.s_addr = inet_addr (IP_DEST);
memset (datagram, 0, sizeof(datagram));
iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = 0;
iph->ip_len = sizeof (struct ipheader) + sizeof (struct udpheader);
iph->ip_id = 1;
iph->ip_off = 0;
iph->ip_ttl = 100;
iph->ip_proto = 17;
iph->ip_sum = 0;
iph->ip_src = inet_addr(IP);
iph->ip_dst = inet_addr(IP_DEST);
udph->uh_sport = htons (PORT);
udph->uh_dport = htons (PORT_DEST);
udph->uh_len = sizeof (struct ipheader) + sizeof (struct udpheader);
udph->uh_sum = 0;
strcpy(datagram,"Teste");
datagram[5000] = NULL;
iph->ip_sum = checksum((unsigned short *)&iph, sizeof(ipheader));
udph->uh_sum = checksum((unsigned short *)&udph, sizeof(udpheader));
checkPacket(datagram);
if (sendto(s, datagram, strlen(datagram), 0, (SOCKADDR *)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf("sendto() failed: %d\n", WSAGetLastError());
return -1;
}
else{
printf("OK\n");
}
if (sendto(s, datagram, strlen(datagram), 0, (SOCKADDR *)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf("sendto() failed: %d\n", WSAGetLastError());
return -1;
}
else{
printf("NOPE\n");
}
return 0;
}
USHORT checksum(USHORT *buffer, int size)
{
unsigned long cksum=0;
while (size > 1)
{
cksum += *buffer++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
}
void checkPacket(char * buff)
{
ipheader* pIpheader;
tcpheader* pTcpheader;
udpheader* pUdpheader;
SOCKADDR_IN saSource, saDest;
int iNextheader;
char * packetData;
pIpheader = (ipheader *) buff;
printf("IP Header \n");
printf("IHL: %d\n",(int)pIpheader->ip_hl);
printf("VERSION: %d\n",pIpheader->ip_v);
printf("TYPE OF SERVICE: %d\n",(int)pIpheader->ip_tos);
printf("TOTAL LENGTH: %d\n",pIpheader->ip_len);
printf("IDENTIFICATION: %d\n",pIpheader->ip_id);
printf("FRAGMENT OFFSET: %d\n",pIpheader->ip_off);
printf("TTL: %d\n",(int)pIpheader->ip_ttl);
printf("PROTOCOL: %d\n", (int)pIpheader->ip_proto);
printf("CHECKSUM: %d\n",pIpheader->ip_sum);
saSource.sin_addr.s_addr = pIpheader->ip_src;
printf("SOURCE ADDRESS: %s\n",inet_ntoa(saSource.sin_addr));
saDest.sin_addr.s_addr = pIpheader->ip_dst;
printf("DESTINATION ADDRESS: %s\n",inet_ntoa(saDest.sin_addr));
iNextheader = (pIpheader->ip_hl * 4);
if ((int)pIpheader->ip_proto == 6)
{
pTcpheader = (tcpheader *) (buff + iNextheader);
printf("\nTCP Header\n");
saSource.sin_port = pTcpheader->th_sport;
printf ("SOURCE PORT: %d\n", htons(saSource.sin_port));
saDest.sin_port = pTcpheader->th_dport;
printf ("DESTINATION PORT: %d\n", htons(saDest.sin_port));
printf ("SEQUENCE: %d\n", pTcpheader->th_seq);
printf ("ACK: %d\n", pTcpheader->th_ack);
printf ("X2: %d\n", (int)pTcpheader->th_x2);
printf ("OFF SET: %d\n", (int)pTcpheader->th_off);
printf ("FLAGS: %d\n", (int)pTcpheader->th_flags);
printf ("WINDOW: %d\n", pTcpheader->th_win);
printf ("CHECKSUM: %d\n", pTcpheader->th_sum);
printf ("URP: %d\n", pTcpheader->th_urp);
printf("\nDATA\n");
iNextheader = (pIpheader->ip_hl * 4 + sizeof(tcpheader));
packetData = (char *) (buff + iNextheader);
printf ("DATA: %s\n", packetData);
}
if ((int)pIpheader->ip_proto == 17)
{
printf("\nUDP Header\n");
pUdpheader = (udpheader *) (buff + iNextheader);
saSource.sin_port = pUdpheader->uh_sport;
printf ("SOURCE PORT: %d\n", htons(saSource.sin_port));
saDest.sin_port = pUdpheader->uh_dport;
printf ("DESTINATION PORT: %d\n", htons(saDest.sin_port));
printf ("LEN: %d\n", pUdpheader->uh_len);
printf ("CHECKSUM: %d\n", pUdpheader->uh_sum);
printf("\nDATA\n");
iNextheader = (pIpheader->ip_hl * 4 + sizeof(udpheader));
packetData = (char *) (buff + iNextheader);
printf ("DATA: %s\n", packetData);
}
getch();
}
</code>
Anyone know whats wrong here?
A possible clue might be: When the program runs and it displays all the info, for destination address it says "0.123.0.123" and i don't think thats right...
Also sendto() returns 10049, this is something to do with an address-not-available-error.
Thanks
Kuniva
--------------------------------------------
|
|
|
|
|
|
|
Hi y'all!
I have create a list view control which has check boxes!
How can I make the check boxes disabled inside the list view control, and how can I make transparent the frame which is around of the check mark?
Thanks for any answer!
|
|
|
|
|
use:
ListView_SetItemState ()
look for its parameters on the msdn library:
href="http://msdn.microsoft.com">msdn.microsoft.com[^]
Kuniva
--------------------------------------------
|
|
|
|
|
Hello
I want to make a separator between tow buttons in toolbar!! how can i do it?
Best Regards
|
|
|
|
|
|
thank you very match
Best Regards
|
|
|
|
|
If I create a window, and I use the normal SetWindowsHook procedure like this:
<br />
SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTProc, hInstance, NULL);<br />
It does work on self defined windowclasses but it doesn't seem to work with normal controls, like buttons and so on. How can I overrule this? If not possible, can I make a button using a selfdefined windowclass which calls the button procedure on it's turn?
LPCTSTR Dutch = TEXT("Double Dutch ");
|
|
|
|
|
I know in mfc application, we can get the path of exe from CWinApp. But how can i do the same thing in console exe without MFC? The reason i need this feature is some parameter file is store in the same directory as exe. Any idea to solve it?
|
|
|
|
|
|
How can I open a database and execute SQL requests without MFC classes (CDaoDatabase, CDaoRecordset...)? in other words: how people used to work with databases before the existance of MFC (the old times)?
Thank you in advance.
|
|
|
|
|
Maybe you should use the ODBC API directly from your app. There should be plenty of information on MSDN.
|
|
|
|
|
|
In the computer world, 1 = true and 0 = false,
but why do we return 0 to indicate it is
successful? For example,
int main()
{
// codes here
return 0; // to indicate successful
}
|
|
|
|
|
One of the reasons is because a lot of the time we want to know what went wrong. We can't have lots of different zeros, so we just let 0 be success, and have any non-zero value meaning failure. This becomes just as simple a test, except that we can get more detail if required by checking for a particular non-zero value - if the program in question returns different values for different errors.
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
There are two ways of looking at return values. One is succeeded/failed, and the boolean type is perfect for that. So true (1) indicates success, false (0) indicates failure. The other way is using a set of numbers with predefined meanings. In this case, success does not have to be 0, or 1, or 6000, or anything really. 0 is the first number, and it just happened to be assigned the meaning success (ERROR_SUCCESS ).
--Mike--
Latest blog entry: *drool* (Alyson) [May 10]
Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber
"You have Erica on the brain" - Jon Sagara to me
|
|
|
|
|
<quote>So true (1) indicates success, false (0) indicates failure.
That's excatly why I'm confusing. In the boolean value, true is 1 and it
should indicate succeeded. But we use return 0; to indicate successful,
don't we? But of course the return type is int and it is not boolean.
I wonder can we do this to indicate succeeded?
bool main()
{
// codes here
return true;
}
|
|
|
|
|
VW_Red_Jetta wrote:
I wonder can we do this to indicate succeeded?
bool main()
{
// codes here
return true;
}
Nope. Comparing boolean return values to int return values is much like comparing apples to asparagus. As others have mentioned, there's no deep philosophical reason for the apparent contradiction in terms; merely that's the way it is technically.
If it makes you feel a bit better, you can always do this:
#include <stdlib.h><br />
<br />
int main(int argc,char **args) {<br />
<br />
<br />
<br />
return EXIT_SUCCESS;<br />
}
- Mike
|
|
|
|
|
Some questions about EXIT_SUCCESS: is it always capitalized? Can I
return exit_success? And is it deifined in stdlib.h?
I once have a trouble understanding what does it do and I'm still
having trouble with it too. Could you tell me what does
EXIT_FAILURE do? How does it return on error? Is there any alternative
to this method?
int main()
{
.
.
.
// open input and exit on error.
if(ins.fail())
{
cerr << "ERROR: cannot open";
return EXIT_FAILURE;
}
.
.
.
}
|
|
|
|
|
VW_Red_Jetta wrote:
Some questions about EXIT_SUCCESS: is it always capitalized?
Yes, all identifiers in C and C++ are case-sensitive.
VW_Red_Jetta wrote:
And is it deifined in stdlib.h?
Yes, you can check for yourself... there will probably be a line like:
<br />
#define EXIT_SUCCESS 0<br />
VW_Red_Jetta wrote:
Could you tell me what does
EXIT_FAILURE do? How does it return on error?
The return value from main() in C/C++ is passed back to the parent process or the operating system. It's a good idea to use EXIT_SUCCESS and EXIT_FAILURE , particularly for command-line apps. In Unix (dunno about Win32), the return code can be useful when your program is being run as part of a script (I won't get into the crazy world of Unix shell scripting here).
Of course, in many cases, nobody is really listening to the return value. However, it's good to get into the habit of properly returning exit codes as defined by the language or API.
VW_Red_Jetta wrote:
Is there any alternative
to this method?
When returning from main() , EXIT_SUCCESS and EXIT_FAILURE are generally the way to go. Since you're using C++, keep in mind that within your app, returning a bool or int is not the only way to signal an error has occurred. Exceptions are a powerful and versatile system for dealing with error conditions, but can be overkill for dealing with simple errors.
- Mike
|
|
|
|
|
I have seen tons of reference books doing this. They
put the braces right after the function names or class
names. For example,
instead of doing this:
class Cat
{
// codes
};
they do this:
class Cat {
// codes
};
But obviously, the first one is much clear than the
second one and not likely to make mistakes too.
What do you thinks? Especially those real world
programmers, what is your habit and why?
|
|
|
|
|
When I first started coding in C, I once spent 1.5 hours looking for an error in some example code that I was playing with. The error turned out to be a misplace } because the author was doing the :
class Foo {
// blah blah blah
}
Ever since then I have lined the braces up. Never wasted time trying to find the matching brace ever since.
Do Lipton employees get coffee breaks?
|
|
|
|
|
i would say off hand it's because it doesn't really matter.
I put the braces where ever I feel like at the given time.
hey
|
|
|
|
|