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

  • Home
  • SEARCH
  • 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 5845577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:27:17+00:00 2026-05-22T12:27:17+00:00

class mydialog declaration. ……………………… my work is to create a window with a button

  • 0

class mydialog declaration.
………………………
my work is to create a window with a button and a textbox and when i click that button for the 1st time a thread will be created which will be reading on the port selected and for the subsequent data in the textbox on button click it will just write to the port rather than creating a thread for reading.

#include "resource.h"

class mydialog : public CDialog
{
    DECLARE_DYNAMIC(mydialog)

public:
    mydialog(CWnd* pParent = NULL);   // standard constructor
    virtual ~mydialog();
    static HANDLE hSerial4;
    static int loop_count;

// Dialog Data
    enum { IDD = IDD_DIALOG1 };

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

    DECLARE_MESSAGE_MAP()
public:
    CString m_name;
    afx_msg void OnBnClickedButton1();
    BOOL OnInitDialog();
    static UINT read_data(LPVOID hSerial);
//  void read_data(HANDLE);
    CString select_port;
};

cpreparationapp.cpp
………………………

#include "stdafx.h"
#include "mydialog.h"

class CPreparationApp : public CWinApp
{
public:
    BOOL InitInstance();
};


BOOL CPreparationApp::InitInstance()
{

    mydialog Dlg;
    Dlg.DoModal();
    m_pMainWnd = &Dlg;
    return TRUE;
}

CPreparationApp theApp;

mydialog.cpp
……………………..

#include "stdafx.h"
#include "mydialog.h"
#include <windows.h>
#include <process.h> 



// mydialog dialog

IMPLEMENT_DYNAMIC(mydialog, CDialog)

mydialog::mydialog(CWnd* pParent /*=NULL*/)
    : CDialog(mydialog::IDD, pParent)
    , m_name(_T(""))
    , select_port(_T(""))
{

}

mydialog::~mydialog()
{
}

void mydialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT1, m_name);
    DDX_CBString(pDX, IDC_COMBO1, select_port);
}


BEGIN_MESSAGE_MAP(mydialog, CDialog)
    ON_BN_CLICKED(IDC_BUTTON1, &mydialog::OnBnClickedButton1)
END_MESSAGE_MAP()


// mydialog message handlers
void mydialog::OnBnClickedButton1()
{
    CEdit* abc=(CEdit*)GetDlgItem(IDC_EDIT1);
    LPTSTR xyz123=new TCHAR[50];
    CString text;
    int k=abc->GetWindowTextA(xyz123,20);
    CComboBox * pCombo=(CComboBox *)GetDlgItem(IDC_COMBO1);
    int item = pCombo->GetCurSel();
    if(item != CB_ERR)
    {
        pCombo->GetLBText(item,text);
    }
    TRACE("%s\n",text); 
    loop_count++;
    HANDLE hSerial;
    if(loop_count==1)
    {
    hSerial = CreateFile(text,
    GENERIC_WRITE | GENERIC_READ,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    0,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    0);

    if(hSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND)
        {
            TRACE("serial port does not exist for writing\n");
        //serial port does not exist. Inform user.
        }
            TRACE("some other error,serial port does not exist for writing\n");
        //some other error occurred. Inform user.
    }

    DCB dcbSerialParams = {0};

    dcbSerialParams.DCBlength=sizeof(dcbSerialParams);

    if (!GetCommState(hSerial, &dcbSerialParams)) 
    {
                    TRACE("error getting state for writing\n");
    //error getting state
    }

    dcbSerialParams.BaudRate=CBR_19200;
    dcbSerialParams.ByteSize=8;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;

    if(!SetCommState(hSerial, &dcbSerialParams))
    {
                    TRACE("error setting state for writing\n");
    //error setting serial port state
    }
    COMMTIMEOUTS timeouts={0};

    timeouts.ReadIntervalTimeout=50;
    timeouts.ReadTotalTimeoutConstant=50;
    timeouts.ReadTotalTimeoutMultiplier=10;

    timeouts.WriteTotalTimeoutConstant=50;
    timeouts.WriteTotalTimeoutMultiplier=10;

    if(!SetCommTimeouts(hSerial, &timeouts))
    {
                    TRACE("some error occured for writing\n");
        //error occureed. Inform user
    }
    mydialog::hSerial4=hSerial;
    }
    int n=100;
    char szBuff1[100];
    int m=0;
    while(m<100 && xyz123[m]!='\0')
    {
        szBuff1[m]=xyz123[m];
        m++;
    }
    szBuff1[m]='\0';
    DWORD dwByteswrote = 0;

    if(!WriteFile(mydialog::hSerial4, szBuff1, n, &dwByteswrote, NULL))
    {
                    TRACE("error writing \n");
    }           
    TRACE("%d\n",dwByteswrote);
//  mydialog Dlg1;
    if(loop_count==1)
    {
    CWinThread *m_pThread;
    m_pThread=AfxBeginThread(read_data,(LPVOID)hSerial);
    }

}

 UINT mydialog::read_data(LPVOID hSerial5)
{
    HANDLE hSerial1=(HANDLE)hSerial5;
    int n=100;
    char szBuff[100];
    DWORD dwBytesRead = 0;

    if(!ReadFile(hSerial1, szBuff, n, &dwBytesRead, NULL))
    {
                    TRACE("error reading \n");
        //error occurred. Report to user.
    }

    TRACE("%s\n",szBuff);
//  TRACE("%d\n",dwBytesRead);
    CloseHandle(hSerial1);

    return 1;
}



BOOL mydialog::OnInitDialog()
{
//  BOOL x;
    mydialog dialog2;
    HANDLE hSerial3;
    CString temp,temp1;
    int n=0;
    CComboBox * pCombo=(CComboBox *)GetDlgItem(IDC_COMBO1);
    pCombo->AddString("SAMPLE1");
    pCombo->AddString("SAMPLE2");
    for(n=0;n<9;n++)
    {
        temp1.Format("%d",n);
        temp="COM"+temp1;
        hSerial3 = CreateFile(temp,
        GENERIC_WRITE | GENERIC_READ,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        0,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        0);
        if(hSerial3==INVALID_HANDLE_VALUE)
        {

            TRACE("port not available\n");
        }
        else
        {   
            pCombo->AddString(temp);
            CloseHandle(hSerial3);
            TRACE("port available\n");
        }

    }       
//  x=dialog2.check_open_port();
    return CDialog::OnInitDialog();
}
  • 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-22T12:27:18+00:00Added an answer on May 22, 2026 at 12:27 pm

    You have declared the static members in your class but haven’t defined them, so you are getting linker errors.

    Provide the definitions outside the class body

    HANDLE mydialog::hSerial;
    int mydialog::loop_count;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a dialog from a non-UI thread, in onUtteranceCompleted(): runOnUiThread(
I'm writing an Android app that communicates with a website. Any time I hit
class Foo { static bool Bar(Stream^ stream); }; class FooWrapper { bool Bar(LPCWSTR szUnicodeString)
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
class A : IFoo { } ... A[] arrayOfA = new A[10]; if(arrayOfA is
class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;
class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField() Is there a built-in way to
class AbstractQuery { virtual bool isCanBeExecuted()=0; public: AbstractQuery() {} virtual bool Execute()=0; }; class
class C { T a; public: C(T a): a(a) {;} }; Is it legal?
class Score { var $score; var $name; var $dept; var $date; function Score($score, $name,

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.