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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:58:37+00:00 2026-05-15T19:58:37+00:00

I’m using a library (ANet) which is written natively in C and I am

  • 0

I’m using a library (ANet) which is written natively in C and I am interfacing with it using C# via a small subset wrapper dll which i’ve made in C and statically links to it.
The library (ANet) has a DllMain which simply tracks the number of references to it (so “Process attach” and “Thread attach” counts) but it seems to go into the negative (i.e. more detaches than attaches).

Here’s ANet’s DllMain. It’s quite simple:

BOOL WINAPI DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
{
    static int procRefCount = 0;
    static int threadRefCount = 0;

    switch (fdwReason) {
    case DLL_PROCESS_ATTACH:
        procRefCount++;
        break;

    case DLL_PROCESS_DETACH:
        procRefCount--;
        break;

    case DLL_THREAD_ATTACH:
        threadRefCount++;
        break;

    case DLL_THREAD_DETACH:
        threadRefCount--;
        break;

    }

    if (procRefCount < 0) {
        MessageBox( NULL, "Bug - negative processes?", "DP DLL Error", MB_OK|MB_ICONERROR );
        return FALSE;
    }
    if (procRefCount > 1) {
        MessageBox( NULL, "Bug - too many processes trying to use DP", "DP DLL Error", MB_OK|MB_ICONERROR );
        return FALSE;
    }
    if (threadRefCount < 0) {
        MessageBox( NULL, "Bug - negative threads?", "DP DLL Error", MB_OK|MB_ICONERROR );
        return FALSE;
    }
    /* Only the first thread is allowed to join? */
    if (threadRefCount > 0) {
        //MessageBox( NULL, "Bug - too many threads trying to use DP", "DP DLL Error", MB_OK|MB_ICONERROR );
        return FALSE;
    }

    return(TRUE);
}

.. Which i’m aiming to not change so I don’t have to recompile and redistribute this prebuilt library.
So since C# seems to randomly create and destroy threads, DllMain is called sporadically throughout my program.
The thing is that the messagebox saying “Bug – negative threads?” will often also display!

Why would there be more THREAD_DETACH messages than THREAD_ATTACH messages?

  • 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-15T19:58:37+00:00Added an answer on May 15, 2026 at 7:58 pm

    Because more threads exited during the lifetime of the DLL than were created during the lifetime of the DLL.

    Threads don’t remember which DLLs were present when they were created, so you get a detach message for every thread which exits after the DLL was loaded, but you only get attach messages for threads which are created after the DLL was loaded. So if you create n threads, load a DLL, then close those threads, the DLL sees n DLL_THREAD_DETACH messages but no DLL_THREAD_ATTACH messages.

    It’s up to the DLL to decide whether or not to do anything with the detached thread and to remember whether or not it has seen it before; the ‘if’ is important in ‘If the DLL has stored a pointer to allocated memory in a TLS slot, it should use this opportunity to free the memory.’ MSDN

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

Sidebar

Related Questions

No related questions found

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.