• 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
https://www.neowin.net/forum/topic/1137464-cc/
Share on other sites

6 answers to this question

Recommended Posts

  • 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
https://www.neowin.net/forum/topic/1137464-cc/#findComment-595533144
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
https://www.neowin.net/forum/topic/1137464-cc/#findComment-595533198
Share on other sites

This topic is now closed to further replies.
  • Posts

    • MusicBee 3.6.9668 by Razvan Serea MusicBee is an application geared toward managing extensive music collections, easy to use and with a comprehensive feature set. It makes it easy to organize, find, and play music files on your computer, on portable devices, and on the Web. It provides playback of a wide range of audio formats, smart playlists with the ability to discover and play new music from the web, advanced tag editing with automated artwork and tag look up, folder monitoring, automated file re-organization, portable device synchronization, and secure CD ripping with AccurateRip verification. MusicBee features: Supported formats: MP3, AAC, M4A, MPC, OGG, FLAC, APE, TAK, WV, WMA and WAV. Audio CDs: Audio CD playback and ripping (with CD-Text capabilities) is supported. CD tracks can be ripped (in fast or secure mode) as individual files or as a single album with embedded cuesheet. Conversion: Conversion from and to all supported formats as metadata are preserved. Synchronization of tags only (in case that the output file already exists) instead of reencoding is possible. ReplayGain support: both playback and calculation. File Organization: Organization and renaming of music files into folders and files based on tag values such as artist, album, name, track number, etc. that can be specified. MusicBee can do this automatically for all files in a music library or the user can choose the files or folders themselves. Web Browsing: Browsing of the web using Mozilla's XULRunner environment. Scrobbling: Tracks played from MusicBee can optionally be scrobbled to Last.fm. Customizable user interface layout. Customizable keyboard shortcuts. MiniLyrics support Download: MusicBee 3.6.9668 | MusicBee Portable | ~9.0 MB (Freeware) Download: Windows Store Edition View: MusicBee Home page | Release Notes | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • On xiaomi hyperos there's also an option to disable google assistant. I've got everything disabled. Only thing I do have installed is a web wrapped for duck.ai which claims to let you use various AIs anonymously
    • I need to understand the rationale of not shipping all of these K2 improvements in a single update/release. It's giving "we will fix Windows 11 but no commitments". It seems to me that they just announce these improvements just to appease the community.
    • The term "RTM" is long gone starting with Windows 10. Every current release is a GA build. This is the result of MS making Windows as a Service (WaaS).
    • Looks like no official TBW rating, which should be a required listing in my opinion for sites like Amazon (hell, put it on the box too.)
  • Recent Achievements

    • Conversation Starter
      sumytbe earned a badge
      Conversation Starter
    • One Year In
      B4dM1k3 earned a badge
      One Year In
    • One Year In
      DarkWun earned a badge
      One Year In
    • Dedicated
      Almohandis earned a badge
      Dedicated
    • Dedicated
      JuvenileDelinquent earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      517
    2. 2
      +Edouard
      186
    3. 3
      PsYcHoKiLLa
      87
    4. 4
      Michael Scrip
      79
    5. 5
      Steven P.
      73
  • Tell a friend

    Love Neowin? Tell a friend!