Jump to content



Photo

[C#/C++]

c# c++

  • Please log in to reply
6 replies to this topic

#1 sathenzar

sathenzar

    Neowinian

  • Joined: 12-June 06

Posted 20 February 2013 - 02:09

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.


#2 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 20 February 2013 - 19:59

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.

#3 OP sathenzar

sathenzar

    Neowinian

  • Joined: 12-June 06

Posted 20 February 2013 - 20:08

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

#4 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 20 February 2013 - 22:03

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

#5 Tuishimi

Tuishimi

    Michinator

  • Joined: 19-November 10
  • OS: Windows 8

Posted 20 February 2013 - 22:10

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

#6 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 20 February 2013 - 22:31

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.

#7 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 6
  • Joined: 30-October 05
  • Location: Ontario, Canada
  • OS: Windows 7
  • Phone: Android (4.1.2)

Posted 21 February 2013 - 01:18

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



Click here to login or here to register to remove this ad, it's free!