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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:18:33+00:00 2026-05-20T06:18:33+00:00

I’m new to C++. I have been told using a callback with C++ is

  • 0

I’m new to C++. I have been told using a “callback” with C++ is the best solution for this. Here is my situation.

I have a DLL written in C++
this DLL has a method to start the service which is run via the C# code (this works fine)
when the service in the DLL runs I want the DLL to pass back text to the C# code, this is just progress code such as “stage one starting ” and “stage one completed”

I have looked around and been told that the best way to achieve this is to use callbacks, I don’t really have a clue how to implement this. Does anyone have any suggestions or articles out there I can check out? Please include C++ as I have zero experience in C++.

Cheers

  • 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-20T06:18:34+00:00Added an answer on May 20, 2026 at 6:18 am

    You can simply pass a C# string back to C++ and a C++ string to C#. The requirement is that the string is unicode and the allocation method is SysAllocString and not malloc. Any ASCII string you need to convert to unicode.

    const wchar_t* theString = L"hello";
    BSTR bstr = SysAllocString(theString);
    DoSomething(bstr);
    SysFreeString(bstr);
    

    And this to register the C# dll

    Assembly asm = Assembly.LoadFile (@"c:\temp\ImageConverter.dll");
    RegistrationServices regAsm = new RegistrationServices();
    bool bResult = regAsm.RegisterAssembly(asm, AssemblyRegistrationFlags.SetCodeBase);
    

    And this to convert Unicode to ASCII and vice-versa.

    inline BSTR Cstring2VBstring(char *szString)
    {
        WCHAR* res = NULL;
        BSTR bs;
        DWORD n;
        char *sz = NULL;
        if (*szString && szString)
        {
            sz = strdup(szString);
            n = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, -1, NULL, 0);
    
            if (n)
            {
                res = (WCHAR*) malloc(n * sizeof(char) );
                MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, -1, res, n);
            }
    
        }
    
        bs = SysAllocString( (const OLECHAR*) res);
        free(sz);
        return bs;
    }
    
    
    
    // C String to BSTR conversion (2)
    BSTR Cstringn2VBstring(char *szString, int dwSize)
    {
        WCHAR* res = NULL;
        BSTR bs;
        DWORD n = (DWORD) dwSize;
        char *sz = NULL;
        if (*szString)
        {
            sz = (char*) malloc(dwSize);
            memcpy(sz, szString, dwSize);
            n = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, n, NULL, 0);
            if(n)
            {
                res = (WCHAR*) malloc(n * sizeof(char) );
                MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, -1, res, n);
            }
        }
        bs = SysAllocStringLen( (const OLECHAR*) res, n);
    
        free(sz);
        return bs;
    }
    

    And the .NET code:

    Namespace TestLibrary2
        ' Interface declaration. '
        Public Interface ICalculator
            Function Add(ByVal Number1 As Integer, ByVal Number2 As Integer) As Integer
            Function Subtract(ByVal Number1 As Long, ByVal Number2 As Long) As Long
            Function ReturnValue() As String
            Function Concat(ByVal Number1 As String, ByVal Number2 As String) As String
    
            Sub Concat2(ByVal Number1 As String, ByVal Number2 As String)
    
            Function isTrue(ByVal bInputvalue As Boolean) As Boolean
            Function isTrue2(ByRef bInputvalue As Boolean) As Boolean
        End Interface
    
    
    
        ' Interface implementation. '
        Public Class ManagedClass
            Implements ICalculator
    
    
            Public Function Add(ByVal Number1 As Integer, ByVal Number2 As Integer) As Integer Implements ICalculator.Add
                Return Number1 + Number2
            End Function
    
    
            Public Function Subtract(ByVal Number1 As Long, ByVal Number2 As Long) As Long Implements ICalculator.Subtract
                Try
                    System.IO.File.WriteAllText("c:\temp\subtract.txt", "Subtracted: ")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
    
                Return Number1 - Number2
            End Function
    
    
            Public Function Concat(ByVal Number1 As String, ByVal Number2 As String) As String Implements ICalculator.Concat
                Try
                    System.IO.File.WriteAllText("c:\temp\Concat.txt", "Nummer1: " + Number1 + vbCrLf + "Nummer2:" + Number2)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
    
                Dim strReturnValue As String = Number1 + Number2
                Return strReturnValue
            End Function
    
    
            Public Sub Concat2(ByVal Number1 As String, ByVal Number2 As String) Implements ICalculator.Concat2
                Console.WriteLine("moo")
            End Sub
    
    
            Public Function ReturnValue() As String Implements ICalculator.ReturnValue
                Dim x As String = "moooooo"
                Return x
            End Function
    
    
            Public Function isTrue(ByVal bInputvalue As Boolean) As Boolean Implements ICalculator.isTrue
                If bInputvalue = True Then
                    Return True
                End If
                Return False
            End Function
    
    
            Public Function isTrue2(ByRef bInputvalue As Boolean) As Boolean Implements ICalculator.isTrue2
                If bInputvalue = True Then
                    Return True
                End If
                Return False
            End Function
    
        End Class
    
    
    End Namespace
    

    Edit:
    See here for closer information:

    http://support.microsoft.com/kb/828736
    http://msdn.microsoft.com/en-us/library/ms734686.aspx

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I want use html5's new tag to play a wav file (currently only supported
I've got a string that has curly quotes in it. I'd like to replace
I'm looking for suggestions for debugging... If you view this site in Firefox or
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.