Using C++ libs and headers through a DLL in C#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Oscee
    New Member
    • Feb 2010
    • 3

    Using C++ libs and headers through a DLL in C#?

    I am trying to write a camera control software - I have some factory provided headers and SDK libraries for the camera (I'm assuming they're from the VS6 era) and the hardware, of course. I would like to call these library functions from C# because the interface and some graph- and image displaying seems to be much more easier to write in C#.

    What I was thinking of, I would write a C++ code which basically includes the headers and other stuff and wraps these SDK/lib functions into C++ functions. Then I make a DLL from it and use it in C#.

    For instance, the initialization in the C++ part:
    Code:
    int initCam(int param)
    {
    	return SET_INIT(param);
    	//returns with an error code or 0
    }
    and I call the initCam(argumen t) function in C# to initialize the camera.

    Would this work? I am not sure this is the proper way to call C/C++ libraries from C# and as I tried a couple of things it doesn't seem to be :)

    edit:
    I hope I posted to the right place, I wasn't sure where to post this question
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Google with the terms "Call C++ DLL from C#" yielded several tutorials.

    Comment

    • alag20
      New Member
      • Apr 2007
      • 84

      #3
      I agree with tlhintoq, that googling will give you lots of tutorials to explain in detail, but just as a reference for future, incase people come to this page via google, you can accomplish it in the way below.

      Code:
      [DllImport("user32.dll", SetLastError = true)]
              [return: MarshalAs(UnmanagedType.Bool)]
              static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
      This goes to user32.dll which is a windows dll, and returns a boolean, but you will write something similar to tap into your C++ dll with exposed functions. Google for tutorials to explain in detail.

      Comment

      Working...