• 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

    • 26H2 absolutely will support ARM Windows just not on devices that came with 26H1. This is evident by the fact I am running 26H2, which on my MacBook Neo and Surface Book, within a VM.
    • Mp3tag 3.35 by Razvan Serea Mp3tag is a powerful and yet easy-to-use tool to edit metadata (ID3, Vorbis Comments and APE) of common audio formats. It can rename files based on the tag information, replace characters or words from tags and filenames, import/export tag information, create playlists and more. The program supports online freedb database lookups for selected files, allowing you to automatically gather proper tag information for select files or CDs. Mp3tag supports the following audio formats: Advanced Audio Coding (aac) Free Lossless Audio Codec (flac) Monkeys Audio (ape) Mpeg Layer 3 (mp3) MPEG-4 (mp4 / m4a / m4b / iTunes compatible) Musepack (mpc) Ogg Vorbis (ogg) OptimFROG (ofr) OptimFROG DualStream (ofs) Speex (spx) Toms Audio Kompressor (tak) True Audio (tta) Windows Media Audio (wma) WavPack (wv) Mp3tag 3.35 changelog: This version introduces a new Files options page, enhanced toolbar customization, support for RF64 WAV files, improved Discogs and MusicBrainz tag sources, and many other improvements and fixes. See the Release Notes for more details. Download: Mp3tag 64-bit | 5.7 MB (Freeware) Download: Mp3tag 32-bit | 5.2 MB Link: Mp3tag Homepage | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • The FIFA World Cup is not US centric.
    • It’s amusing how Microsoft is pushing IT admins as if this was a major, game-changing update. In reality, it’s just an enablement package that bumps the build number, which is disappointing compared to the more substantial 22H2 and 24H2 releases. Technically, 25H2, 26H1, and the upcoming 26H2 are essentially the same, differing only in support schedules. They could have included the Windows K2 improvements here, but chose not to. The era of Windows being in the backburner continues, and this 26H2 release feels like an afterthought. Shame, Nadella, shame.
    • Microsoft, totally not confusing /s 25H2 - Current for non-Arm based Windows 26H1 - Current for Arm based Windows 26H2 - Only for non Arm Windows
  • Recent Achievements

    • Week One Done
      AMV earned a badge
      Week One Done
    • One Month Later
      AMV earned a badge
      One Month Later
    • Collaborator
      ryansurfer98 went up a rank
      Collaborator
    • One Month Later
      Eurosoft10 earned a badge
      One Month Later
    • Week One Done
      Eurosoft10 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      523
    2. 2
      +Edouard
      172
    3. 3
      PsYcHoKiLLa
      78
    4. 4
      Steven P.
      72
    5. 5
      Michael Scrip
      71
  • Tell a friend

    Love Neowin? Tell a friend!