locked
How can I call this C++ function in C# RRS feed

  • Question

  • Hello,

    I have a C# function and have a hard time how to declare and use it in C#:

    virtual ErrCode  SetPropertyValue (const char *pPrp, const char *pValue , const char *pUnit = ((const char*)0))=0;

    Thanks for your help,

    Tammt

    Tuesday, July 24, 2012 11:07 PM

Answers

  • Hi Tammt ,

    I think you can use DllImport to call c++ function in C# .Please  follow the steps below :

      1.Compile your C++ function into a DLL . For creating the DLL you can refer to reating a simple DLL

      2. Put the generated DLL file in the same output directory as the C# output (so in/Debug or bin/Release). In your C# project, be sure to include the following.

    using System.Runtime.InteropServices;

      3.  About the “ErrCode” , it is not a type in C# . I guess it maybe  the custom type in your code , so it need the further conversion .If it return just a int /string or some basic type , please use the key words: int /string etc.

    In C# , pointers are considered   to be unsafe  code  which  I do  not suggest you to use .

    Therefore , give the passing parameters type depending on the type of these pointers .

    If the pointer’s  type in C++ code  is int/char , you can give the parameter with int /string type .If the normal pointer, you can use IntPtr as the type.

    furthermore , const char *pUnit = ((const char*)0) correspond to the Optional Arguments .

     In the end ,your C# Class  should be :

    using System.Runtime.InteropServices;
                               
    namespace CallingFunction
    {    
      class Test      
      {    
          [DllImport("Myfunction.dll", CharSet = CharSet.Auto)]    
          public static extern virtual ErrCode  SetPropertyValue (const string pPrp, const string pValue , const string pUnit = 0)=0;        
         
          static void Main()         
          {                
              ...
          }     
      }}

     Please also refer to : http://www.go4expert.com/forums/showthread.php?t=9699

    http://joshhendo.blogspot.com/2011/02/calling-c-functions-in-c-and-passing.html  

    Regards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us







    • Marked as answer by Lisa Zhu Thursday, August 2, 2012 9:10 AM
    • Edited by Lisa Zhu Monday, September 10, 2012 5:56 AM
    Thursday, July 26, 2012 8:04 AM

All replies

  • Is this a Microsoft function or your own C++ function?


    Goldfish64 - Please remember to mark any replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 25, 2012 12:14 AM
  • Goldfish64,

    This C++ function is written from our camera vendor.  We tried to call this function to be able to talk to the camera.

    -Tammt

    Wednesday, July 25, 2012 12:19 AM
  • Who is the vendor? If this is part of an SDK, where could it be found?


    Goldfish64 - Please remember to mark any replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 25, 2012 3:00 AM
  • Hi Tammt ,

    I think you can use DllImport to call c++ function in C# .Please  follow the steps below :

      1.Compile your C++ function into a DLL . For creating the DLL you can refer to reating a simple DLL

      2. Put the generated DLL file in the same output directory as the C# output (so in/Debug or bin/Release). In your C# project, be sure to include the following.

    using System.Runtime.InteropServices;

      3.  About the “ErrCode” , it is not a type in C# . I guess it maybe  the custom type in your code , so it need the further conversion .If it return just a int /string or some basic type , please use the key words: int /string etc.

    In C# , pointers are considered   to be unsafe  code  which  I do  not suggest you to use .

    Therefore , give the passing parameters type depending on the type of these pointers .

    If the pointer’s  type in C++ code  is int/char , you can give the parameter with int /string type .If the normal pointer, you can use IntPtr as the type.

    furthermore , const char *pUnit = ((const char*)0) correspond to the Optional Arguments .

     In the end ,your C# Class  should be :

    using System.Runtime.InteropServices;
                               
    namespace CallingFunction
    {    
      class Test      
      {    
          [DllImport("Myfunction.dll", CharSet = CharSet.Auto)]    
          public static extern virtual ErrCode  SetPropertyValue (const string pPrp, const string pValue , const string pUnit = 0)=0;        
         
          static void Main()         
          {                
              ...
          }     
      }}

     Please also refer to : http://www.go4expert.com/forums/showthread.php?t=9699

    http://joshhendo.blogspot.com/2011/02/calling-c-functions-in-c-and-passing.html  

    Regards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us







    • Marked as answer by Lisa Zhu Thursday, August 2, 2012 9:10 AM
    • Edited by Lisa Zhu Monday, September 10, 2012 5:56 AM
    Thursday, July 26, 2012 8:04 AM