0

Recently, i decided to get a bit into hooking functions, i did it already in c++, i started off with my own program then started on doing it on games.

Since i am a fan of c# and i prefer it in many ways over c++, i decided to do it all in c#.

It was easy, and i recreated almost everything that i made in c++ in less time actually(weird right).

I just got stuck to something and that is calling unmanaged functions that is passed down as reference/pointer.

I eventually made a c-wrapper but i would like to go pure c# soon

Example function: void GetData(Entity& entity, Filter* filter, Data* data)

Now, the entity. i tried doing ref struct entity. and for the others, i used IntPtr and i passed down Marshal.StructureToPtr(filter/data instance).

This didn't work, i don't know what is wrong or what but i defiantly want to know and i thought i would need to ask smarter peoples out there.

1(eh) More Question: In C#, how is a struct/class diffrent, what is a c# struct in c++? Does creating a class with new in c# always result in being Class* ?

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Balen Danny
  • 58
  • 1
  • 5

1 Answers1

2

First of all, why do you want to use IntPtr and Marshal.PtrToStructure? Is there a specific need for this? If there is a specific need, come with that problem.

Or you're just trying to change the argument you passed to a function?

If you just want to change the parameter, I suggest, you have a look at out (also see this link) and ref keywords, and how to use them, links' got the examples too, just read them thoroughly.

In C#, how is a struct/class diffrent, what is a c# struct in c++? Does creating a class with new in c# always result in being Class* ?

I don't know why, but I'm getting a feel that you're not much familiar with Reference Types and Value Types, so let me explain them to you first, then you can better understand the difference between class/struct, so I'm not straightforwardly answering your question, hold on, till you find your answer in there.

There are two main types in C#, first is Refernce Types and second is Value Types

C# Common Type System (CTS) has a simple hierarchy, following diagram illustrates the relationship between Value Types and Reference Types in CTS.

Reference Types

Whenever you create a Reference Type, it only creates the Reference (C++ also have references, but C# References are somewhat different from C++ References, see this -> C++ references Vs C# references) on the stack, and the object (if created) is created on heap (C# References are pretty close to the C++ Pointers).

C# Reference Types include:

I've provided the links to all of them, you can go ahead and read them all.

Value Types

Whenever you create an object of a Value Type, itself it is created on the stack, and not the heap (only Reference Types are created at heap)

MSDN Documentation of Value Types state:

Variables that are based on value types directly contain values. Assigning one value type variable to another copies the contained value. This differs from the assignment of reference type variables, which copies a reference to the object but not the object itself.

Value Types includes:

Structs fall into these categories:

I've just provided you the content, you'd have to read it yourself.

So the answer to your question:

how is struct/class different, what is c# struct in c++?

is that struct is a value type, and class is reference type, so creating an instance of a class will create just a reference (or say pointer, don't use this term, because they're not pointers, but just to have some understanding) in stack and will create the instance itself in Managed Heap (to have some understanding of Managed Heap, study the Automatic Memory Management in C#). And when creating an instance of a struct, it will create the object in stack and not the Managed Heap.

Does creating a class with new in c# always result in being Class* ?

No and Yes!

No in the sense that classes fall in the category of Reference types, and a reference is created on the stack and not the pointer, so you can't say that a pointer to class instance is being created but reference is.

Yes is in the sense, when you say (informally) that the references in C#, they're much like pointers.

EDIT

I see, you're trying to consume your void GetData(Entity& entity, Filter* filter, Data* data) function (or similar functions) in your C# code, see Passing Structures and Classes, I think you can find your answer there.

Community
  • 1
  • 1
Ahmad Khan
  • 2,655
  • 19
  • 25
  • Thanks for the help, you also didn't seem to know exactly what i wanted, i simply wanted to know the conversion of marshaling the example in the post, while i also wanted more info and i got it, thanks. – Balen Danny Aug 30 '16 at 21:24
  • Oh, so you're still trying to consume the `void GetData(Entity& entity, Filter* filter, Data* data)` function in your C# code? – Ahmad Khan Aug 31 '16 at 06:43
  • See, maybe [this link](https://msdn.microsoft.com/en-us/library/awbckfbz(v=vs.110).aspx) will help you. – Ahmad Khan Aug 31 '16 at 06:47