• 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

    • I disagree; they come off very "bitchy" and "whiny". Make a great product and combine that with a great price (free) and people will come over to your side. Or build it and they will come as they say. Constantly trying to get attention by complaining all the time, will turn people off to your product.
    • It use to be a nightmare, with LibreOffice supporting a newer draft ODF standard by default, and Microsoft Office supporting the older non-draft standard. Now that they both support the same version of ODF, they should be interoperable.
    • Brave Browser 1.91.171 by Razvan Serea Brave Browser is a lightning-fast, secure web browser that stands out from the competition with its focus on privacy, security, and speed. With features like HTTPS Everywhere and built-in tracker blocking, Brave keeps your online activities safe from prying eyes. Brave is one of the safest browsers on the market today. It blocks third-party data storage. It protects from browser fingerprinting. And it does all this by default. Speed - Brave is built on Chromium, the same technology that powers Google Chrome, and is optimized for speed, providing a fast and responsive browsing experience. Brave Browser also features Brave Rewards, a system that rewards users with Basic Attention Tokens (BAT) for viewing opt-in ads. This innovative system provides an alternative revenue model for content creators and a way to support the Brave community. SlimBrave Neo takes all the good things about Brave and makes them even better by keeping everything clean, light, and privacy-focused. It removes the extra clutter, turns off features you might not need, and cuts down on anything that could slow you down or collect unnecessary data. Because it relies on simple settings and policies instead of modifying the browser itself, you still get full Brave compatibility—just in a smoother, lighter, and more privacy-friendly package. Brave Browser 1.91.171 changelog: General Fixed Cardano not being disabled on upgrade to Brave Origin. Upgraded Chromium to 149.0.7827.103. Origin Removed “Survey Panelist” setting from brave://settings/privacy. Fixed P3A and usage ping under brave://settings/privacy being displayed on first launch on Linux. Upgraded Chromium to 149.0.7827.103. Download: Brave Browser 64-bit | 1.2 MB (Freeware) Download: Brave Browser 32-bit View: Brave Homepage | Offline Installers | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Hi. As the title suggests, I can't access the forum on my phone. I'm using Edge on Android and when I try to navigate to the forum I get a "we value your privacy" popup and none of the buttons are clickable. It effectively stonewalls me from reading any forum content.
    • Honestly you're not wrong about AdGuard. Neowin frequently has lifetime license discounts for them and that's how I got my cheap family license a few years ago to run it on all my devices.
  • Recent Achievements

    • Rookie
      Marzoid went up a rank
      Rookie
    • Community Regular
      coch went up a rank
      Community Regular
    • One Year In
      slackerzz earned a badge
      One Year In
    • One Year In
      highriskpaym earned a badge
      One Year In
    • One Month Later
      highriskpaym earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      519
    2. 2
      PsYcHoKiLLa
      190
    3. 3
      +Edouard
      156
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!