Click here to Skip to main content
15,742,357 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Is it okay to cast from int to unsigned char *? Pin
ChemmieBro18-Mar-08 9:36
ChemmieBro18-Mar-08 9:36 
AnswerRe: Is it okay to cast from int to unsigned char *? Pin
David Crow18-Mar-08 9:33
David Crow18-Mar-08 9:33 
GeneralRe: Is it okay to cast from int to unsigned char *? Pin
Maxwell Chen18-Mar-08 9:36
Maxwell Chen18-Mar-08 9:36 
GeneralRe: Is it okay to cast from int to unsigned char *? Pin
ChemmieBro18-Mar-08 9:38
ChemmieBro18-Mar-08 9:38 
GeneralRe: Is it okay to cast from int to unsigned char *? Pin
Maxwell Chen18-Mar-08 9:49
Maxwell Chen18-Mar-08 9:49 
GeneralRe: Is it okay to cast from int to unsigned char *? Pin
Mark Salsbery18-Mar-08 10:21
Mark Salsbery18-Mar-08 10:21 
GeneralRe: Is it okay to cast from int to unsigned char *? Pin
ramana.g18-Mar-08 17:30
ramana.g18-Mar-08 17:30 
QuestionAutomation Server Questions Pin
garyflet18-Mar-08 8:53
garyflet18-Mar-08 8:53 
I'm using VC++ 6 service pack 6 on Windows 2000. I'm working with an automation server MDI application with the following requirements.

1) Can be started by user with visible interface.
2) Only one instance at a time.
3) If controlled by client, runs in background (invisible) initially.
4) Client(s) has option to make application visible.
5) If visible and user attempts to close with client(s) connections, warning is given with choice to close or not.
6) Has menu item that allows user to disconnect from all clients.
7) If invisible, closes when last client disconnects.

I have no problem with 1-4, the questions arise with 5-7. In order to implement these, the server needs to know if there are clients connected to it. My main automation class (CAutoApp) is instantiated every time a client contacts it. In the constructor I add to a static array of LPUNKOWNs (m_lpUnk[MAXNUMCONN]) using GetInterface(&IID_IUnknown):
LPUNKNOWN CAutoApp::m_lpUnk[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
CAutoApp::CAutoApp()
{
	EnableAutomation();
	for (int i=0; i < MAXNUMCONN && NULL != m_lpUnk[i]; i++);
	if (i < MAXNUMCONN)
		m_lpUnk[i] = GetInterface(&IID_IUnknown);
}

Whenever a client closes, CMainFrame::OnClose is called in the server. So I have the following code to implement 5 and 7 in OnClose():
//see if any clients are still connected
int i=0;
while(i < MAXNUMCONN && NULL != CAutoApp::m_lpUnk[i])
{
    try
    {
        CAutoApp::m_lpUnk[i]->AddRef();
        CAutoApp::m_lpUnk[i++]->Release();
    }
    catch(...)
    {
        //connection no longer exists, remove from array
        for(int j=i; j<MAXNUMCONN-1 && NULL != CAutoApp::m_lpUnk[j]; j++)
            CAutoApp::m_lpUnk[j] = CAutoApp::m_lpUnk[j+1];
    }
}

//if connected to at least one client
if (NULL != CAutoApp::m_lpUnk[0])
{
    if (IsWindowVisible())
    {
        if (IDNO == AfxMessageBox("One or more remote connections is still active. Exit anyway?", MB_YESNO))
            //user decides not to close application
            return;
    }
    else
        //a client disconnected but do not close because other client(s) remain
        return;
}
            .....
        CMDIFrameWnd::OnClose();

This works, although maybe it's a no-no to use a try-catch this way. If there's a better way to do it, I'd like to know.

To implement (6), I have the following:
void CMainFrame::OnCloseconnections() 
{
	if (IDNO == AfxMessageBox("This will close all connections. Are you sure?", MB_YESNO))
		return;

	//close all connections
	for (int i=0; NULL != CAutoApp::m_lpUnk[i]; i++)
	{
		try
		{
			while(CAutoApp::m_lpUnk[i]->Release());
		}
		catch(...)
		{
		}
		CAutoApp::m_lpUnk[i] = 0;
	}
}

This works, however when a user attempts to close my application, it crashes somewhere in the OLE dlls. There must be a better way to do what I'm trying to do?

Thanks,
Gary
QuestionHow to read Swedish word correctly Pin
njhy18-Mar-08 7:54
njhy18-Mar-08 7:54 
QuestionRe: How to read Swedish word correctly Pin
Maxwell Chen18-Mar-08 8:37
Maxwell Chen18-Mar-08 8:37 
GeneralRe: How to read Swedish word correctly Pin
njhy18-Mar-08 8:42
njhy18-Mar-08 8:42 
GeneralRe: How to read Swedish word correctly Pin
Mark Salsbery18-Mar-08 9:07
Mark Salsbery18-Mar-08 9:07 
GeneralRe: How to read Swedish word correctly Pin
Randor 18-Mar-08 10:52
professional Randor 18-Mar-08 10:52 
GeneralRe: How to read Swedish word correctly Pin
Mark Salsbery18-Mar-08 11:23
Mark Salsbery18-Mar-08 11:23 
GeneralRe: How to read Swedish word correctly Pin
Randor 18-Mar-08 12:57
professional Randor 18-Mar-08 12:57 
GeneralRe: How to read Swedish word correctly Pin
Mark Salsbery18-Mar-08 12:58
Mark Salsbery18-Mar-08 12:58 
GeneralRe: How to read Swedish word correctly Pin
Moak19-Mar-08 10:45
Moak19-Mar-08 10:45 
Questioncan any friend send me source code for developing notepad appln using VC++ 6.0 Pin
raveen18-Mar-08 7:28
raveen18-Mar-08 7:28 
AnswerRe: can any friend send me source code for developing notepad appln using VC++ 6.0 Pin
Maximilien18-Mar-08 7:33
Maximilien18-Mar-08 7:33 
GeneralRe: can any friend send me source code for developing notepad appln using VC++ 6.0 Pin
ThatsAlok18-Mar-08 20:24
ThatsAlok18-Mar-08 20:24 
AnswerRe: can any friend send me source code for developing notepad appln using VC++ 6.0 Pin
Mark Salsbery18-Mar-08 8:28
Mark Salsbery18-Mar-08 8:28 
GeneralRe: can any friend send me source code for developing notepad appln using VC++ 6.0 Pin
JudyL_MD18-Mar-08 8:54
JudyL_MD18-Mar-08 8:54 
GeneralRe: can any friend send me source code for developing notepad appln using VC++ 6.0 Pin
Mark Salsbery18-Mar-08 8:57
Mark Salsbery18-Mar-08 8:57 
AnswerRe: can any friend send me source code for developing notepad appln using VC++ 6.0 Pin
Joe Woodbury18-Mar-08 16:00
professionalJoe Woodbury18-Mar-08 16:00 
Questionmy mfc application works on windows xp sp2 but fails on vista business Pin
sthotakura18-Mar-08 6:34
sthotakura18-Mar-08 6:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.