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 


File Mapping Between A Delphi App, and a C++ DLL [SOLVED]

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
FerrisBuellerYourMyHero
Master Cheater
Reputation: 0

Joined: 14 Feb 2007
Posts: 401
Location: Inside your <kernel>

PostPosted: Tue Feb 19, 2008 7:16 pm    Post subject: File Mapping Between A Delphi App, and a C++ DLL [SOLVED] Reply with quote

Ok so, I code my bot in C++(because I Like it better and its easier for me)

and I have a GUI made in delphi!(because coding GUI's in c++ is difficult and time consuming)

the delphi app calls load library to load the dll... I then have a c++ dll working inside of a delphi app... currently what im using to have them communicate with each other is what I call a sketchy method... the delphi GUI constantly reads a text file in the same folder, to check if things are turned on /off... and the C++ dll saves to the file whenever a hotkey is pressed, 1 for on 0 for off, and the delphi app updates the status accordingly... the c++ dll also constantly reads the text file to see if the user clicked on or off on the gui(of one of the features of the bot)

the problem with that is, sometimes both programs try to read/write to the text files at the same time, causing the delphi app to crash with an error...

I read about file mapping but can't figure out how to do it between c++ and delphi...

How can both programs read the same variables without crashing...

in delphi i have something like this:

Code:

MappingName:= 'Global//MSBOT2';
comm:=OpenFileMapping(FILE_MAP_ALL_ACCESS, true, MappingName);
    begin
      Nfo:=MapViewOfFile(comm,FILE_MAP_ALL_ACCESS,0,0,0);
      if Nfo=nil then
      begin
        closehandle(Nfo);
        label6.caption:='Failure';
      end;

      label6.caption:='File map opened successfully';

      sleep(1000);

      label7.caption:=(dword)comm;
      zeromemory(Nfo,sizeof(Tcommunication));
      label8.caption:=comm.SzMsg;

      makecopy;




in c++ i have this:

Code:

TCHAR szName[]=TEXT("Global\\MSBOT2");
const char* szMsg = "Yay it WORKZ!!!";
HANDLE hMapFile;
LPCTSTR pBuf;

hMapFile = CreateFileMapping(
                 INVALID_HANDLE_VALUE,    // use paging file
                 NULL,                    // default security
                 PAGE_READWRITE,          // read/write access
                 0,                       // max. object size
                 BUF_SIZE,                // buffer size 
                 szName);                 // name of mapping object
 
   if (hMapFile == NULL)
   {
      printf("Could not create file mapping object (%d).\n",
             GetLastError());
   }
   

    hMapFile = OpenFileMapping(
                   FILE_MAP_ALL_ACCESS,   // read/write access
                   FALSE,                 // do not inherit the name
                   szName);               // name of mapping object
 
   if (hMapFile == NULL)
   {
      printf("Could not open file mapping object (%d).\n",
             GetLastError());
   }
   pBuf = (LPTSTR) MapViewOfFile(hMapFile,   // handle to map object
                        FILE_MAP_ALL_ACCESS, // read/write permission
                        0,                   
                        0,                   
                        BUF_SIZE);         
 
   if (pBuf == NULL)
   {
      printf("Could not map view of file (%d).\n",
             GetLastError());
   }

   CopyMemory((PVOID)pBuf, szMsg, strlen(szMsg));
   _getch();

   UnmapViewOfFile(pBuf);

   CloseHandle(hMapFile);


In delphi it doesn't display "Yay It Workz" on label 8

thanks for your help

Very Happy

_________________
You know, life moves pretty fast. If you don't stop and look around once in a while, You could miss it!



Last edited by FerrisBuellerYourMyHero on Sat Feb 23, 2008 6:12 pm; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Tue Feb 19, 2008 7:21 pm    Post subject: Reply with quote

how about you inject the Dll into the delphi app and then they share memory >.>
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Tue Feb 19, 2008 7:27 pm    Post subject: Reply with quote

blankrider wrote:
how about you inject the Dll into the delphi app and then they share memory >.>


Doesn't that just seem a wee bit pointless, and overdoing it?

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
sponge
I'm a spammer
Reputation: 1

Joined: 07 Nov 2006
Posts: 6009

PostPosted: Tue Feb 19, 2008 7:54 pm    Post subject: Reply with quote

Pipe your data?
_________________
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Tue Feb 19, 2008 8:05 pm    Post subject: Reply with quote

Samurai, creating a file just to share data isn't pointless?
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Tue Feb 19, 2008 8:23 pm    Post subject: Reply with quote

I never said it wasn't. =P

Maybe you could create a callback in Delphi, so you call the init method in the dll, passing in the callback method, and the callback does whatever?

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Tue Feb 19, 2008 8:43 pm    Post subject: Reply with quote

Or he could port over the gui using CreateWindow and the properties that delphi sets. Thats how i make my guis.
Back to top
View user's profile Send private message
DoomsDay
Grandmaster Cheater
Reputation: 0

Joined: 06 Jan 2007
Posts: 768
Location: %HomePath%

PostPosted: Tue Feb 19, 2008 11:57 pm    Post subject: Reply with quote

As sponge suggested, CreateNamedPipe is the best solution, msdn gives a few examples for this:
http://msdn2.microsoft.com/en-us/library/aa365588(VS.85).aspx
ttp://msdn2.microsoft.com/en-us/library/aa365592(VS.85).aspx
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 475

Joined: 09 May 2003
Posts: 25974
Location: The netherlands

PostPosted: Wed Feb 20, 2008 4:25 am    Post subject: Reply with quote

in C you type "Global\\MSBOT2" to get the string "Global\MSBOT2"

In delphi there is no such problem yet you do
Code:

MappingName:= 'Global//MSBOT2';

which is twice wrong. and no, double wrong doesn't give positive here

try it with:
Code:

MappingName:= 'Global\MSBOT2';

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
FerrisBuellerYourMyHero
Master Cheater
Reputation: 0

Joined: 14 Feb 2007
Posts: 401
Location: Inside your <kernel>

PostPosted: Fri Feb 22, 2008 7:48 pm    Post subject: Reply with quote

Thanks Dark Byte! Somehow I didn't notice that the slashes were backwards! Laughing

Any way I almost got this working for me! I have it working in delphi correctly but I'm having a compiling error in Visual C++ 2008

I think I need to have a record in delphi, and a struct in c++...

I have this in delphi, in fmap.pas which is on my uses list, in the main application, in Unit1.pas

Code:

// fmap.pas
unit fmap;

interface

uses windows;

type
    FilemapRecord = record
    Dmg: DWORD;
    Godmode: integer;
    SStele: integer;
    SpeedAtk: integer;
    SpeedAtkDelay: integer;
    DXon: integer;
    hp: integer;
    maxhp: integer;
    mp: integer;
    maxmp: integer;
    exp: integer;
    maxexp: integer;
    yy: integer;
    xx: integer;
end;
implementation

end.


then in Unit1.pas I have this under var:

Code:

  GM: integer;
  fMapHandle: thandle;
  fileMap: thandle;
  fMap: ^FilemapRecord;


I have this in my form create:

Code:

fileMap := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, 1000, pchar('ferristrainer'));

fMapHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, TRUE,  'ferristrainer');
if (fMapHandle <> 0) then
    begin
    fMap := MapViewOfFile(fMapHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
    end;


then I can successfully write to it, and read from it in delphi...

for example if I click a check box it to turn godmode on, it would
Code:

Dmgstring:= damage.Text;
fMap.Dmg:= StrToInt(Dmgstring);
fMap.Godmode:= 1;


then In a created thread, I would want to constantly read from the filemap so the GUI can tell when If i press a hotkey instead of click a check box, to uncheck the box or vise versa...

Code:

function WorkerThread(Ptr : Pointer) : LongInt; stdcall;
begin
repeat

Sleep(10);

GM:= fMap.Godmode;

if GM = 1 then
Form1.CheckBox1.Perform(BM_SETCHECK, 1, 0)
else
Form1.CheckBox1.Perform(BM_SETCHECK, 0, 0);

until False;

end;


well you get the idea... I'll add in more once I get it working...

in C++ im having a weird error...

error C2059: syntax error : ')'

And I don't really understand it... Ive never seen that before..

anyway I have this at the top:

Code:

struct FileMap
{
   DWORD Dmg;
        int Godmode;
   int SStele;
   int SpeedAtk;
   int SpeedAtkDelay;
   int DXon;
        int hp;
        int maxhp;
        int mp;
        int maxmp;
        int exp;
        int maxexp;
   int yy;
   int xx;

};


then in my main thread where I want to open the filemap, and write to it(also read once i get it working) heres where my problem is...

Code:

   FileMap* FileMap;
   HANDLE hFMap = NULL;
   void* fDataMap = NULL;

   fDataMap = OpenFileMappingA(FILE_MAP_ALL_ACCESS, false, "ferristrainer");

   if (fDataMap != NULL) // check if we have a valid mapping
   {
      hFMap = MapViewOfFile(fDataMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);

      if (hFMap != NULL) // check to see if we have a valid view
      {
         // Copy the the data from the shared memory file into our local stucture.
         FileMap = (FileMap*)hFMap;
      }
   }



everything compiles fine except the

FileMap = (FileMap*)hFMap;

line... how can I copy data to and from the local structure to the file map...

then once I get that working I'll just be able to:
if I pressed godmode hotkey
Code:

FileMap.Godmode = Godmode;
// code for copying it to the shared memory goes here


Im so close ! How can I modify it to compile... Im Using Microsoft Visual C++ 2008

thanks! Surprised

_________________
You know, life moves pretty fast. If you don't stop and look around once in a while, You could miss it!

Back to top
View user's profile Send private message MSN Messenger
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Sat Feb 23, 2008 6:16 am    Post subject: Reply with quote

Maybe this will help, I have some old code that I used for a hook for a game I play to pass settings from a console to the hook once it was loaded into the game. The struct for the settings was:

Code:
struct SettingsStruct
{
   // Default Values
   SettingsStruct()
   {
      szwGamePath[0]            = 0;
      szwHookPath[0]            = 0;
      iLanguage            = 0;
      windowXRes            = 0;
      windowYRes            = 0;
      bFullScreen            = false;
      bHideBorder            = false;
      bIsLoaded            = false;
   }

   wchar_t szwGamePath[MAX_PATH];
   wchar_t szwHookPath[MAX_PATH];
   int      iLanguage;
   int      windowXRes;
   int      windowYRes;
   bool   bFullScreen;
   bool   bHideBorder;
   bool   bIsLoaded;
};


Then the code to obtain the structure from the MMF:

Code:
SettingsStruct MMFHandler::GetSettings()
{
   HANDLE m_FileMapping      = NULL;
   LPVOID m_ViewOfFile         = NULL;
   SettingsStruct* pSettings   = NULL;

   // Open The Launchers Settings MMF
   m_FileMapping = OpenFileMapping( FILE_MAP_ALL_ACCESS, FALSE, L"MMF_SETTINGS_HANDLER" );
   if( m_FileMapping != NULL )
   {
      // Copy View Of File To This Instance Memory
      m_ViewOfFile = MapViewOfFile( m_FileMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
      if( m_ViewOfFile != NULL )
      {
         // Copy Memory Into Settings Struct
         pSettings = (SettingsStruct*)m_ViewOfFile;
      }
   }

   // Pull Settings And Load Them
   SettingsStruct m_Settings;
   if( pSettings != NULL )
   {
      m_Settings = *pSettings;
      pSettings->bIsLoaded = true;
   }

   // Close MMF Handle And Cleanup
   if( m_FileMapping != NULL )
   {
      // Unmap The View Of The File
      if( m_ViewOfFile != NULL )
      {
         UnmapViewOfFile( m_ViewOfFile );
      }
      CloseHandle( m_FileMapping );
   }

   // Clear Out The Old Struct
   pSettings = NULL;

   return m_Settings;
}

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

Joined: 14 Feb 2007
Posts: 401
Location: Inside your <kernel>

PostPosted: Sat Feb 23, 2008 6:11 pm    Post subject: Reply with quote

Wiccaan wrote:
Maybe this will help, I have some old code that I used for a hook for a game I play to pass settings from a console to the hook once it was loaded into the game. The struct for the settings was:





Thanks Alot Wic!!!!! Very Happy

Your source code provided compiled fine and WORKED!!!

I owe you one!

Now I have a better way of communication between delphi and c++, than sloppy text files!

PROBLEM SOLVED

thanks again!

_________________
You know, life moves pretty fast. If you don't stop and look around once in a while, You could miss it!

Back to top
View user's profile Send private message MSN Messenger
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Sun Feb 24, 2008 3:27 am    Post subject: Reply with quote

FerrisBuellerYourMyHero wrote:
Wiccaan wrote:
Maybe this will help, I have some old code that I used for a hook for a game I play to pass settings from a console to the hook once it was loaded into the game. The struct for the settings was:





Thanks Alot Wic!!!!! Very Happy

Your source code provided compiled fine and WORKED!!!

I owe you one!

Now I have a better way of communication between delphi and c++, than sloppy text files!

PROBLEM SOLVED

thanks again!


Not a problem. And you don't "owe" me anything.

_________________
- 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
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