Solved

Application->Terminate Problem

Posted on 2001-07-09
16
614 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

717 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