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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:06:09+00:00 2026-05-27T01:06:09+00:00

I have a tricky bug I can’t find. I’m doing late-binding on from C#

  • 0

I have a tricky bug I can’t find. I’m doing late-binding on from C# to a native DLL I wrote.
The late-binding seem to work fine. The problems started after I added the callbacks.

The callbacks are defined as so (in c)(On a global scale in the DLL):

typedef void (*FinishedDelegate) (ProcessResult a_bResult);

typedef void (*DownloadStatusUpdateDelegate) (int a_iParametersDownloaded, int a_iTotalParameters);
typedef void (*DownloadFinishedDelegate) (char* a_sConfiguration_values, ProcessResult a_bResult);

DownloadStatusUpdateDelegate pfDownloadStatusUpdateDelegate = NULL;
DownloadFinishedDelegate pfDownloadFinishedDelegate = NULL;

This function is exported:

PLUGIN_API BOOL DownloadConfigration(DownloadStatusUpdateDelegate a_pfStatusDelegate, DownloadFinishedDelegate a_pfFinishedDelegate);

And this is the native function implementation:

DWORD WINAPI DownloadThreadFunc(void* a_pParam)
{
    while (g_iParameterDownloaded < PARAMETER_COUNT)
    {
        if (IsEventSignaled(g_hAbortEvent))
        {
            CallDownloadFinished(PROCESS_ABORT);
            return 0;
        }

        Sleep(STATUS_DELAY);
        CallDownloadStatusUpdate();
        g_iParameterDownloaded += STATUS_PARAMS_JUMP;
    }

    CallDownloadFinished(PROCESS_SUCCESS);

    return 0;
}

PLUGIN_API BOOL DownloadConfigration(DownloadStatusUpdateDelegate a_pfStatusDelegate, DownloadFinishedDelegate a_pfFinishedDelegate)
{
    if (IsEventSignaled(g_hInProcessEvent))
        return false;


    pfDownloadStatusUpdateDelegate = a_pfStatusDelegate;
    pfDownloadFinishedDelegate = a_pfFinishedDelegate;

    g_iParameterDownloaded = 0;

    DWORD l_dwResult = WaitForSingleObject(g_hThreadsStructGuardian, INFINITE);
    if (l_dwResult == WAIT_OBJECT_0)
    {
        g_ahThreads[PLUGIN_THREAD_DOWNLOAD] = CreateThread(NULL, 0, DownloadThreadFunc, 0, 0, NULL);
        ReleaseMutex(g_hThreadsStructGuardian);
        return true;
    }

    return false;
}

On the managed side, the function is called here:

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        public delegate void DownloadStatusUpdateDelegate(int a_iParametersDownloaded, int a_iTotalParameters);
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate void DownloadFinishedDelegate_Native(StringBuilder a_sConfigurationValues, EPluginProcessResult a_eResult);

        private void OnDownloadStatusUpdate(int a_iParametersDownloaded, int a_iTotalParameters)
        {
            if (DownloadStatusUpdate != null)
            {
                DownloadStatusUpdate(a_iParametersDownloaded, a_iTotalParameters);
            }
        }

        private void OnDownloadFinished(StringBuilder a_sConfigurationValues, EPluginProcessResult a_eResult)
        {
            if (DownloadFinished != null)
            {
                DownloadFinished(a_sConfigurationValues.ToString(), a_eResult);
            }
        }

        public bool DownloadConfiguration()
        {
            bool l_bResult = DLLDownloadConfigration(OnDownloadStatusUpdate, OnDownloadFinished);

            return l_bResult;
        } 

The weird thing is – It works for a while. I get a “Privileged Instruction” exception after some time, but as I lower STATUS_DELAY, it happens less. The exception shows up at the IsEventSignaled function – But there’s simply nothing there.

The download thread syncs to the c# GUI thread to update the GUI.

I’ve been on this problem for way too much time. It looks like a classic calling conventions problem, but I verified it thoroughly!

Any ideas?

  • 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-27T01:06:10+00:00Added an answer on May 27, 2026 at 1:06 am
     bool l_bResult = DLLDownloadConfigration(OnDownloadStatusUpdate, OnDownloadFinished);
    

    The code isn’t very clear, but this is the likely trouble spot. This creates two delegate objects, the callback bombs after the garbage collector runs and deletes the objects. It cannot track managed object references into unmanaged code. You’ll need to create the delegates explicitly and store them in a class member so the garbage collector always sees at least one reference.

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

Sidebar

Related Questions

I have tricky problem which I can't completely understand. It's jQuery tabs with cookie
I have a tricky regex. numbers are is delimited by periods . there can
I currently have a tricky bug that occurs in a place where I don't
I have a tricky problem that I've been messing about with for a few
I have a tricky problem that has had me scratching my head for a
I have a very tricky situation (for my standards) in hand. I have a
I have a very tricky problem going on with content inside a textarea on
I have the following tricky problem: I have implemented a (rather complicated) class which
I have sort of a tricky problem I'm attempting to solve. First of all,
I have run into a bit of a tricky problem in some C++ code,

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.