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++]Reading data from .ini
Goto page Previous  1, 2
 
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: Tue Jan 22, 2008 4:14 pm    Post subject: Reply with quote

I though these were suppose to return a string and get you the application path. Instead all I get are random numbers. Sad
_________________
What dosen't kill you, usually does the second time.
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Tue Jan 22, 2008 4:48 pm    Post subject: Reply with quote

Wiccaan wrote:
blankrider wrote:
LPTSTR cOutputFolder = NULL;

if(GetCurrentDirectory(sizeof(cOutputFolder), cOutputFolder) = 0)
//error

else{
//the cOutputFolder has the string of the folder
}


Couple of errors with your code, you are trying to set GetCurrentDirectory to 0 instead of comparing it. You forgot the first set of braces { } for the if statement as well. Along with that you are using sizeof() on an undefined size array. The sizeof() in this case will return 4 because LPTSTR is a pointer to a string. Instead, you should write it like:

Code:
   TCHAR tszCurrentDirectory[MAX_PATH] = {0};
   if( GetCurrentDirectory( sizeof(tszCurrentDirectory), tszCurrentDirectory ) == 0 ){
      // Success
   }else{
      // Error
   }


Just thought i'd point out that 0 is the error value. so switch success and error

_________________
Back to top
View user's profile Send private message
Heartless
I post too much
Reputation: 0

Joined: 03 Dec 2006
Posts: 2436

PostPosted: Tue Jan 22, 2008 5:23 pm    Post subject: Reply with quote

Nevermind I found it.
_________________
What dosen't kill you, usually does the second time.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Tue Jan 22, 2008 8:16 pm    Post subject: Reply with quote

Actually, I wrote the cases wrong, if the return is 0 from GetCurrentDirectory, that means it failed. So the first part of the if should be the fail code, 2nd being the success. As for the fail, no reason to switch the error, just use GetLastError() to obtain the number and then FormatMessage it for the detailed info. (Reason why I suggested { } around the error to allow more then one line. I do know you can do a single line without the bracets.)

I rewrote my code with a small bit of error handling in its own function:

Code:
BOOL CurrentDirectory( TCHAR* tszBuffer )
{
   TCHAR tszCurrentDirectory[MAX_PATH] = {0};
   if( GetCurrentDirectory( _ARRAYSIZE(tszCurrentDirectory), tszCurrentDirectory ) == 0 )
   {
      //
      // Error Occured, Obtain And Display
      //
      LPVOID   lpMsgBuf;
      DWORD   dwLastError = GetLastError();
      FormatMessage(   FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
                  NULL,
                  dwLastError,
                  MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
                  (LPTSTR)&lpMsgBuf,
                  0,
                  NULL );
      MessageBox( NULL, (LPTSTR)lpMsgBuf, TEXT("ERROR -- CurrentDirectory"), MB_OK|MB_ICONERROR );
      return FALSE;
   }

   //
   // No Error Occured, Copy Path Into Buffer
   //
   _tcscpy_s( tszBuffer, MAX_PATH, tszCurrentDirectory );
   return TRUE;
}


Example of using it:

Code:
#include <windows.h>
#include <tchar.h>
#include <iostream>
using namespace std;


BOOL CurrentDirectory( TCHAR* tszBuffer )
{
   TCHAR tszCurrentDirectory[MAX_PATH] = {0};
   if( GetCurrentDirectory( _ARRAYSIZE(tszCurrentDirectory), tszCurrentDirectory ) == 0 )
   {
      //
      // Error Occured, Obtain And Display
      //
      LPVOID   lpMsgBuf;
      DWORD   dwLastError = GetLastError();
      FormatMessage(   FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
                  NULL,
                  dwLastError,
                  MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
                  (LPTSTR)&lpMsgBuf,
                  0,
                  NULL );
      MessageBox( NULL, (LPTSTR)lpMsgBuf, TEXT("ERROR -- CurrentDirectory"), MB_OK|MB_ICONERROR );
      return FALSE;
   }

   //
   // No Error Occured, Copy Path Into Buffer
   //
   _tcscpy_s( tszBuffer, MAX_PATH, tszCurrentDirectory );
   return TRUE;
}

int main()
{
   TCHAR tszPath[MAX_PATH] = {0};
   if( CurrentDirectory( tszPath ) )
      std::cout << tszPath << std::endl;
   std::cin.ignore();
   std::cin.sync();
   return 0;
}


The _tcscpy_s will fail if the buffer is not 255 characters or more, I didn't bother adding a try/catch or other error handling methods to it though. Just be sure to define your array sizes correctly via MAX_PATH for paths.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Heartless
I post too much
Reputation: 0

Joined: 03 Dec 2006
Posts: 2436

PostPosted: Tue Jan 22, 2008 9:55 pm    Post subject: Reply with quote

Some thing is not right, all I get are random numbers and 00000000.
_________________
What dosen't kill you, usually does the second time.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Tue Jan 22, 2008 10:47 pm    Post subject: Reply with quote

HornyAZNBoy wrote:
Some thing is not right, all I get are random numbers and 00000000.


Ok a few questions then:

- What OS are you using? (Service packs too.)
- What compiler are you using?


The code I posted above returns the following for me:

c:\Documents and Settings\Owner\Desktop\[ C++ ]\New Folder\asdf\asdf

Which is the path to the solution which the file is being ran from. For you to get random numbers, it makes it seem like you are using the wrong part of the return and not the string. GetCurrentDirectory returns the number of characters in the string thats being returned. So do not use the return value from it.

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

Joined: 02 Nov 2007
Posts: 1371

PostPosted: Wed Jan 23, 2008 2:25 am    Post subject: Reply with quote

This might be offtopic but how do you do the same in vb?
_________________
Intel over amd yes.
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Wed Jan 23, 2008 2:53 am    Post subject: Reply with quote

Naablet wrote:
This might be offtopic but how do you do the same in vb?


Well if you want the current folder your application is in, you can just use:

Code:
App.Path


But if you want a function like above that uses the API you can do:

Code:
Option Explicit

Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const LANG_NEUTRAL = &H0
Private Const SUBLANG_DEFAULT = &H1

Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
Private Declare Function GetCurrentDirectory Lib "kernel32" Alias "GetCurrentDirectoryA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long

Public Function CurrentDirectory() As String
    Dim strTempPath As String
    strTempPath = String$(255, Chr$(0))
   
    If GetCurrentDirectory(255, strTempPath) = 0 Then
        Dim strErrorBuffer As String
        strErrorBuffer = Space(255)
       
        FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, GetLastError, LANG_NEUTRAL, strErrorBuffer, 255, ByVal 0&
        MsgBox strErrorBuffer, vbCritical, "ERROR - CurrentDirectory"
        Exit Function
    End If
   
    CurrentDirectory = strTempPath
End Function


Then to use it, you can do:

Code:
Private Sub Form_Load()
    Text1.Text = CurrentDirectory
End Sub

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

Joined: 02 Nov 2007
Posts: 1371

PostPosted: Wed Jan 23, 2008 2:56 am    Post subject: Reply with quote

I meant how to read variable values from a ini file, in vb Wink
_________________
Intel over amd yes.
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
Pseudo Xero
I post too much
Reputation: 0

Joined: 16 Feb 2007
Posts: 2607

PostPosted: Wed Jan 23, 2008 5:42 am    Post subject: Reply with quote

Naablet wrote:
I meant how to read variable values from a ini file, in vb Wink

Google is your friend.

_________________
haxory' wrote:
can't VB do anything??
windows is programmed using VB right? correct me if im wrong.

so all things in windows you have like the start menu is a windows form too.
Back to top
View user's profile Send private message
NINTENDO
Grandmaster Cheater Supreme
Reputation: 0

Joined: 02 Nov 2007
Posts: 1371

PostPosted: Wed Jan 23, 2008 8:08 am    Post subject: Reply with quote

Xenophobe wrote:
Naablet wrote:
I meant how to read variable values from a ini file, in vb Wink

Google is your friend.

And what will i search for?

I know how to open a file in vb6 and vb8 but still that dosen't help a bit.

_________________
Intel over amd yes.
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Wed Jan 23, 2008 10:33 am    Post subject: Reply with quote

http://allapi.mentalis.org/apilist/GetPrivateProfileInt.shtml
http://allapi.mentalis.org/apilist/GetPrivateProfileString.shtml
http://allapi.mentalis.org/apilist/WritePrivateProfileString.shtml

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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