Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] returning CHAR

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Heartless
I post too much
Reputation: 0

Joined: 03 Dec 2006
Posts: 2436

PostPosted: Sat Mar 08, 2008 6:57 pm    Post subject: [C++] returning CHAR Reply with quote

Main.cpp
Code:

#include <windows.h>
#include <iostream>
using namespace std;
#include "Header.h"
int main()
{
     //Gets Activation Code From Activation.ini
     CHAR* Activation_Code = Read_Strings("Activation", "Key", "\\Activation.ini");
     cout << Activation_Code;
}


Header.h
Code:

#include <windows.h>
CHAR* Read_Strings(CHAR*, CHAR*, CHAR*);


Read Ini.cpp
Code:

#include <windows.h>
#include <direct.h>
#include <string>
#include <iostream>
using namespace std;
#include "Header.h"
CHAR* Read_Strings(CHAR* Section, CHAR* Key, CHAR* File)
{
     //Declare the current path and new path
     char Current_Path[_MAX_PATH];
     char* New_Path;
     //use this function to get the path
     _getcwd(Current_Path, _MAX_PATH);
     //Get New path
     New_Path = strncat(Current_Path, File, 50);
     //Declare a string to get the string
     char Return_String[50];
     //Get String from settings.ini
     GetPrivateProfileString(Section, Key, "No Key", Return_String, sizeof(Return_String), New_Path);
     //return thing here
     cout << Return_String;
     return Return_String;
}


Output
Code:

Key
╠╠╠╠╠╠╠╠╠╠x╩T►╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╚╩T►☺╠╠╠╠╠╠╠╠╠╠╠    ╠╠╠╠╠╠╠╠8☺  ╠╠╠╠╠╠╠╠<-


I know that the string is correct before I return it, what am I doing wrong? How can I get the string to return and not have the messed up stuff.

Off Topic - Super Smash Bros. Brawl is comming out tomorrow! Who's getting it? I know I am.

_________________
What dosen't kill you, usually does the second time.
Back to top
View user's profile Send private message
killersamurai
Expert Cheater
Reputation: 0

Joined: 10 Sep 2007
Posts: 197
Location: Colorado

PostPosted: Sat Mar 08, 2008 8:28 pm    Post subject: Reply with quote

I usually make it static. You could also go the dynamic memory route, but I don't recommend that.
Code:

static char Return_String[50] = { 0 };
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Sat Mar 08, 2008 10:13 pm    Post subject: Reply with quote

Maybe i'm just having a im tired moment, but you declare the function in the Readini.cpp and the prototype in the header. You don't include readini.cpp in the header or main.cpp. It seems like readini isn't connected to the project in any way?
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sat Mar 08, 2008 11:46 pm    Post subject: Reply with quote

You are trying to return a local pointer that gets destroyed after the function is done being called. Pass a pointer to a char array as a param in the function and copy the string to the pointer.

For example:

Code:
#include <windows.h>

void Function1( char* szString )
{
   char szName[1024] = {0};
   GetEnvironmentVariable( "USERNAME", szName, 1024 );
   strcpy_s( szString, 1024, szName );
}


int main()
{
   char szName[1024] = {0};
   Function1( szName );
   return 0;
}

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Sun Mar 09, 2008 9:01 am    Post subject: Reply with quote

yea like wiccan said. But i thought i would point out that

char buf[1024] - big ass buffer is still a pointer to a char. You can pass that in a pointer situation as long as you do buf[sizeofbuffer]

And include the file in your project damit
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites