Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

C++: Dll + pointers to ruby objects

Well, I was kinda playing with poccil method for accessing ruby objects, and I was trying to edit a simple number passing the number id to the dll, but I couldn't change it despite having tried with 100 different methods. I have done so:
In ruby:
Code:
a = 15
Win32API.new("test.dll", "Test", "I", "").call(a.__id__)
p a
In c++:
Code:
void __declspec(dllexport) __stdcall Test(int object)
{
  int *data = (int*)(object<<1);
  *data = 10;
}
Can someone help me with this?

PD: In this case, the game closes when the API is called.
 

e

Sponsor

I'd help, but I can't seem to be able to make his demo work; I get a GetProcAddress error (assuming it can't find the function?).
 
I'm just curious, but don't C++ exports get decorated in some cases unless you use a certain calling convention on all methods, or by using a .def file to define the exports of the library?

From what little experience with C++ I have, I remember that function names were decorated with useless garbage unless you did something about it.  The garbage would make the methods useless for look-up in most cases.
 

e

Sponsor

No, his code should be fine; my guess is that it dies when he tries to access the memory (which might be read-only?).

I've tested it a bit, and whenever I try to access the actual memory (i.e.: *data += 1), Game.exe crashes. But if I don't, it runs fine and dandy.

Yet poccil's stuff works. So I don't know; maybe it's a compilation/linker problem. poccil's stuff is all in C whilst we're both doing C++; some libs might do things differently.
 

Zeriab

Sponsor

You could try to catch the exception and then write it to an output file. That way you can get more information than Something Went Wrong.
I have found that any unhandled exception in the dll file will make the game crash.
 
Code:
Maybe you can find an alternative in this chapter of the pickaxe book:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html
I don't know much about that, but I am trying to use just poccil's method, which seems to be easier.

Code:
I'm just curious, but don't C++ exports get decorated in some cases unless you use a certain calling convention on all methods, or by using a .def file to define the exports of the library?

From what little experience with C++ I have, I remember that function names were decorated with useless garbage unless you did something about it.  The garbage would make the methods useless for look-up in most cases.
Yes, I have to use a .def file for the dll to work well in ruby.

Code:
No, his code should be fine; my guess is that it dies when he tries to access the memory (which might be read-only?).

I've tested it a bit, and whenever I try to access the actual memory (i.e.: *data += 1), Game.exe crashes. But if I don't, it runs fine and dandy.

Yet poccil's stuff works. So I don't know; maybe it's a compilation/linker problem. poccil's stuff is all in C whilst we're both doing C++; some libs might do things differently.
I get the same error, when trying to edit the memory, the Game.exe crashs. Maybe it can be so, because as you say I compile it in c++. However, I have tried a simple API which returns a number or a string, and it works fine with RMXP.

Code:
You could try to catch the exception and then write it to an output file. That way you can get more information than Something Went Wrong.
I have found that any unhandled exception in the dll file will make the game crash.
The Game just crashs when calling the DLL, without any explanation of the error.

Well, I should try to make the dll in C, but I don't know how... I use Visual Studio, can someone help me on that?
 

Zeriab

Sponsor

I know. It's not very helpful.
That's why I am suggesting that you write it out to a file.
You could write something to a file for each line. Then you can locate the error or the position of the first error.
You could also try attaching the Visual Studio's debugger although that might be difficult since the DLL file is called from rmxp game.

*hugs*
 
Game.exe crashs cuz u basically do:
Code:
15 = 10
Dis won't werk, guy.

Please don't post to insult the original poster's intelligence.  He's not trying to say 15 equals 10, he's trying to assign a value by using an indirection operator.  For more information, please refer to the global forum rules.  Further, it's difficult to tell your goal beyond flaming due to your naming choice when you registered. -Alexander Morou
 
I'm wondering, is it possible that the Ruby language doesn't handle all values in the same way?

What if, by chance, primitive values, such as numbers, are treated as just numbers, and they're sent in by reference or by value?

Have you tried your method without bit-shifting the incoming value?
 
Well, I have been doing more test in C, it just take to add a .c file insteand a .cpp file in the DLL project. And for normal API functions it works well in ruby, and I have done some progress with editing Ruby Objects.
First I have remembered that same numbers in ruby have the same pointer, so they are not easy editable. Also after seeing ruby sources, exactly the ruby.h file I have discovered that ruby objects are structs with different values in them. So the __id__ value of a ruby object is a pointer to struct, and not to the objects data.
I have made a test project for editing a simple string object, using a struct defined in ruby.h, but all doesn't work well. Here is the String struct:
Code:
typedef unsigned long VALUE;

typedef struct {
    unsigned long flags;
    VALUE klass;
} RBasic;

typedef struct {
	RBasic basic;
	long len;
	char *ptr;
	union {
		long capa;
		VALUE shared;
	} aux;
} RString;
Obtaining the lenght of the string, works perfeclty, but obtaining the data, or modifying it doesn't work well.
I can't obtain the data well, and if I try to modify it, I get weird results, like strange ASCII characters.
Here I have uploaded the dll project in zip and rar:
DLL Test.rar
DLL Test.zip
The project includes also a .rb file for testing the dll in ruby.

I know. It's not very helpful.
That's why I am suggesting that you write it out to a file.
You could write something to a file for each line. Then you can locate the error or the position of the first error.
You could also try attaching the Visual Studio's debugger although that might be difficult since the DLL file is called from rmxp game.

*hugs*
I thought on writing debug things into a file, but I still don't know how to do that in c. Maybe I should google for it.
I'm wondering, is it possible that the Ruby language doesn't handle all values in the same way?

What if, by chance, primitive values, such as numbers, are treated as just numbers, and they're sent in by reference or by value?

Have you tried your method without bit-shifting the incoming value?
I have discovered as I said before, that yes ruby handle the ruby objects by structs, defined in ruby.h in the ruby sources. I will try without bit-shifting, maybe it will work better.

PD: who the hell is vgvgf?
 
ERZENGEL":20ihp8qu said:
Here is my working test project (about modifying Ruby strings): http://www.mediafire.com/?az0yzba5mno

The length change is needed because thr Ruby String will be cut down the string if the C string is longer or if the C string is shorter it will be filled up with stuff like "\000".
Thanks ERZENGEL, it works perfectly. I will try to find how to edit all other types of ruby objects and make a kind of SDK for ruby objects editing DLLs.
 

e

Sponsor

Honestly, if someone could make a sample project for common environments (i.e.: a GCC Makefile, a VStudio project file and an XCode project file), it'd be pretty amazing.
 
etheon":xftmfyr5 said:
Honestly, if someone could make a sample project for common environments (i.e.: a GCC Makefile, a VStudio project file and an XCode project file), it'd be pretty amazing.
I am working on a kind of SDK, for including most ruby objects for being modificated by this method.
My progress is so:
- Strings, credits to ERZENGEL
- Bitmaps, credits to poccil
- Fixnums, true, false and nil(Can't be edited by this method, because their pointers are fixed and ever the same. However there is no need for editing them so, because you can obtain them simply as a result from a Win32API call)
- Bignums
- Floats
- Arrays
- Hashs

And to do:
- Sprites
- Planes
- Tilemaps
- Table
- Classes(class Something)
- And some more
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top