Question Unable to Import C++ Function in C# using Digital Mars compiler but not using VS 2012

awolfe

New member
Joined
Jan 20, 2016
Messages
1
Programming Experience
5-10
The following simple C++ program is compiled using Digital Mars (v 8.57) and also using Visual Studio 2012 Empty C++ project type (build target 86). When I try to import the functions in a C# program, the application crashes reading the Digital Mars version but works fine using the VS version. When I run in debug mode, the function returns "Function Evaluation Was Aborted" and attempts multiple times to evaluate then stops. I'm running on Windows 7 64 bit.
C++ program:


C#:
#include <stdio.h>
#include <windows.h>

extern "C"
{
    _declspec(dllexport) LPSTR ReturnString()
    {
      return "HI THERE";
    }

    _declspec(dllexport) int ReturnInt()
    {
      int myvalue = 12;
      return myvalue;
    }

    _declspec(dllexport) void ReturnNothing()
    {
	  printf("Hello World");
    }
}

int main()
{
	return 0;
}



C# Application:

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace TestCSharp
{
    class Program
    {
        public const string strPath = "C:\\Code Blocks Projects\\Test\\bin\\Debug\\Test.dll";
        //public const string strPath = "C:\\Visual Studio 2012\\Projects\\TestCPPEmpty\\Debug\\TestCPPEmpty.dll";

        [DllImport("User32.dll")]
        public static extern int MessageBox(int h, string m, string c, int type);

        [DllImport(strPath, EntryPoint = "ReturnInt")]
        public static extern int ReturnInt();

        [DllImport(strPath, EntryPoint = "ReturnString")]
        public static extern string ReturnString();

        [DllImport(strPath, EntryPoint = "ReturnNothing")]
        public static extern void ReturnNothing();

        public static void Main()
        {
            //call winapi function
            MessageBox(0, "API Message Box", "API Demo", 0);

            int myvalue = ReturnInt();
            Console.WriteLine(myvalue);

            ReturnNothing();

            string mystring = ReturnString();
            Console.WriteLine(mystring);
        }
    }
}
 
Last edited by a moderator:
Back
Top Bottom