How to get Data from C++ Program from C# program through Dll Import?

I have created an application in C++, Now through dll Inport in C# I am accessing the method of C++.

I wan to return some data from C++, can any one tell how this is possible.

-------C#Code-----------------

[DllImport("Sabs.dll")]

public static extern int GetDataFromCPlus(string path);

--------C# Code------------------

The C++ code is:

extern "C"{

__declspec(dllexport) void GetRdrData(char* path) {

//---------Code-----

} }




Answer this question

How to get Data from C++ Program from C# program through Dll Import?

  • Stephane Seigneurin

     The following code does this (somewhat crudely, as there is no error handling or intelligence about the data being read).  Depending on the data you need to read, this may or may not help.

    ---------getdata.cpp--------------------

    #include <stdio.h>
    #include <malloc.h>

    extern "C"{

    char* buf;

    __declspec(dllexport) char* GetRdrData(char* path)
    {
       FILE* fp;
       buf = new char(1025);

       fopen_s(&fp, path, "rb");
       int count = fread(buf, 1, 1024, fp);
       return buf;

    }

    __declspec(dllexport) void FreeNativePtr()
    {
       if (buf) delete buf;
    }

    }

    ----------------consumedata.cs------------------

    // consumedata.cs

    using System;
    using System.Runtime.InteropServices;

    class CSharpMain
    {

    [DllImport("getdata.dll", EntryPoint = "GetRdrData")]

    public static extern String GetDataFromCPlus(string path);

    [DllImport("getdata.dll")]
    public static extern void FreeNativePtr();

       public static void Main()
       {
           String data = GetDataFromCPlus("file1.txt");
           Console.WriteLine(data);
           FreeNativePtr();
       }

    };

    In this example, the data is treated as text.  Will that work for your purposes

    You could improve this by providing a handle to the native data that you can then free, instead of having a global variable in the C++ code.  The marshaling process copies the native data to a new managed string object.

     



  • Andreas Asterlund

    Here's a somewhat better version in which the memory is allocated on the C# side using Marshal.AllocHGlobal. This is then passed to the native data reader function, which fills it in. We also pass the size to make sure that there is enough space for the data.

    // getdata2.cpp
    #include <stdio.h>
    #include <malloc.h>

    extern "C"{

    __declspec(dllexport) int GetRdrData(char* path, char* data, int size)
    {
    FILE* fp;
    fopen_s(&fp, path, "rb");
    int count = fread(data, 1, size - 1, fp);
    return count;
    }

    }

    // consumedata2.cs
    using System;
    using System.Runtime.InteropServices;

    class CSharpMain
    {

    [DllImport("getdata2.dll", EntryPoint = "GetRdrData")]

    public static extern int GetDataFromCPlus(string path, IntPtr handle, int size);


    public static void Main()
    {
    IntPtr handle = Marshal.AllocHGlobal(1025);
    int count = GetDataFromCPlus("file1.txt", handle, 1025);
    string s = Marshal.PtrToStringAnsi(handle);
    Console.WriteLine(s);
    Marshal.FreeHGlobal(handle);
    }


    };



  • How to get Data from C++ Program from C# program through Dll Import?