Click to See Complete Forum and Search --> : Namespace issues


Archimedes
April 26th, 2005, 03:41 PM
This question takes many forms. The problem I am having is that I am creating a windows service. In it I want to pop up a dialog box, so I need to use the MessageBox::Show method from System::Windows::Forms. This works fine. However, if I want to use a function like GetModuleFileName, then I have to include windows.h. This immediately breaks the compiler, which tells me either a) MessageBoxW is not a class or namespace, or b) if I use full naming such as System::Windows::Forms::MessageBox::Show() that MessageBoxW is not a member of System::Windows::Forms. What is the conflict? Am I misunderstanding the use of namespaces, etc?

#include "stdafx.h"
#include "file.h"

#include <fstream>
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;
using namespace System::Windows::Forms;

void OpenFiles()
{
ofstream Output;
ifstream Input;

char szAppPath[MAX_PATH] = "";
string strAppDirectory;
::GetModuleFileNameA(0, szAppPath, sizeof(szAppPath) - 1);
// Extract directory
strAppDirectory = szAppPath;
strAppDirectory = strAppDirectory.substr(0, strAppDirectory.rfind("\\"));

...
MessageBox::Show("Output open failed!!", "Start", MessageBoxButtons::OK, MessageBoxIcon::Asterisk,
MessageBoxDefaultButton::Button1, MessageBoxOptions::ServiceNotification);
...
}

JimG
April 26th, 2005, 05:20 PM
// windows.h #defines MessageBox which overrides the
// System::Windows::Forms version.
#ifdef MessageBox
#undef MessageBox
#endif

RickCrone
April 27th, 2005, 10:54 AM
This has nothing to do with your question; but:

A Windows service should not be showing a message box. Services are backgroung processes that may be running with NO user logged on. So there is no one to see or respond to the message box.

Sure things go wrong, like your not being able to open a file in your example. You can write to the event log, maybe send an email, or have some application monitoring something that your service is doing to deal with these issues.

You may have a special case and need to break this 'rule'.
I just thought you might want to rethink what you are trying to do here.

Archimedes
April 27th, 2005, 11:21 AM
thanks, i was kind of hoping it was something simple. :rolleyes:

You may have a special case and need to break this 'rule'.

Yeah, this is really more for debugging purposes, since it is kind of a pain to debug services. I was really more concerned about the problem itself, and the fact I may be doing something weird. :wave:

RickCrone
April 27th, 2005, 11:33 AM
Oh... yea debugging services: One thing I do is create log files for my services. This helps debug them and trouble shoot after they go live. My log files are just simple text files and I write to them at any interesting step in my process. It's kind of a simple trace that includes varibable values of interest at that point.

As far a monitoring. I am updating SQL tables so I have a ImAlive table and update that regularly... my monitoring applicaton just looks to the ImAlive table to see if my services have updated thier record in the last 60 seconds... if not it's dead or hung up and needs attention.