Go Back   The PokéCommunity Forums > ROM Hacking > Tools, Tutorials & Resources
Reload this Page [Tutorial] C/C++ tutorial for the masses

Notices
For all updates, view the main page.

Tools, Tutorials & Resources Various tools to help you develop your hacks can be found here.
New threads in this forum are to be approved by a moderator before they are displayed.



Reply
 
Thread Tools
  #1    
Old August 3rd, 2008 (12:56 PM). Edited April 15th, 2010 by hashtag.
interdpth interdpth is offline
I've seen things, man.
 
Join Date: Jul 2004
Posts: 275
Open up VC++ or any other C/C++ compiler make a new commandline project
open a blank c/cpp file paste this into it

#include <stdio.h>//Basic headers I always use
#include <windows.h>




int main(){
FILE *fp = NULL;// The file we'll be workin with
char FilePath[512]={0};//The zero is to make sure it's all zeroed out otherwise there could be weird values this is an array BTW
unsigned char Var1=0;//Our 8 bit variable set it to zero read above
unsigned short Var2=0;//unsigned is for values 0 to other stuff no negatives
unsigned long Var3=0;
printf("This program will grab a\n 8 bit variable, a 16 bit variable, a 32 bit variable\n"); // \n tells it to go to a new line
while(1){ // Infinite loop
printf("Please enter a file path\n");
scanf("%s",FilePath);//It knows to expect a string for input
fp = fopen(FilePath,"r+b");//Open for reading and writing to binary
if(fp){//Check to make sure the file opening succeeded
fseek(fp,5,SEEK_SET);//Hop to offset 5
Var1=fgetc(fp);
fread(&Var2,2,1,fp);
fread(&Var3,4,1,fp);
printf("Var1: 0x%X\n",Var1);//Print in hexadecimal output
printf("Var2: 0x%X\n",Var2);
printf("Var3: 0x%X\n",Var3);
printf("Yay we made it thus far,\nnow on each line please input a new value in hexadecimal output\n");
scanf("%8x",&Var1);//read from the commandline
scanf("%16x",&Var2);
scanf("%32x",&Var3);
printf("You entered for Var1: 0x%X\n", Var1);
printf("You entered for Var2: 0x%X\n", Var2);
printf("You entered for Var3: 0x%X\n", Var3);
printf("Now inserting into ROM\n");
fseek(fp,5,SEEK_SET);
fputc(Var1, fp);
fwrite(&Var2,2,1,fp);
fwrite(&Var3,4,1,fp);
fclose(fp);
}else{
printf("File opening failed");
}

printf("Ok, we're done please type 0 to exit anything else will restart the program\n");
scanf("%d",Var1);
if(Var1==0) return 0;
}


return 0;

}


compile it and run it


i'll do a basic windows application later
enjoy =p
__________________
yes, this is doge.

potato is not here.

bye
Reply With Quote
  #2    
Old August 5th, 2008 (07:01 AM).
ckk's Avatar
ckk ckk is offline
 
Join Date: Jun 2008
Gender:
Posts: 23
This tutorial.......
i dont understand a thing......
why don't you start with simple commands and then add on instead of tyin up one huge one and explaining i all
that way our brains can absorb little by little and it'll be easier to learn wats bein told
Reply With Quote
  #3    
Old August 5th, 2008 (07:20 AM).
X-aveon's Avatar
X-aveon X-aveon is offline
IS BACK!!!!!!!!!!!!!!!!!!!
 
Join Date: Jan 2008
Gender:
Nature: Naive
Posts: 233
Send a message via Yahoo to X-aveon
uh............ first of all, what is this about,
and second of all, it sounded like ur a scientist.
in english, please?
__________________


I want that apple......
Reply With Quote
  #4    
Old August 9th, 2008 (12:31 AM).
Cy-Chan's Avatar
Cy-Chan Cy-Chan is offline
GSC Hacker
 
Join Date: Jan 2005
Location: UK, England.
Age: 23
Gender:
Nature: Timid
Posts: 152
Send a message via Windows Live Messenger to Cy-Chan
It's a Dos program made in C++ to create some sort of hacking utility.

As far as I recognise, it's designed to grab an 8-bit, 16-bit and 32-bit variable. I assume it's simply showcasing the different code required, so that people can use it as reference for creating hacking tools in C++.

It's quite enlightening for me; I always assumed C++ was big, scary and made no freaking sense, but this is quite simple. Great tutorial.
Reply With Quote
  #5    
Old August 9th, 2008 (01:46 AM).
D-Trogh's Avatar
D-Trogh D-Trogh is offline
Dead
 
Join Date: Jul 2005
Location: Belgium
Age: 23
Gender: Male
Posts: 439
Send a message via Windows Live Messenger to D-Trogh
Quote originally posted by Cy-Chan:
I assume it's simply showcasing the different code required, so that people can use it as reference for creating hacking tools in C++.

It's quite enlightening for me; I always assumed C++ was big, scary and made no freaking sense, but this is quite simple. Great tutorial.
That's exactly what this is all about
__________________
[NTME | WPEDS | HTPE | SGPE | TGPE | PDEDS]
Reply With Quote
  #6    
Old August 9th, 2008 (09:10 AM).
GBChacker GBChacker is offline
 
Join Date: Mar 2008
Posts: 18
Thats the C way not C++. It works in C++, but why use C++ with C's bad ways? In C++ we use the fstream and seekp and seekg for getting the pointers. And in C++ we definately don't use printf. Using the C++ way is much simpiler and uses a great synax. This program isn't cross platform either. We mine aswell use the crappy vb or something of the like lol. If your trying to learn to make programs or whatever, stop. Buy a book or read tutorials, theres always more than one way to do things. Its nice you posted this, but not many people will get it and the people that do will have a harder time writting a new program than if they knew what they were doing and wrote it from scratch.
Reply With Quote
  #7    
Old August 15th, 2009 (03:43 PM).
DawnRyder DawnRyder is offline
I cannot believe I'm back -.-
 
Join Date: Aug 2009
Posts: 79
This is an excellent tutorial. Thank you for sharing this, especially the offsets part (never got that part before..)

I'll probably try to write an app in c++ that adds the shiny hack.
Reply With Quote
  #8    
Old August 15th, 2009 (07:12 PM).
SharpPoint's Avatar
SharpPoint SharpPoint is offline
Suicune used Surf!
 
Join Date: Jul 2009
Location: Stalker...
Gender: Male
Nature: Adamant
Posts: 479
I'm sorry but, wheres the tutorial?
Reply With Quote
  #9    
Old August 15th, 2009 (07:56 PM).
.Seth's Avatar
.Seth .Seth is offline
.explorer.
Crystal Tier
 
Join Date: Jun 2008
Location: A capacitor in a power supply board.
Gender: Male
Nature: Quiet
Posts: 1,645
It's nice, but it'd be a lot more useful if you explained command by command.
Reply With Quote
  #10    
Old August 16th, 2009 (07:46 AM).
DawnRyder DawnRyder is offline
I cannot believe I'm back -.-
 
Join Date: Aug 2009
Posts: 79
Quote originally posted by 非常に高いレベル:
It's nice, but it'd be a lot more useful if you explained command by command.
Why would he have to explain it command by command?

Any c/c++ at the beginner/intermediate level would understand this in a snap.

If you are not into c/c++ you should not be reading this thread.
Reply With Quote
  #11    
Old August 16th, 2009 (04:09 PM).
.Seth's Avatar
.Seth .Seth is offline
.explorer.
Crystal Tier
 
Join Date: Jun 2008
Location: A capacitor in a power supply board.
Gender: Male
Nature: Quiet
Posts: 1,645
Hmm, could of sworn a tutorial was designed to teach you a subject. "C/C++ tutorial for the masses". Sounds like a C/C++ tutorial to me. He didn't mention any background information required, so technically, it's not a tutorial. More of a reference.
Reply With Quote
  #12    
Old August 16th, 2009 (07:25 PM).
liuyanghejerry's Avatar
liuyanghejerry liuyanghejerry is offline
 
Join Date: Jan 2008
Location: China,Xi'an
Nature: Calm
Posts: 218
Send a message via ICQ to liuyanghejerry Send a message via Windows Live Messenger to liuyanghejerry Send a message via Yahoo to liuyanghejerry
If you're using C++,I prefer you use cout/cin to replace printf().Because that's what C++ suggests.
__________________
Zel,thethethethe,LU-HO,Darthatron,HackMew,ZodiacDaGreat,Juan,score_under,JPAN,Tamah-chan,I really appreciate your kindness and your help!:D


I did something that really bad.But made all Chinese can hack Pokemon, too.If you guys hate me, I totally understand,but cannot do anything but force to keep everything.
If there must be someone to undertake all spit and curse, it must be me.
Reply With Quote
  #13    
Old August 17th, 2009 (05:12 PM).
DawnRyder DawnRyder is offline
I cannot believe I'm back -.-
 
Join Date: Aug 2009
Posts: 79
Quote originally posted by 非常に高いレベル:
Hmm, could of sworn a tutorial was designed to teach you a subject. "C/C++ tutorial for the masses". Sounds like a C/C++ tutorial to me. He didn't mention any background information required, so technically, it's not a tutorial. More of a reference.
It's entitled, 'C/C++ tutorial for the masses', the masses that program in c/c++.

Like I said, If you don't program in c/c++ DONT post in this thread.
__________________
I believe in Jesus Christ my Savior. If you do too, and aren't scared to admit it, then copy and paste this in your signature.

Sorry guys, but I'm leaving Pokécommunity. I just don't have time for it. As of right now you will no longer be able to contact me.
Reply With Quote
  #14    
Old August 17th, 2009 (07:12 PM).
colcolstyles's Avatar
colcolstyles colcolstyles is offline
Yours truly
Crystal Tier
 
Join Date: May 2008
Location: The Bay Area
Gender: Male
Nature: Lonely
Posts: 1,592
Quote originally posted by interdpth:
char FilePath[512]={0};//The zero is to make sure it's all zeroed out otherwise there could be weird values this is an array BTW
I'm new to C++ so forgive me if I make absolutely no sense but I don't believe you can perform aggregate assignment to an array in C++. Therefore, this assigns only the first item in the array to 0. Wouldn't something like:
Code:
for (count = 0; count < 512; count++)
    FilePath[count] = 0;
be more appropriate?

I did some tests and I'm pretty sure that your code won't initialize the array properly but I'm a little hesitant to question your code given your reputation as a programmer of ROM Hacking tools
__________________

Brother of Vrai
Reply With Quote
  #15    
Old August 17th, 2009 (08:35 PM).
SharpPoint's Avatar
SharpPoint SharpPoint is offline
Suicune used Surf!
 
Join Date: Jul 2009
Location: Stalker...
Gender: Male
Nature: Adamant
Posts: 479
Quote originally posted by DawnRyder:
It's entitled, 'C/C++ tutorial for the masses', the masses that program in c/c++.

Like I said, If you don't program in c/c++ DONT post in this thread.
That still doesn't explain why the word 'tutorial' is used in the title, regardless whether or not I program in C/C++.
Reply With Quote
  #16    
Old August 17th, 2009 (10:06 PM). Edited August 17th, 2009 by IIMarckus.
IIMarckus's Avatar
IIMarckus IIMarckus is offline
J946@5488AA97464
 
Join Date: Oct 2007
Gender:
Posts: 402
Quote originally posted by colcolstyles:
I'm new to C++ so forgive me if I make absolutely no sense but I don't believe you can perform aggregate assignment to an array in C++. Therefore, this assigns only the first item in the array to 0. Wouldn't something like:
Code:
for (count = 0; count < 512; count++)
    FilePath[count] = 0;
be more appropriate?

I did some tests and I'm pretty sure that your code won't initialize the array properly but I'm a little hesitant to question your code given your reputation as a programmer of ROM Hacking tools
Since the filename is never read before being changed through scanf, why zero the array at all?

Personally, I would do it this way:
Code:
#include <stdio.h>

int main(int argc,char* argv[]){
	FILE *fp = NULL;

	if(argc != 2)
	{
		printf("Usage: progname filename\n");
		exit(1);
	}

	/* assuming argv[1] is a file path */

	unsigned int eightBit = 0, sixteenBit = 0, thirtytwoBit = 0;

	fp = fopen(argv[1],"rb");

	if(fp == NULL)
	{
		perror("Error opening file");
		exit(1);
	}

	fseek(fp,5,SEEK_SET);

	/* read and write bytes ... */

	fclose(fp);

	return 0;
}
And encapsulate it in a batch file if people really want looping.
__________________
iimarck.us / hax.iimarck.us

If you want me, please contact me by email. [email protected]

If you want to hack Pokémon RBY or GSC, read, read, and read some more. This has tons of valuable information.

Pokémon Red disassembly project

Rules that should be rethought: 25charlimit, bumping.
Bad posts are bad posts, regardless of how many words are in them or how old the thread is.
Good posts are good posts, regardless of how old the thread is—and brevity is underrated.
Reply With Quote
  #17    
Old August 18th, 2009 (04:43 AM).
score_under's Avatar
score_under score_under is offline
I program the *other* ASM.
Crystal Tier
 
Join Date: Aug 2005
Location: Hertfordshire, England
Age: 22
Nature: Rash
Posts: 525
Quote originally posted by GBChacker:
Thats the C way not C++. It works in C++, but why use C++ with C's bad ways?
C has no "bad ways". In fact, the way C++ works pretty much blinds the programmer to what's going on behind the scenes. The fact that you can use = on a string in C++ shows a lot about how abstracted it is.

Quote:
In C++ we use the fstream and seekp and seekg for getting the pointers. And in C++ we definately don't use printf. Using the C++ way is much simpiler and uses a great synax. This program isn't cross platform either.
It is if you remove #include <windows.h> , because that's completely unnecessary in this program.

Quote:
We mine aswell use the crappy vb or something of the like lol. If your trying to learn to make programs or whatever, stop. Buy a book or read tutorials, theres always more than one way to do things. Its nice you posted this, but not many people will get it and the people that do will have a harder time writting a new program than if they knew what they were doing and wrote it from scratch.
I never read a book on C++, but I am willing to bet that I can code faster, smaller, more efficent, and generally less buggy code than Interdpth, given his example in this forum.
Sorry for being so blunt, but seeing people who think they know C or C++ completely abuse it and refuse to think that they might just be a little fundamentally wrong really gets to me.
If you know C, you will know that...
- "long" is not a type
- Assigning a char* to another char* will not copy the string, it will copy the reference.
- fopen() is not descriptive enough on Windows - you can get all sorts of errors on Windows, it's best to use the API with GetLastError().
- if() executes for anything but 0, and a faster way of checking if(x==NULL) or if(x==0) is simply if(!x).
- There are easy ways to make a program compile on Windows, Linux, Mac, for Unicode or ASCII, using windows or command-line interface, all with the same source.
- You can do anything from C if you know how. You can even access .NET if you know how.
__________________
Plz not to PM me, I don't come here often enough. Email if you need me
Reply With Quote
  #18    
Old August 18th, 2009 (01:33 PM). Edited August 18th, 2009 by DawnRyder.
DawnRyder DawnRyder is offline
I cannot believe I'm back -.-
 
Join Date: Aug 2009
Posts: 79
Quote originally posted by SharpPoint:
That still doesn't explain why the word 'tutorial' is used in the title, regardless whether or not I program in C/C++.
This 'TUTORIAL' is showing 'C/C++ PROGRAMMERS' how to access certain offsets an obtain bit variables.
Like 'OFFSETS'.

Jeeze...

And BTW, listen to score_under he's good.
__________________
I believe in Jesus Christ my Savior. If you do too, and aren't scared to admit it, then copy and paste this in your signature.

Sorry guys, but I'm leaving Pokécommunity. I just don't have time for it. As of right now you will no longer be able to contact me.
Reply With Quote
  #19    
Old August 18th, 2009 (06:38 PM).
colcolstyles's Avatar
colcolstyles colcolstyles is offline
Yours truly
Crystal Tier
 
Join Date: May 2008
Location: The Bay Area
Gender: Male
Nature: Lonely
Posts: 1,592
Quote originally posted by IIMarckus:
Since the filename is never read before being changed through scanf, why zero the array at all?
Good programming practice?
Yes, technically it isn't necessary to initialize the array but if interdpth ever writes a program where he does need to zero out an array, he might run into some problems. Here, it's not needed but hey, it can't hurt, can it?

Anyways, I would just like to say that, for what it's worth, this tutorial helped me out quite a bit. I've been able to familiarize myself with some of the basic C commands (which, unfortunately, were not covered (as you can probably imagine) in my C++ class) and with that knowledge, I was able to write a simple command line utility that edits ROM headers. Thanks to interdpth for the tutorial and IIMarckus for clarifying a few things for me.
__________________

Brother of Vrai
Reply With Quote
  #20    
Old January 11th, 2010 (06:24 PM).
OM3GA Umbr30n's Avatar
OM3GA Umbr30n OM3GA Umbr30n is offline
Was Mazot2
 
Join Date: Jun 2009
Location: UK
Age: 17
Gender: Male
Nature: Relaxed
Posts: 123
If you done understand you dont need to know and seeing me here you are thinking,
1. Why revive an old topic?
to give help
2.What is a noob doing here?
well im not so noobish now as i have decided to learn C++ i actually understand alot of this and as i said if you dont understand you dont need to,
this i a tutorial for the masses of people that use C++ on the basic code for a Commandline/Console Application for Hacking,
I cant wait for the basic GUI version.
Also out of curiosity what compiler do you use?
__________________

Flareon's are Red,
Vaporeon's Blue,
Espeon is my concious,
Umbreon is my life-style

~Umbreon
Reply With Quote
  #21    
Old June 8th, 2010 (04:07 PM).
Umbreonic Aura Umbreonic Aura is offline
 
Join Date: Mar 2010
Gender: Male
Posts: 42
Is the basic windows application ever going to happen?
__________________



Trainer Cards
Spoiler:
Reply With Quote
Reply
Quick Reply

Sponsored Links
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are UTC -8. The time now is 05:49 PM.