?
Solved

Application->Terminate Problem

Posted on 2001-07-09
16
Medium Priority
?
618 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 80 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

752 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