• 0

[C#/C++]


Question

You know even though I have been programming in C# for a bit I have never touched really on interop. I really would like to learn how to interface with C++. I know C++ basics and have programmed some windows with the C++/WINAPI even. I'm trying to start small with it and just return a string but the program crashes and doesn't let me see the error info. Here are my C++ files (dll win32 project file):

InteropTest.h

extern "C" __declspec(dllexport) char * HelloFromDll();

InteropTest.cpp


#include "stdafx.h"
#include "InteropTest.h"

extern "C" __declspec(dllexport) char * HelloFromDll()
{
    char *result;
    result = "Hello from my DLL";
    return result;
}

And finally my C# code (.net 4.5 console program):

Program.cs


class Program
    {
        [DllImport(@"InteropTest.dll")]
        [return:MarshalAsAttribute(UnmanagedType.LPWStr)]
        private static extern string HelloFromDll();
        //[return:System.Runtime.InteropServices.MarshalAsAttribute(UnmanagedType.LPWStr)]

        static void Main(string[] args)
        {
            try
            {
                string strRetVal = HelloFromDll();
                Console.WriteLine("Returned string: {0}", strRetVal);
            }
            catch
            {
            }
            Console.ReadLine();
        }
    }

The program fails on the dll call. It throws an accessviolationexception but hangs forever on that.

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Try to call a method that takes void and returns void first. If that works, you're just not marshalling the string correctly.

For C#/C++ interop I much prefer using C++/CLI wrapper classes to P/Invoke; this removes the need for a lot of custom marshalling.

Link to comment
Share on other sites

  • 0

Thanks for the tip :) I saw the C++/CLI wrapper examples I just was going to build up to that. It's amazing how much fun it is to use C++ code in C#. Even for simple things like calling ExitWindowsEx :)

Link to comment
Share on other sites

  • 0

Out of curiosity (since I am assuming developers are looking at this thread), how many people here pay for MS VS? Most of my work is done (these days) in Java, some PHP, some Ruby, a dash of Python... Maybe some scala in the future. But in my spare time I play around with all kinds of things... Awhile back I worked with MS products for something like 5 years in a row and I actually enjoyed it... but now ... I don't know. I am so used to open source or freeware that just thinking about ponying up the $$$ for VS disturbs me...

Link to comment
Share on other sites

  • 0

Every company I've worked for gladly pays for VS. I've never had to pay for my personal use as I've always got it through Dreamspark as a student. Now that I'm not a student anymore, we'll see. I don't code that much at home, perhaps I'll make do with Express, or I'll get a discount through my employer, I'm not sure yet. It's a hefty sum but if you plan on making a lot of money with it, it's worth it.

Link to comment
Share on other sites

This topic is now closed to further replies.