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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:12:39+00:00 2026-06-09T13:12:39+00:00

I have this code in a c++ file, with compiles to a dll. #include

  • 0

I have this code in a c++ file, with compiles to a dll.

#include "stdafx.h"

#include "WHUU.h"

#include "stdafx.h"

typedef int (__stdcall * Callback)(const int text);

static int x , y= 0;

Callback Handler = 0;

int getX()
{
    return x;
}

void setX(int i)
{
    x = i;
}

void setY(int i)
{
    y = i;
}
int getY()
{
    return y;
}
extern "C" __declspec(dllexport)
void __stdcall SetCallback(Callback handler) {
  Handler = handler;
}


extern "C" __declspec(dllexport)
void __stdcall addX(int x) {
    setX(x);
}

extern "C" __declspec(dllexport)
void __stdcall addY(int y) {
    setY(y);


}


extern "C" __declspec(dllexport)
void __stdcall TestCallback() {
    int z = getX() + getY();
  int retval = Handler(z);
}

My c# application now has to load this dll on runtime. Add to the callback and call the functions. I dont want to use a class. I could load the class and with

 Type[] types = moduleAssembly.GetTypes();

But this overkill! Also c++ is not managed.

I mean its so tiny! (and yes this is an example , but the “real” is just as big as this example).

How do i do that?

Thanks you for your help!

add:

  • i dont like frameworks (like pinvoke / assembly)

  • the function names / types are fixed and will never change (think of a driver.dll read write)

  • this dll is written by customers so it should be as easy as possible!

  • 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-09T13:12:41+00:00Added an answer on June 9, 2026 at 1:12 pm

    I used P/Invoke, post my own cause the callback was not included by the others. But they were good answers! The C# Applicationhas use this GUI.

    The Code for the Application is this : (mind it is a prototype to answer a technical question)

            public partial class Form1 : Form
            {
    
            private delegate int Callback(int text);
            private Callback mInstance;   // Ensure it doesn't get garbage collected
    
            [DllImport(@"C:\Visual Studio 2010\Projects\kalkulatorius\Debug\WHUU1.dll")]
            private static extern void SetCallback(Callback fn);
            [DllImport(@"C:\Visual Studio 2010\Projects\kalkulatorius\Debug\WHUU1.dll")]
            private static extern void addX(int x);
            [DllImport(@"C:\Visual Studio 2010\Projects\kalkulatorius\Debug\WHUU1.dll")]
            private static extern void addY(int y);
            [DllImport(@"C:\\Visual Studio 2010\Projects\kalkulatorius\Debug\WHUU1.dll")]
    
            private static extern void TestCallback(); 
    
            private int Handler(int text) 
            {
              textBox3.Text = text.ToString();
              return 42;                       
            }
    
    
            private void button1_Click(object sender, System.EventArgs e)
            {
              mInstance = new Callback(Handler); // to set the callback in lib 
              SetCallback(mInstance);            // could also be withhin constructor!
    
              addX(int.Parse(textBox1.Text)); // other people in this thread posted this correct answer
              addY(int.Parse(textBox2.Text));
              TestCallback();
            }
    
            public Form1()
            {
              InitializeComponent();
            }
    

    So if you want to use a c++ lib with functions and callbacks (like in the question posted) you can use this.

    But how to change the lib?

    • The Path to the lib is hardcoded (see [dllImport ……] ). But you
      can swop the lib in filesystem. This can happen AFTER you build this
      application.
    • If the lib is not on the given path an exception is thrown. So if
      your program wants to use this lib first check the lib is present on the filesystem.
      (not included in this simple prototype)
    • So to switch functionality (swop a driver lib ect) you copy a different lib with the
      same name into the given path. (Copy paste – maybe change name)

    This solution has the least overhead and is good if you never change the interface of an dll after the application was build!

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

Sidebar

Related Questions

I have this code which will include template.php file from inside each of these
I have this code in PHP writing views to a text-file and just increasing
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
I have this code in a xaml file now in a event handler code
I have this code to download a file, but on sourceforge.net sever there is
I have this code to move my uploaded file to a specific directory: if
I have this code that reads from XML file. It gets five strings (groupId,
I have this code to write to a file, it works perfect but I
I have this code that saves a pdf file. FileStream fs = new FileStream(SaveLocation,
I have this code in a .html.erb file: <script src=http://connect.facebook.net/en_US/all.js#xfbml=1></script> <script> FB.Event.subscribe('edge.create', function(response) {

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.