Help me with C++ / C#
I want to make a simple c# program with 1-2 functions and a program in c++ that inject in this c# program and check integrity of c# program by calling the c# function and validating its output.
HMODULE hModule = LoadLibraryA("Program.exe");
AddFunc Add = (AddFunc)GetProcAddress(hModule, "Add");
I can't get function address.
6 Replies
I've never done this before, but you probably just need to set the calling convention on the c++ side to C
extern(C)
or use the mangled name on the C# side
Why?
Just use P/Invoke
you can open the executable in a hex editor to see the strings, it should have the mangled name in it if the function is exported
if you're using the c++ calling convention
What are you trying to make?
I want to check the integrity of the c# program in c++
call from c++ program a function in c# program and ONLY check if the result is ok
Like:
2 + 2 = 4
But if in my cpp program I get 5 must be a problem
So just call and validate
It is not working
if u want to call a c# method from c++ you have a lot to consider,
first of that C# (or .NET) is usually not natively compiled in first place and thus do not expose functions/methods like ur typical C or C++ functions
you can compile via NAOT to expose methods via the
[UnmanagaedCalleryOnly]
attribute.
though, NAOT comes with with some restrictions.
secondary u have to take into account, that C# (or .NET) uses garbage collection instead of explicitly freeing memory.
and at that a compacting one, thus on the C# side, the addresses of references could change and make them invalid, if they were created on the C# side.
basically for what you are looking for u are most likely need to host the runtime inside ur native code, unless u want to restrict urself to NAOT compiled libraries/programs, which itself is explained there:
https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting
and thats just a starter; you will need to read the whole (or at least most of the) "Native interoperablity" section
@Anton as far as i know .NET doesnt provide functionaility for c++ interop but only for C,
and generally speaking they want to "reverse p/invoke"
@Siegthis sounds a lot like u want to write some unit or integration tests.
the question is what u really want to test.
use the .NET test frameworks to test the C# side.
use whatever c++ provides to test their side.
testing the interoperability will become quite a big task