 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
FerrisBuellerYourMyHero Master Cheater
Reputation: 0
Joined: 14 Feb 2007 Posts: 401 Location: Inside your <kernel>
|
Posted: Tue Feb 19, 2008 7:16 pm Post subject: File Mapping Between A Delphi App, and a C++ DLL [SOLVED] |
|
|
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
 _________________
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 |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Tue Feb 19, 2008 7:21 pm Post subject: |
|
|
| how about you inject the Dll into the delphi app and then they share memory >.> |
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Tue Feb 19, 2008 7:27 pm Post subject: |
|
|
| 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? _________________
|
|
| Back to top |
|
 |
sponge I'm a spammer
Reputation: 1
Joined: 07 Nov 2006 Posts: 6009
|
Posted: Tue Feb 19, 2008 7:54 pm Post subject: |
|
|
Pipe your data? _________________
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Tue Feb 19, 2008 8:05 pm Post subject: |
|
|
| Samurai, creating a file just to share data isn't pointless? |
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Tue Feb 19, 2008 8:23 pm Post subject: |
|
|
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? _________________
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Tue Feb 19, 2008 8:43 pm Post subject: |
|
|
| Or he could port over the gui using CreateWindow and the properties that delphi sets. Thats how i make my guis. |
|
| Back to top |
|
 |
DoomsDay Grandmaster Cheater
Reputation: 0
Joined: 06 Jan 2007 Posts: 768 Location: %HomePath%
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 475
Joined: 09 May 2003 Posts: 25974 Location: The netherlands
|
Posted: Wed Feb 20, 2008 4:25 am Post subject: |
|
|
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 |
|
 |
FerrisBuellerYourMyHero Master Cheater
Reputation: 0
Joined: 14 Feb 2007 Posts: 401 Location: Inside your <kernel>
|
Posted: Fri Feb 22, 2008 7:48 pm Post subject: |
|
|
Thanks Dark Byte! Somehow I didn't notice that the slashes were backwards!
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!  _________________
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 |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Sat Feb 23, 2008 6:16 am Post subject: |
|
|
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 |
|
 |
FerrisBuellerYourMyHero Master Cheater
Reputation: 0
Joined: 14 Feb 2007 Posts: 401 Location: Inside your <kernel>
|
Posted: Sat Feb 23, 2008 6:11 pm Post subject: |
|
|
| 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!!!!!
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 |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
|
| Back to top |
|
 |
|
|
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
|
|