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

  • Home
  • SEARCH
  • 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 8182007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:42:51+00:00 2026-06-07T00:42:51+00:00

I am trying to do 2 things: get a return value from a C

  • 0

I am trying to do 2 things: get a return value from a C dll function, and have that same function modify 1 of the members of a structure that is passed to it. After much experimentation I was able to get the function to return a value, but I am still unable to get it to return a modified value to the C# code; the value remains (unmodified) as 0.
I’ve tried lots of variations (ref, [In,Out], etc) to no avail

using System;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;

namespace Vexing.Problem{
    public class myIdx : VexingObject {
        public myIdx(object _ctx) : base(_ctx) { }
        private IPlotObject plot1;
    [StructLayout(LayoutKind.Sequential)] 
    public class PLEX { public int yowser; }

    [DllImport("my.dll", CharSet = CharSet.Unicode)]
    public static extern int cFunction(
               [MarshalAs(UnmanagedType.LPStruct)] PLEX mPlex);

    PLEX a;
    protected override void Create() { a = new PLEX(); }
    protected override void CalcBar() {
        int mf = cFunction(a);
        plot1.Set(a.yowser); }
}}

// pertinent c dll code
typedef struct s_plex { int yowser;} cplex;

extern "C" __declspec( dllexport )  
int cFunction(cplex *Cplex){ Cplex->yowser = 44; return 1;}
  • 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-06-07T00:42:53+00:00Added an answer on June 7, 2026 at 12:42 am

    Your import declaration is wrong.
    Setting the CharSet in your case doesn’t make any sense (there are no string parameters in the native function declaration).
    If you want to pass class instance, ref/out also must be thrown away (classes being passed by reference).
    And the main point: extern "C" __declspec( dllexport ) means CallingConvention.Cdecl.

    UPDATE. Here’s complete working code sample:
    C++ (header):

    struct CStruct
    {
        int myField;
    };
    
    extern "C" __declspec( dllexport ) int MyFunction(CStruct *pStruct);
    

    C++ (code):

    int MyFunction(CStruct *pStruct)
    {
        pStruct->myField = 100;
        return 1;
    }
    

    C#:

    [StructLayout(LayoutKind.Sequential)]
    class MyStruct
    {
        public int myField;
    };
    
    class Program
    {
        MyStruct myStruct = new MyStruct();
    
        [DllImport("MyLib.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern int MyFunction(MyStruct pStruct);
    
        static void Main(string[] args)
        {
            var p = new Program();
            var result = MyFunction(p.myStruct);
    
            Console.WriteLine("Result: {0}, MyStruct.myField = {1}", result, p.myStruct.myField);
        }
    }
    

    Prints:

    Result: 1, MyStruct.myField = 100

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

Sidebar

Related Questions

I have been trying different things and still have been able to get exactly
I'm trying to get the four or five things that happened on this day
I am trying to make a function that can return the prime factors of
I'm trying to get a value from a variable inside some files, here is
I have been struggling for days trying to get a simple ActiveX DLL to
After some time researching and trying different things I still cannot get my @ExceptionHandler
Just trying to get my head around what can happen when things go wrong
I'm trying to get a sequence of things to happen in the correct order,
I'm trying to achieve two things, both of which I fail to get at.
I'm trying to create a DLL that will later be used in Inno Setup.

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.