1

I'm trying to make something in c# and to add some speed I want some of the methods to be done in c++. I have done something like this in java before and it was really fun and somewhat easy. Trying to do this in c# has been frustrating. The easiest part in java is now the hardest part with c#: finding out what parameters to use in the native/extern. With things like int,double,and other rudimentary types it is simple, but how would I do this with more complex things like structs and classes? With java there is a program in the jdk called javah which does this for you, but after a good 3 hours of googling I have found no such thing for a c#/c++ bridge. Am I oblivious or is there no such program for c#?

I know about [DllImport] I was wondering how to turn public static extern void ExternFunction(MyCustomStruct m) to a c++ signature

William
  • 21
  • 2
  • it sounds your searching for [DllImport] https://stackoverflow.com/questions/19450783/how-to-use-dllimport-in-c https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute(v=vs.110).aspx see example here https://www.codeguru.com/csharp/csharp/cs_data/article.php/c4217/Calling-Unmanaged-Code-Part-1--simple-DLLImport.htm – DotNetDev Nov 27 '17 at 06:52

2 Answers2

1

You are looking for DllImport attribute:

class Example
{
    // Use DllImport to import the Win32 MessageBox function.
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

    static void Main()
    {
        // Call the MessageBox function using platform invoke.
        MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
    }
}

For a detailed tutorial: https://learn.microsoft.com/en-us/dotnet/framework/interop/consuming-unmanaged-dll-functions

Identifying which DLL has what function: https://learn.microsoft.com/en-us/dotnet/framework/interop/identifying-functions-in-dlls

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Sunil
  • 3,404
  • 10
  • 23
  • 31
  • I knew about [DllImort] I was wondering how to turn `public static extern void ExternFunction(MyCustomStruct m)` to a c++ signature – William Nov 27 '17 at 07:06
0

As far as the native declaration goes you are pretty much on your own, you can use the MSDN docs (or the SDK headers) to find the signatures of any public Windows API you might want to use. The headers are the most likely route if the code you want to access is not from MS.

Native C code simply does not have a reliable way of getting function signatures from the binary. C++ has somewhat more (but not much), you can use a tool like dumpbin.exe that can take the mangled c++ symbols and turn that back into the human-readable signature. However many DLLs even if written in C++ will simply give a list of function names with no type information whatsoever.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • I want to turn a c# function declaration to a c++ function declaration not the other way around – William Nov 27 '17 at 07:31