Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Application->Terminate Problem

Posted on 2001-07-09
16
Medium Priority
?
623 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
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 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses

885 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