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 


Passing parameters/variables from CE Lua to other apps.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Thu Apr 09, 2020 8:47 am    Post subject: Passing parameters/variables from CE Lua to other apps. Reply with quote

In VB or C# I can pass variables from one app to other apps, for example:

Code:
'sender app. Send textbox text to receiver app as a variable

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     Process.Start(My.Application.Info.DirectoryPath + "/receiver.exe", TextBox1.Text)
 End Sub


'receiver app. Receive textbox text from the sender app as a variable and set as Label1 text.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim receiveVariable As String = Command()
    Label1.Text = receiveVariable
    Label1.Refresh()
End Sub


Of course, I can store the variables from the CE Lua script as a text file or copy to clipboard and then use it in other apps by reading the text file or get texts from the clipboard. But I think there is other way to do this. Maybe using stream reader or string builder/string list ?. Any examples?

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1549

PostPosted: Thu Apr 09, 2020 1:39 pm    Post subject: Reply with quote

I may not have understood correctly. Sorry for the unnecessary post already.

Just an idea;
Encode the current command as "vbscript".
Temporarily extract ".vbScript" to the TEMP folder and enable it.
When closing the trainer, delete "vbScript" from the temp folder.

Example:
https://forum.cheatengine.org/viewtopic.php?t=610493

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
panraven
Grandmaster Cheater
Reputation: 62

Joined: 01 Oct 2008
Posts: 961

PostPosted: Thu Apr 09, 2020 2:54 pm    Post subject: Reply with quote

Probably related info
https://en.wikipedia.org/wiki/Inter-process_communication

For instance, hackerish ipc example,
ie. command ce lua from injected codes in another process (for cheat purpose)
https://www.cheatengine.org/forum/viewtopic.php?t=577027

For data interchange between a csharp app and ce (or ce and ce), for example, a custom named pipe server, socket, shared memory etc. may be more handy than direct file io / external program execution.

_________________
- Retarded.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Thu Apr 09, 2020 10:05 pm    Post subject: Reply with quote

Thanks, @panraven, I need time to read and learn for contents from the link you provided.

Meanwhile, I am using these methods:

1. Use writeToClipboard()

Example (CT Lua script) :

Code:
-- in Sender App:
v_link = textbox1.Text
writeToClipboard(v_link)

-- in Receiver App
label1.Caption = pasteFromClipboard()


Problem:
How if I have more than one variable?.
I need concatenation some variables as a new variable and then write this new variable to clipboard. When calling this new variable in another app, I need to parse that variable into some defined variables.


2. Using a file to save variables

Example (CT Lua script) :

Code:
-- Sender App
local f = assert(io.open(path.."globaldata.txt", "w"))
 f:write(vname,"\n")
 f:write(vgender,"\n")
 f:write(vbirthdate,"\n")
 f:close()

-- Receiver app
local f = assert(io.open(path.."globaldata.txt", "r"))
 g_name = f:read("*line")
 g_gender = f:read("*line")
 g_bdate = f:read("*line")
 f:close()
 tbox_name.Text = g_name
 tbox_gender.Text = g_gender
 tbox_birthdate.Text = g_bdate


Problem:
Not yet try how it works with not CT or Lua app (i.e VB, Java, etc), but it doesn't really matter.

3. Global Variable (two CT file apps)

Code:
--- Sender.CT
_G.name = 'Corroder'


-- Receiver.CT
print(_G.Name)


not work.


4. Didn't try using a table or meta table or string stream.

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
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: Fri Apr 10, 2020 5:28 pm    Post subject: Reply with quote

This is an ideal situation for sockets or shared memory.

LuaSockets if you want to go the socket route.

For shared memory, memory-mapped files works fine and CE supports this already:
- allocateSharedMemory

You can create the shared memory with a given name and then access it in both your own program and CE. Open the shared memory in your program in the same manner, via:
- CreateFileMapping / OpenFileMapping
- MapViewOfFile

Then you can read/write to the shared memory between both things.

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

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Fri Apr 10, 2020 7:07 pm    Post subject: Reply with quote

Thanks, atom0s. I have read this:

https://forum.cheatengine.org/viewtopic.php?p=5643897&sid=27c0ea15b9ff68ad80f4a9522e6e53fb

and practice it, works fine between to CE instance.

Then I have my own CreateFileMapping compiled as DLL use for set/get data between C++, C#, and VB Net. I need to learn how to implementation CE Lua allocateSharedMemory with another app, say like VB Net or C#.

For now, I am using an INI file to save all variables/parameters which able to use by CE and other apps, considering waste memory or memory leak.



Capture.JPG
 Description:
Shared memory vb demo
 Filesize:  104.09 KB
 Viewed:  4278 Time(s)

Capture.JPG



_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
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: Fri Apr 10, 2020 8:41 pm    Post subject: Reply with quote

Here's a quick example of doing this with CE.

In Cheat Engine, here is a demo app to test shared memory I wrote real fast:
Code:

-- Ensure cleanup..
_G._atom0s = _G._atom0s or {};
if (_G._atom0s._form ~= nil) then
    _G._atom0s._form:destroy();
    _G._atom0s._form = nil;
end
if (_G._atom0s._mem ~= nil) then
    deallocateSharedMemoryLocal(_G._atom0s._mem);
    _G._atom0s._mem = nil;
end

-- Create the form..
local f = createForm();
_G._atom0s._form = f;

-- Create the form controls..
local b1 = createButton(f);
local b2 = createButton(f);
local b3 = createButton(f);
local b4 = createButton(f);
local t1 = createEdit(f);
local t2 = createEdit(f);

-- Setup the form..
f.caption = 'Shared Memory Example by atom0s';
f:Show();
f:setSize(780, 165);

-- Setup the form controls..
b1.caption = 'Create Shared Memory';
b1:setPosition(10, 10);
b1:setSize(150, 32);
b1.onClick = function()
    -- Prevent double creating the memory..
    if(_G._atom0s._mem ~= nil) then
        deallocateSharedMemoryLocal(_G._atom0s._mem);
        _G._atom0s._mem = nil;
    end

    -- Create the shared memory..
    _G._atom0s._mem = allocateSharedMemoryLocal('CEShareTest', 2048);
    if (_G._atom0s._mem == nil or _G._atom0s._mem == 0) then
        showMessage('Failed to create shared memory!');
    end
end
b2.caption = 'Delete Shared Memory';
b2:setPosition(10, 45);
b2:setSize(150, 32);
b2.onClick = function()
    -- Cleanup the shared memory..
    if(_G._atom0s._mem ~= nil) then
        deallocateSharedMemoryLocal(_G._atom0s._mem);
        _G._atom0s._mem = nil;
    end
end
b3.caption = 'Read Shared Memory';
b3:setPosition(10, 90);
b3:setSize(150, 32);
b3.onClick = function()
    -- Attempt to read a string from the shared memory..
    if(_G._atom0s._mem ~= nil) then
        t1:clear();
        local s = readStringLocal(_G._atom0s._mem, 2048);
        if (s ~= nil and string.len(s) > 0) then
            t1.text = s;
        end
    else
        showMessage('Shared memory is not created yet!');
    end
end
b4.caption = 'Write Shared Memory';
b4:setPosition(10, 125);
b4:setSize(150, 32);
b4.onClick = function()
    if(_G._atom0s._mem ~= nil) then
        writeStringLocal(_G._atom0s._mem, t2.text);
    else
        showMessage('Shared memory is not created yet!');
    end
end
t1:setPosition(165, 98);
t1:setSize(600, 24);
t2:setPosition(165, 132);
t2:setSize(600, 24);


In C#, here is a monitoring app example, written in .NET Core 3.x:
Code:

        /// <summary>
        /// Application entry point.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // The size of the target memory mapped file..
            const int mapSize = 2048;

            MemoryMappedFile mm = null;
            MemoryMappedViewAccessor va = null;

            // Create the memory mapped file (2048 bytes long)..
            mm = MemoryMappedFile.CreateOrOpen("CEShareTest", mapSize, MemoryMappedFileAccess.ReadWriteExecute, MemoryMappedFileOptions.None, System.IO.HandleInheritability.None);
            if (mm != null)
            {
                // Create the view accessor to read/write to the memory mapped file..
                va = mm.CreateViewAccessor(0, mapSize, MemoryMappedFileAccess.ReadWriteExecute);
                if (va != null)
                {
                    // Monitor the shared memory for data and print it..
                    do
                    {
                        while (!Console.KeyAvailable)
                        {
                            Thread.Sleep(1000);

                            //  Read the shared memory for a string..
                            var data = new byte[mapSize];
                            var read = va.ReadArray<byte>(0, data, 0, mapSize);

                            // Output if we were able to read the shared memory..
                            if (read > 0)
                            {
                                var str = Encoding.ASCII.GetString(data);
                                Console.WriteLine((data[0] != 0 && !string.IsNullOrWhiteSpace(str)) ? str : "(empty)");
                            }
                        }
                    }
                    while (Console.ReadKey(true).Key != ConsoleKey.Escape);
                }
            }

            // Cleanup..
            va?.Dispose();
            mm?.Dispose();
        }


Compile and run the C# app. Then run Cheat Engine, open a new Lua window and execute the above Lua code to create the test form.
Click the Create Shared Memory button first, then edit the text box next to 'Write Shared Memory' with a string of your choice. Click 'Write Shared Memory' and the C# app should immediately see the change. You can also use the 'Read Shared Memory' to make the Lua code read from the shared memory.

You can edit the C# app to do extra things if you want to test writing back to it as well and letting the Lua side see it etc.

Should be everything needed to get started with it.

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

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Fri Apr 10, 2020 9:54 pm    Post subject: Reply with quote

Wow, atom0s, you write the code really pass.
I did copy the script for Lua but I have a question. For the C# sample script, is that the console Net core app?. What is the namespace you are using on your C# script?. Thank you

EDIT:
I got this error in C#

Code:
MemoryMappedFile mm = null;
MemoryMappedViewAccessor va = null;
..
..
Thread.Sleep(1000);
..
..
var str = Encoding.ASCII.GetString(data);



fixed with add:

Code:
using System;
using System.IO.MemoryMappedFiles;
using System.Text;
using System.Threading;


Works properly and I just need implementing it to my apps.



Capture.JPG
 Description:
 Filesize:  82.29 KB
 Viewed:  4257 Time(s)

Capture.JPG



_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL


Last edited by Corroder on Fri Apr 10, 2020 10:23 pm; edited 1 time in total
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: Fri Apr 10, 2020 10:12 pm    Post subject: This post has 1 review(s) Reply with quote

Yea, it's a console .NET Core app, usings are:
Code:

    using System;
    using System.IO.MemoryMappedFiles;
    using System.Text;
    using System.Threading;

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

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Fri Apr 10, 2020 10:25 pm    Post subject: Reply with quote

atom0s wrote:
Yea, it's a console .NET Core app, usings are:
Code:

    using System;
    using System.IO.MemoryMappedFiles;
    using System.Text;
    using System.Threading;


Yes I did it, thank so much

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting 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