Sign in to follow this  

[C#/C++]

Recommended Posts

sathenzar    23

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.

Share this post


Link to post
Share on other sites
+Andre S.    1,892

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.

Share this post


Link to post
Share on other sites
sathenzar    23

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 :)

Share this post


Link to post
Share on other sites
+Andre S.    1,892

I guess it's also worth nothing that you're returning a pointer to a stack-allocated object, which is the very definition of evil. :)

  • Like 1

Share this post


Link to post
Share on other sites
Tuishimi    129

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...

Share this post


Link to post
Share on other sites
+Andre S.    1,892

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.

Share this post


Link to post
Share on other sites
firey    3,627

Where I work we have the partner program, but yea we have legitimate copies of VS 2003 -> 2012

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.