Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write my C++ function so I can call it from C#?

I have C++ code. That code contains Windows mobile GPS enable/disable functionality. I want to call that method from C# code, that means when the user clicks on a button, C# code should call into C++ code.

This is the C++ code for enabling the GPS functionality:

#include "cppdll.h"

void Adder::add()
{
// TODO: Add your control notification handler code here
  HANDLE hDrv = CreateFile(TEXT("FNC1:"), GENERIC_READ | GENERIC_WRITE,
                0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  if (0 == DeviceIoControl(hDrv, IOCTL_WID_GPS_ON, NULL, 0, NULL, 0, NULL, NULL))
  {
     RETAILMSG(1, (L"IOCTL_WID_RFID_ON Failed !! \r\n")); return;
  }
     CloseHandle(hDrv);

 return (x+y);
}

And this is the header file cppdll.h:

class __declspec(dllexport) Adder
{
  public:
   Adder(){;};
  ~Adder(){;};
 void add();
};

How can I call that function using C#?

Please, can anybody help me out with this issue?

like image 603
Piraba Avatar asked Feb 23 '12 05:02

Piraba


People also ask

Can you call a function in a function in C?

No you can't have a nested function in C . The closest you can come is to declare a function inside the definition of another function. The definition of that function has to appear outside of any other function body, though.

How can I call one C program from another?

use "system" a in-built function. Say you want to invoke another C program with name abc.exe. system("abc.exe"); // provide absolute path if exe place at other directory. If its a executable file you can use system("program_to_run") function to run any program which can be invoked from terminal.

How do you call one function from another function in C?

The main function always acts as a driver function and calls other functions. We can also write function call as a parameter to function. In the below code, first add(num1, num2) is evaluated, let the result of this be r1. The add(r1, num3) is evaluated.


1 Answers

I'll give you an example.

You should declare your C++ functions for export like so (assuming recent MSVC compiler):

extern "C"             //No name mangling
__declspec(dllexport)  //Tells the compiler to export the function
int                    //Function return type     
__cdecl                //Specifies calling convention, cdelc is default, 
                       //so this can be omitted 
test(int number){
    return number + 1;
}

And compile your C++ project as a dll library. Set your project target extension to .dll, and Configuration Type to Dynamic Library (.dll).

enter image description here

Then, in C# declare:

public static class NativeTest
{
    private const string DllFilePath = @"c:\pathto\mydllfile.dll";

    [DllImport(DllFilePath , CallingConvention = CallingConvention.Cdecl)]
    private extern static int test(int number);

    public static int Test(int number)
    {
        return test(number);
    }
}

Then you can call your C++ test function, as you would expect. Note that it may get a little tricky once you want to pass strings, arrays, pointers, etc. See for example this SO question.

like image 54
Gleno Avatar answered Sep 30 '22 18:09

Gleno