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 7653635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:04:28+00:00 2026-05-31T12:04:28+00:00

I have the following C++ code: #pragma once #include StdAfx.h #include <string> using namespace

  • 0

I have the following C++ code:

#pragma once
#include "StdAfx.h"
#include <string>
using namespace std;
using namespace System;


extern "C" __declspec( dllexport ) string __stdcall GetVale()
{
    return "test";
}

I am trying to call this function in C# by doing this:

    [DllImport("Security.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, EntryPoint = "_GetVale@4")]
    internal static extern string Getvalue();

I am doing this just to learn and understand really. When I call this I get a PInvoke exception saying my CallingConvention is not correct. I am sure my mistakes in my files are very small. I know the entry point is “_GetVale@4” for I used a program to find it. If I can’t change that to anything else it throws a entry point not found, so my problem is some where else.

What exactly am I doing wrong? Thanks!

  • 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-31T12:04:29+00:00Added an answer on May 31, 2026 at 12:04 pm

    You can’t use simple p-invoke for functions that use C++ types. You should restrict yourself to pure c.


    Returning a char* is rarely the correct solution. You get lifetime issues: It’s unclear when and how the return value should be freed.

    One standard pattern is the caller passing in a char* and a length, and the callee filling this buffer.

    C

    extern "C" __declspec( dllexport ) void __stdcall GetValue(char* buf, in length)
    

    C#

    [DllImport("Security.dll", CallingConvention = CallingConvention.StdCall, Charset = CharSet.Ansi]
    internal static extern void GetValue(StringBuilder buf, int length);
    

    There are a few ways to work with C++ directly:

    1. Create a SWIG wrapper
    2. Use CXXI

    I’m not too fond of SWIG. It’s a bit annoying to work with, and it requires a C++ sided wrapper. CXXI sounds interesting, but I haven’t used it myself.

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

Sidebar

Related Questions

I have the following code in a header only file. #pragma once class error_code
I have following code using loop unrolling: #pragma unroll for (int i=0;i<n;i++) { ....
I have the following C/C++ code using OpenMP: int nProcessors=omp_get_max_threads(); if(argv[4]!=NULL){ printf(argv[4]: %s\n,argv[4]); nProcessors=atoi(argv[4]);
i have the following main code: #include ComHandler.h #include socketMessage.h #define THIS_IP localhost #define
I have the following code... public partial class DownloadFile : System.Web.UI.Page { protected void
I am new to OpenMP. I have the following code which compiles fine using
I have the following code in VS2008: int i,j; bool pr = false; #pragma
I have the following code : #define LIMIT_DATE \01-03-2010\ #ifdef LIMIT_DATE #if _MSC_VER #pragma
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section

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.