CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2003
    Posts
    100

    Calling a C++/CLI method from C#

    Hello,

    In C# I want to call a C++/CLI method that get 2 parameters:
    The first parameter (int a) is passed "by value"
    The second paramter (int *b) is passed 'by reference".
    The second parameter is changed by this method and passes it back to C#

    Code:
    namespace SampleCLI 
    {
    	public ref class Class1
    	{
    		public:
    			Class1()
    			{
    			};  // constructor
    			int Open (int a,int *b)
    			{
    				*b=a;
    				return 0;
    			}
    	};
    }
    When I tried to use this method in C# I got the error:
    "Pointers and fixed size buffers may only be used in an unsafe context"

    So in C# I wrote:
    Code:
    unsafe
    {
             p.Open(5, &b);
    }
    Is it dangerous ? Can it harm the C# application ?

    The goal: I have a C++ code running on embedded machine under vxWorks. This code compiles with an h file that contains several (large) structures.

    My PC runs a C# code that talks with the other side using UDP. This code must be compiled with the same h file. I guess I need a C++/CLI dll for this purpose.

    It it wise to use C# for this purpose ? I want to use the GUI benefits of C#.

    Thanks.

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Calling a C++/CLI method from C#

    This is C++/CLI way to declare ref parameter:
    int Open (int a,int% b)

    About using C++/CLI: yes, your case is exactly matches the C++/CLI language purpose: interoperability between managed and unmanaged code.
    Last edited by Alex F; June 4th, 2010 at 08:26 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured