Solved

Application->Terminate Problem

Posted on 2001-07-09
16
605 Views
Last Modified: 2012-05-04
Hi Experts

i have a small problem...
I wanna terminate an application if there is a conflict (COMM-PORT)
but nothing happens... don't know why. thx for your help.

Code:

DCB dcbCommPort;

  // COM Port offnen.

  hComm = CreateFile("COM2",
                      GENERIC_READ | GENERIC_WRITE,
                      0,
                      0,
                      OPEN_EXISTING,
                      0,
                      0);

  // IF THE PORT CANNOT BE OPENED, SET ERROR CODE.

  if(hComm == INVALID_HANDLE_VALUE) Application->Terminate();
0
Comment
Question by:RideOn
  • 8
  • 6
  • 2
16 Comments
 
LVL 1

Author Comment

by:RideOn
Comment Utility
okay,  the comments in the source are not like they should be... :)) sorry
right version:

DCB dcbCommPort;

  // OPEN COMM PORT

  hComm = CreateFile("COM2",
                      GENERIC_READ | GENERIC_WRITE,
                      0,
                      0,
                      OPEN_EXISTING,
                      0,
                      0);

  // IF THE PORT CANNOT BE OPENED, TERMINATE APPLICATION.

  if(hComm == INVALID_HANDLE_VALUE) Application->Terminate();
0
 
LVL 2

Expert Comment

by:GloriousRain
Comment Utility
SendMessage(WM_CLOSE);
0
 
LVL 1

Author Comment

by:RideOn
Comment Utility
hm, that doesn't work...
"conversion from 'int' to 'void*' not possible"
i'm nor really good in windows-c++ programing 'cause i normally code c/c++ for embedded systems that's why i have some problems with APIs and such stuff...
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
http://www.axter.com/faq/topic.asp?TOPIC_ID=24&FORUM_ID=4&CAT_ID=9&Topic_Title=How+to+launch+a+program+from+another+program+in+VC&Forum_Title=C%2FC%2B%2B

In the above link, I've created a wrapper class for the CreateProcess function.  The class is called CreateProcessMngr.  It's listed at the bottom of the above link.

The class is able to force a process to close by using the following method.

void CreateProcessMngr::CloseApp(void)

{

::PostMessage(m_hWnd, WM_SYSCOMMAND,(WPARAM)SC_CLOSE,0);

::CloseHandle(pi.hProcess);

::TerminateThread(pi.hThread,0);

::TerminateProcess(pi.hProcess,0);

}


0
 
LVL 2

Expert Comment

by:GloriousRain
Comment Utility
Him
if you use SendMessage API the first para is HWND, then WM_CLOSE
Ex:
::SendMessage(AfxGetMainWnd()->m_hWnd, WM_CLOSE, 0 ,0);
0
 
LVL 1

Author Comment

by:RideOn
Comment Utility
okay, then i first have to get the hwnd of my window...right?
0
 
LVL 2

Expert Comment

by:GloriousRain
Comment Utility
Yes, that's right, if you use SendMessage API.
0
 
LVL 1

Author Comment

by:RideOn
Comment Utility
okay, then i first have to get the hwnd of my window...right?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:RideOn
Comment Utility
i now did it this way:

if(hComm == INVALID_HANDLE_VALUE)
    {
    Application->MessageBox("Communication Error, terminate Application", "Error", 16);
    SendMessage(GetActiveWindow (), WM_CLOSE, 0 ,0);
    }

doesn't work either....
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Did you try using the functions I posted?
CloseHandle, TerminateThread, and TerminateProcess?
0
 
LVL 2

Expert Comment

by:GloriousRain
Comment Utility
Can you compile it?
If not, let try
::SendMessage(::GetActiveWindow (), WM_CLOSE, 0 ,0);
0
 
LVL 1

Author Comment

by:RideOn
Comment Utility
no Axter not yet
it seems to be complicated....i want to have small, easy code... if GloriousRain can't help me, i'll try your code.
but thy anyway

GloriousRain yes, can compile it but it doesn't terminate the application.
0
 
LVL 2

Expert Comment

by:GloriousRain
Comment Utility
Because your window doesn't create first in this case. GetActiveWindow() return NULL. Where do you place above codes (Initinstance() or somewhere else?). If in InitInstance just return FALSE insteads SendMessage
0
 
LVL 1

Author Comment

by:RideOn
Comment Utility
This is the whole code at the moment:

/---------------------------------------------------------------------------
#include <vcl.h>
#include <stdlib.h>

#pragma hdrstop

#include "Unit1.h"

//-- GLOBAL VARIABLES --
HANDLE hComm = NULL;
COMMTIMEOUTS ctmoNew = {0}, ctmoOld;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TForm1 *Form1;

void A (void);
void B (void);
void C (void);
void Config (void);
int OpenComm (void);

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{

}

//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{

Form1->Label2->Caption = "";

for (int i=0;i<=ParamCount();i++)
  {
    if (LowerCase(ParamStr(i)) == "a")
    {
        Form1->Label1->Caption = "Programm A";
        A();
    }
    else if (LowerCase(ParamStr(i)) == "b")
    {
        Form1->Label1->Caption = "Programm B";
        B();
    }
    else if (LowerCase(ParamStr(i)) == "c")
    {
        Form1->Label1->Caption = "Programm C";
        C();
    }
    else if (LowerCase(ParamStr(i)) == "config")
    {
        Form1->Label1->Caption = "Konfigurations-Modus";
        Config();
    }
    else
    {
        Form1->Label1->Caption = "Keiner oder ungultiger Parameterwert";
        Form1->Label2->Caption = "Parameterliste in der Datei ReadMe.txt";
    }
   }
}
//---------------------------------------------------------------------------

int OpenComm (void)
{
 int Error;
 Error = 0;

 DCB dcbCommPort;

  // COM Port offnen.

  hComm = CreateFile("COM2",
                      GENERIC_READ | GENERIC_WRITE,
                      0,
                      0,
                      OPEN_EXISTING,
                      0,
                      0);

  // IF THE PORT CANNOT BE OPENED, BAIL OUT AFTER ERROR MSG.

  if(hComm == INVALID_HANDLE_VALUE)
    {
    Application->MessageBox("Communication Error, terminate Application", "Error", 16);
    SendMessage(GetActiveWindow (), WM_CLOSE, 0 ,0);
    }

  // SET THE COMM TIMEOUTS.

  GetCommTimeouts(hComm,&ctmoOld);
      ctmoNew.ReadTotalTimeoutConstant = 100;
  ctmoNew.ReadTotalTimeoutMultiplier = 0;
  ctmoNew.WriteTotalTimeoutMultiplier = 0;
  ctmoNew.WriteTotalTimeoutConstant = 0;
  SetCommTimeouts(hComm, &ctmoNew);

  // SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
  // THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST.
  // IF YOU WANT TO LATER ADD CODE FOR OTHER BAUD RATES, REMEMBER
  // THAT THE ARGUMENT FOR BuildCommDCB MUST BE A POINTER TO A STRING.
  // ALSO NOTE THAT BuildCommDCB() DEFAULTS TO NO HANDSHAKING.

  dcbCommPort.DCBlength = sizeof(DCB);
  GetCommState(hComm, &dcbCommPort);
  BuildCommDCB("9600,N,8,1", &dcbCommPort);
  SetCommState(hComm, &dcbCommPort);

  return Error;
}
//---------------------------------------------------------------------------

void A (void)
{
  OpenComm();
  ShowMessage("send test msg if here");
  while(1)
    {
    TransmitCommChar(hComm, 'f');
    }
}
//---------------------------------------------------------------------------

void B (void)
{

}
//---------------------------------------------------------------------------

void C (void)
{

}
//---------------------------------------------------------------------------

void Config (void)
{

}
//---------------------------------------------------------------------------
0
 
LVL 1

Author Comment

by:RideOn
Comment Utility
hoops, there is no return value at the moment from function OpenComm()
0
 
LVL 2

Accepted Solution

by:
GloriousRain earned 20 total points
Comment Utility
if you don't like to return code for terminate application. just call exit(0);
 if(hComm == INVALID_HANDLE_VALUE)
   {
   Application->MessageBox("Communication Error, terminate Application", "Error", 16);
exit(0); //normal exit
   }
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now