Solved

Application->Terminate Problem

Posted on 2001-07-09
16
609 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
ID: 6268038
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
ID: 6268075
SendMessage(WM_CLOSE);
0
 
LVL 1

Author Comment

by:RideOn
ID: 6268131
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 30

Expert Comment

by:Axter
ID: 6268145
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
ID: 6268159
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
ID: 6268191
okay, then i first have to get the hwnd of my window...right?
0
 
LVL 2

Expert Comment

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

Author Comment

by:RideOn
ID: 6268220
okay, then i first have to get the hwnd of my window...right?
0
 
LVL 1

Author Comment

by:RideOn
ID: 6268234
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
ID: 6268254
Did you try using the functions I posted?
CloseHandle, TerminateThread, and TerminateProcess?
0
 
LVL 2

Expert Comment

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

Author Comment

by:RideOn
ID: 6268360
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
ID: 6268406
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
ID: 6268447
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
ID: 6268455
hoops, there is no return value at the moment from function OpenComm()
0
 
LVL 2

Accepted Solution

by:
GloriousRain earned 20 total points
ID: 6268600
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 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