Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a C++ function which takes pointers as input argument from a C#

Tags:

c++

c#

pointers

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace PatternSequencer
{
    class Version
    {
        public string majorVersion;
        public string minorVersion;

        ushort* pmajorVersion;
        ushort* pminorVersion;
        ulong status;

        [DllImport(@"c:\DOcuments\Myapp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SRX_DllVersion")]
        public static extern ulong SRX_DllVersion(ushort* pmajorVersion, ushort* pminorVersion);


        public Version()
        {
            status = SRX_DllVersion(&pmajorVersion, &pminorVersion);
            if (status)
            {
                majorVersion = "1 - " + *pmajorVersion;
                minorVersion = "1 - " + *pminorVersion;
            }
            else
            {
                majorVersion = "0 - " + *pmajorVersion;
                minorVersion = "0 - " + *pminorVersion;
            }
        }
    }
}

It throws an Error Pointers and fixed size buffers may only be used in an unsafe context. How do I pass pointers to the C++ dll? I am new to C#, please help me

like image 361
user2495173 Avatar asked Oct 18 '13 19:10

user2495173


People also ask

How do you call a function with a pointer argument?

To pass the value by pointer, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.

Can we pass pointer as an argument to function?

Just like any other argument, pointers can also be passed to a function as an argument.

How can we pass a pointer as an argument to a function explain with example?

Example 2: Passing Pointers to Functions Here, the value stored at p , *p , is 10 initially. We then passed the pointer p to the addOne() function. The ptr pointer gets this address in the addOne() function. Inside the function, we increased the value stored at ptr by 1 using (*ptr)++; .


3 Answers

Rather than using an unsafe context try:

[DllImport(@"c:\FreeStyleBuild\BERTScope\Release\Bin\BitAlyzerDLL.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SRX_DllVersion")]
public static extern ulong SRX_DllVersion(out ushort pmajorVersion, out ushort pminorVersion);

To make the call:

ushort major, minor;
SRX_DllVersion(out major, out minor);

I'm assuming the SRX_DllVersion parameters are output only, if not change out to ref.

Avoid unsafe code when ever possible.

like image 141
William Avatar answered Nov 14 '22 21:11

William


Of course you've to mark the class unsafe to make it work.

unsafe class Version
{
    [DllImport(@"c:\FreeStyleBuild\BERTScope\Release\Bin\BitAlyzerDLL.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SRX_DllVersion")]
     public static extern ulong SRX_DllVersion(ushort* pmajorVersion, ushort* pminorVersion);
}

If you have only one method you could mark the method as unsafe.

And don't forget to turn on "allow unsafe code" compiler option as well.

like image 42
Sriram Sakthivel Avatar answered Nov 14 '22 23:11

Sriram Sakthivel


You must mark that method as unsafe

[DllImport(@"c:\FreeStyleBuild\BERTScope\Release\Bin\BitAlyzerDLL.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SRX_DllVersion")]
public static extern ulong SRX_DllVersion(out ushort pmajorVersion, out ushort pminorVersion);
like image 28
gleng Avatar answered Nov 14 '22 21:11

gleng