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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:30:51+00:00 2026-05-19T00:30:51+00:00

I have a dll which includes a function called ReadPort that reads data from

  • 0

I have a dll which includes a function called ReadPort that reads data from serial COM port, written in c/c++. This function is called within an extra thread from another WINAPI function using the _beginthreadex. When COM port has data to be read, the worker thread returns the data, ends normaly, the calling thread closes the worker’s thread handle and the dll works fine.

However, if ReadPort is called without data pending on the COM port, when timeout occurs then WaitForSingleObject returns WAIT_TIMEOUT but the worker thread never ends. As a result, virtual memory grows at about 1 MB every time, physical memory grows some KBs and the application that calls the dll becomes unstable. I also tryied to use TerminateThread() but i got the same results.

I have to admit that although i have enough developing experience, i am not familiar with c/c++. I did a lot of research before posting but unfortunately i didn’t manage to solve my problem.

Does anyone have a clue on how could i solve this problem? However, I really want to stick to this kind of solution. Also, i want to mention that i think i can’t use any global variables to use some kind of extra events, because each dll’s functions may be called many times for every COM port.

I post some parts of my code below:

The Worker Thread:

unsigned int __stdcall ReadPort(void* readstr){

DWORD  dwError; int   rres;DWORD  dwCommModemStatus, dwBytesTransferred;
int ret;
char szBuff[64] = "";

ReadParams* params = (ReadParams*)readstr;

ret = SetCommMask(params->param2, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);
if (ret == 0)
{
    _endthreadex(0);
    return -1;
}
ret = WaitCommEvent(params->param2, &dwCommModemStatus, 0);
if (ret == 0)
{
    _endthreadex(0);
    return -2;
}
ret = SetCommMask(params->param2, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD| EV_RING);
if (ret == 0)
{
    _endthreadex(0);
    return -3;
}

if (dwCommModemStatus & EV_RXCHAR||dwCommModemStatus & EV_RLSD)
{
    rres = ReadFile(params->param2, szBuff, 64, &dwBytesTransferred,NULL);
    if (rres == 0)
    {
        switch (dwError = GetLastError())
        {
            case ERROR_HANDLE_EOF:
            _endthreadex(0);
            return -4;
        }

        _endthreadex(0);
        return -5;
    }
    else
    {
        strcpy(params->param1,szBuff);
        _endthreadex(0);
        return 0;
    }
}
else
{
    _endthreadex(0);
    return 0;
}
_endthreadex(0);
return 0;}

The Calling Thread:

int WINAPI StartReadThread(HANDLE porthandle, HWND windowhandle){

HANDLE hThread;
unsigned threadID;
ReadParams readstr;
DWORD ret, ret2;

readstr.param2 = porthandle;

hThread = (HANDLE)_beginthreadex( NULL, 0, ReadPort, &readstr, 0, &threadID );
ret = WaitForSingleObject(hThread, 500);

if (ret == WAIT_OBJECT_0)
{
    CloseHandle(hThread);  
    if (readstr.param1 != NULL)
        // Send message to GUI
    return 0;
}
else if (ret == WAIT_TIMEOUT)
{
    ret2 = CloseHandle(hThread);
    return -1;
}
else
{
    ret2 = CloseHandle(hThread);
    if (ret2 == 0)
    return -2;
}}

Thank you in advance,

Sna.

  • 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-19T00:30:52+00:00Added an answer on May 19, 2026 at 12:30 am

    Don’t use WaitCommEvent. You can call ReadFile even when there is no data waiting.

    Use SetCommTimeouts to make ReadFile itself timeout, instead of building a timeout on the inter-thread communications.

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

Sidebar

Related Questions

i have a .net dll written in c# which reads data off a data
I have a C++ dll which implements several COM interfaces, that I'm trying to
I have a DLL which outpus data to a stream. But it adds a
I have a DLL which provided a decoding function, as follows: function MyDecode (Source:
I have a .dll library which exports a function in the following format: _Java_folder1_folder2_folder3_JavaClassName_javamethodname@16
I have a dll called Test.dll in which I have a class called ABC
I have a DLL with one single function That gets five doubles and one
I have a project that includes files from a different project. I am getting
Background: I have a DLL I created that includes 2 c files. These c
I have a DLL which has a function which accepts a function pointer converts

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.