Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7688603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:57:34+00:00 2026-05-31T19:57:34+00:00

So I am doing this as a learning moment and I’m not afraid to

  • 0

So I am doing this as a learning moment and I’m not afraid to say I have no idea what I’m doing here. It might also be worth mentioning that I don’t know much about C++ in this scenario.

In C#, I’ve used DllImport plenty of times to bring in stuff from user32.dll or other DLLs that I haven’t written, but I’m looking to better understand how the other half (the C++ half) is implemented to make this happen.

The C++ code I have is simple and just to verify that the call went through successfully:

#include <iostream>

using namespace std;

__declspec(dllexport) void HelloWorld() {
    cout << "Hello, World" << endl;
}

I don’t know what the importance of __declspec(dllexport) is, but I’ve seen it on a couple websites that didn’t touch much on its importance.

My C# isn’t very different than previous DllImports I’ve done before:

[DllImport("TestDLL.dll")]
static extern void HelloWorld();

static void Main(string[] args) {
    HelloWorld();
}

I’m compiled the C++ DLL and put it in the C# project and it’s copied to the bin folder. When I run the C# project I get an EntryPointNotFoundException at the call to HelloWorld() inside the main function.

My guess is that I need to either change the C++ code or the compilation flags of the C++ project. Currently “Use of MFC” is set to “Use Standard Windows Libraries” and there’s no use of ATL or CLR. Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:57:35+00:00Added an answer on May 31, 2026 at 7:57 pm

    C++ is a language that supports overloading. In other words, you can have more than one version of HelloWorld(). You could also export HelloWorld(int), a distinct version. It is also a language that requires a linker. In order to not confuzzle the linker about the same name for different functions, the compiler decorates the name of the function. Aka “name mangling”.

    The tool you want to use to troubleshoot problems like this is Dumpbin.exe. Run it from the Visual Studio Command Prompt on your DLL with the /exports option. You’ll see this:

    ordinal hint RVA      name
    
          1    0 000110EB ?HelloWorld@@YAXXZ = @ILT+230(?HelloWorld@@YAXXZ)
    

    Bunch of gobbledegook, the exported name is shown in parentheses. Note the ? in the front and @@YAXXZ after the name, that’s why the CLR cannot find the exported function. A function that takes an int argument will be exported as ?HelloWorld@@YAXH@Z (try it).

    The [DllImport] directive supports this, you can use EntryPoint property to give the exported name. Or you can tell the C++ compiler that it should generate code that a C compiler can use. Put extern "C" in front of the declaration and the C++ compiler will suppress the name decoration. And won’t support function overloads anymore of course. Dumpbin.exe now shows this:

    ordinal hint RVA      name
    
          1    0 00011005 HelloWorld = @ILT+0(_HelloWorld)
    

    Note that the name is still not plain “HelloWorld”, there’s an underscore in front of the name. That’s a decoration that helps catch mistakes with the calling convention. In 32-bit code there are 5 distinct ways to call a function. Three of which are common with DLLs, __cdecl, __stdcall and __thiscall. The C++ compiler defaults to __cdecl for regular free functions.

    This is also a property of the [DllImport] attribute, the CallingConvention property. The default that’s used if it isn’t specified is CallingConvention.StdCall. Which matches the calling convention for many DLLs, particularly the Windows ones, but doesn’t match the C++ compiler’s default so you still have a problem. Simply use the property or declare your C++ function like this:

    extern "C" __declspec(dllexport) 
    void __stdcall HelloWorld() {
        // etc..
    }
    

    And the Dumpbin.exe output now looks like:

    ordinal hint RVA      name
    
          1    0 000110B9 _HelloWorld@0 = @ILT+180(_HelloWorld@0)
    

    Note the added @0, it describes the size of the stack activation frame. In other words, how many bytes worth of arguments are passed. This helps catch a declaration mistake at link time, such mistakes are extremely difficult to diagnose at runtime.

    You can now use the [DllImport] attribute as you originally had it, the pinvoke marshaller is smart enough to sort out the decoration of the actual function. You can help it with the ExactSpelling and EntryPoint properties, it will be slightly quicker but nothing you’d ever notice.

    First question last: __declspec(dllexport) is just a hint to the compiler that you intend to export the function from a DLL. It will generate a wee bit of extra code that can help making the exported function call faster (nothing the CLR uses). And passes an instruction to the linker that the function needs to be exported. Exporting functions can also be done with a .def file but that’s doing it the hard way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing this as learning and I have been told that NSString return
I have some code doing this : var changes = document.getElementsByName(from); for (var c=0;
I'm probably doing this all wrong. I have a text file full of data
I'm still learning Perl at the moment and have been trying to get a
I'm learning Mockito at the moment and one of the things I'm doing to
I am doing this at the moment but would like the good guys of
I am doing the text categorization machine learning problem using Naive Bayes. I have
I'm learning Java and OOPS from oracle's website. I'm doing this exercise . I'm
I'm still learning Perl, so there's probably a more efficient way of doing this.
So, I'm new to shell scripts and I'm really doing this as a learning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.