|
I'm coding a set a functions to help me debugging by printing messages. I currently have 2 options; either on the console or in a file. That's enough for what I need but I'm just curious to know if it would be possible to do the same with a message box. That's what I have so far.
<br />
enum modes {<br />
CONSOLE, <br />
FILE,<br />
MSG_BOX,<br />
};<br />
<br />
class ErrorLogger {<br />
std::ostream *stream;<br />
std::ofstream *fileOut;<br />
public:<br />
ErrorLogger(modes mode) {<br />
if (mode == CONSOLE)<br />
stream = new std::ostream(std::cout.rdbuf());<br />
else if (mode == FILE) {<br />
fileOut = new std::ofstream("message.log");<br />
stream = new std::ostream(fileOut->rdbuf());<br />
}<br />
else if (mode == MSG_BOX) {<br />
stream = new std::ostream()<br />
}<br />
}<br />
std::ostream &GetStream() { return (*stream); }<br />
};<br />
<br />
template<class T> <br />
void Print(ErrorLogger &logger, T _data) {<br />
logger.GetStream() << _data;<br />
}<br />
Thank you.
-edit-
I need to do something else than code tags for correct formating?
|
|
|
|
|
try swprintf
You will get a string with your message in it. Then, just use Windows API AfxMessageBox to display the string on the Message Box
"Aerodynamically, the bumble bee shouldn't be able to fly, but the
bumble bee doesn't know it so it goes on flying anyway." - Mary Kay Ash
My Articles
|
|
|
|
|
i tried to get filename by a filehandle that was obtained by CreatFile func.
but after checking all MSDN reference,i found there is no API func can implement it directly.
the only way to do is that use the CreateFileMapping and MapViewOfFile functions to create the mapping and then use the GetMappedFileName function to obtain the file name;
such as the following code(copied from MSDN):
BOOL GetFileNameFromHandle(HANDLE hFile)
{
BOOL bSuccess = FALSE;
TCHAR pszFilename[MAX_PATH+1];
HANDLE hFileMap;
// Get the file size.
DWORD dwFileSizeHi = 0;
DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);
if( dwFileSizeLo == 0 && dwFileSizeHi == 0 )
{
printf("Cannot map a file with a length of zero.\n");
return FALSE;
}
// Create a file mapping object.
hFileMap = CreateFileMapping(hFile,
NULL,
PAGE_READONLY,
0,
1,
NULL);
if (hFileMap)
{
// Create a file mapping to get the file name.
void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);
if (pMem)
{
if (GetMappedFileName (GetCurrentProcess(),
pMem,
pszFilename,
MAX_PATH))
{
// Translate path with device name to drive letters.
TCHAR szTemp[BUFSIZE];
szTemp[0] = '\0';
if (GetLogicalDriveStrings(BUFSIZE-1, szTemp))
{
TCHAR szName[MAX_PATH];
TCHAR szDrive[3] = TEXT(" :");
BOOL bFound = FALSE;
TCHAR* p = szTemp;
do
{
// Copy the drive letter to the template string
*szDrive = *p;
// Look up each device name
if (QueryDosDevice(szDrive, szName, BUFSIZE))
{
UINT uNameLen = _tcslen(szName);
if (uNameLen < MAX_PATH)
{
bFound = _tcsnicmp(pszFilename, szName,
uNameLen) == 0;
if (bFound)
{
// Reconstruct pszFilename using szTemp
// Replace device path with DOS path
TCHAR szTempFile[MAX_PATH];
_stprintf(szTempFile,
TEXT("%s%s"),
szDrive,
pszFilename+uNameLen);
_tcsncpy(pszFilename, szTempFile, MAX_PATH);
}
}
}
// Go to the next NULL character.
while (*p++);
} while (!bFound && *p); // end of string
}
}
bSuccess = TRUE;
UnmapViewOfFile(pMem);
}
CloseHandle(hFileMap);
}
printf("File name is %s\n", pszFilename);
return(bSuccess);
}
i wanna know why MS not provide a func to get filename by file handle directly but to use MapView,I guess that must be with some reasons.
Thanks!;P
Stick to C++
|
|
|
|
|
Will this work?
CFile file (hFile);
CString strFilename = file.GetFilePath();
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
thank you for your reply.
But i mean that not use MFC,in fact i m not a VC user.;P
i wanna know why windows API not privide this function.
Stick to C++
|
|
|
|
|
|
Look around GetFileVersionInfo function. after chain of functions you should get the VS_FIXEDFILEINFO structure, who contain required data
|
|
|
|
|
|
I want to create two threads to perform two tasks. However, the two task has some common characteristic, so my two threads need to access the same thread function. Can I do that? Do I need to pay attention the synchronization issue? eg. variables within the thread function?
Thanks
|
|
|
|
|
I use a CCriticalSection to handle synchronization issues. You could declare a singleton instance of a CCriticalSection , and lock/unlock that before/after executing your function. Competing threads will be blocked until the function is available for use.
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
Accessing the same function from two threads is generally ok. You only have to take care about shared resources - so variables inside the thread functions (on the stack) are ok, because each thread have it's own stack. Problem can be with accessing global variables, instances that more than one thread can access etc. Here you should use some synchronization.
Critical section is best (IMHO) on windows system, if you do not need to share the synchronization object for interprocess communication (one critical section can be used only inside one process).
Generally it's best to use some sort of RAII for the synchronization, although it have also it's problems.
There are some articles about synchronization issues around on CP, so you can check for them. In case, don't worry to ask.
|
|
|
|
|
I have a small dialog meant to be an interactive ordered checklist with check boxes that check themselves as each of 4 steps are performed. It's called from my main app when a menu item is chosen. What's the best way to start the chain of execution? I currently am calling each function that does each step in a nested if statement in the constructor but that doesn't feel like the best way because these functions are the meat and potato's of the dialogs purpose, and they are definitely not initialization procedures. If there's a proper way, I don't have the particular way existing now so I'd have to override something.
|
|
|
|
|
Perform the tasks in a separate thread while the dialog is executing.
The thread can post messages to the dialog so the boxes get checked.
Make sure to catch if user tries to close dialog, cancel out thread and possibly undo your work. You can start thread at end of OnInitDialog() so all the dialog controls exist at that time.
|
|
|
|
|
I think I have an idea which is a combination of your solution and PJ Arjends'. I don't have an OnInitDialog function, I assume I need to override the CDialog function but I'm not sure how that's done, can you enligthen me? I'm relatively new to this stuff.
I do have a catch in my code:
CTransmitProgram transmitDlg(NULL, pPackage);<br />
if (transmitDlg.DoModal() != IDOK) {<br />
if (AfxMessageBox(IDS_UNKNOWNSTATE, MB_YESNO | MB_ICONQUESTION) == IDYES) { return TransmitProgram(); }<br />
return false;<br />
} else { return true; }
There's an OK and Cancel Button and OK button is disabled until all steps are completed. If anything happens except the OK button is pressed, it'll prompt the user and then try again, else OK was pressed meaning all steps were completed.
Thanks in advance, Nate.
|
|
|
|
|
It appears that you are using MFC.
Use the ClassWizard to add the WM_INITDIALOG, which will end up as OnInitDialog() member function, to the dialog's class for you automatically.
|
|
|
|
|
I did that, I created a static UINT thread function in the class and timed the it to do the check boxes and enable the OK button and it works pretty well. I'm working on the actual functionality so thanks for the advice, you too PJ Arjends.
|
|
|
|
|
If I understand your problem correctly what you want to do is have your dialog's WM_INITDIALOG handler post a user defined message to itself. In the message handler you initialize your chain of execution.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04
Within you lies the power for good - Use it! Honoured as one of The Most Helpful Members of 2004
|
|
|
|
|
hi,I'm using WinHTTP to connect a https server and I want to visit one web page of it.
However,error 12175 occurred.
I kown that the certificate sent by the server is not trusted by the client because the certificate is not issued by a trusted provider.
But how can i fix the problem in my program?
|
|
|
|
|
You have to obtain the certificate that is sent by your server and install it on the Trusted Providers. The following code will do the job:
bool InstallCertificationAuthority()<br />
{<br />
HCERTSTORE hSystemStore;<br />
hSystemStore = CertOpenSystemStore(NULL, _T("ROOT"));<br />
if (hSystemStore == NULL)<br />
return false;<br />
<br />
HRSRC hRSRC;<br />
hRSRC = FindResource(AfxGetResourceHandle()MAKEINTRESOURCE(IDR_CERTIFICATE), _T("CERTIFICATE"));<br />
if (hRSRC == NULL)<br />
return false;<br />
<br />
DWORD dwSize;<br />
dwSize = SizeofResource(AfxGetResourceHandle(), hRSRC);<br />
if (dwSize == 0)<br />
return false;<br />
<br />
HGLOBAL hCertificate;<br />
hCertificate = LoadResource(AfxGetResourceHandle(), hRSRC);<br />
if (hCertificate == NULL)<br />
return false;<br />
<br />
LPBYTE lpCertificate;<br />
lpCertificate = (LPBYTE) LockResource(hCertificate);<br />
if (lpCertificate == NULL) {<br />
FreeResource(hCertificate);<br />
return false;<br />
}<br />
<br />
if (!CertAddEncodedCertificateToStore(hSystemStore, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, lpCertificate, dwSize, CERT_STORE_ADD_USE_EXISTING, NULL)) {<br />
logRPCCore.Output("Cannot install certificate!\n");<br />
}<br />
<br />
UnlockResource(hCertificate);<br />
FreeResource(hCertificate);<br />
<br />
CertCloseStore(hSystemStore, 0);<br />
return true;<br />
}<br />
As you see, the code install the certification from a resource of your application into the ROOT Store.
|
|
|
|
|
Hi everybody,
Does anybody know how to catch Microsoft word application events in a non MFC application (specifically WTL application) ?
thanks in advance,
Roozbeh
|
|
|
|
|
Getting file information like author, subject i.e
|
|
|
|
|
How do you know when you are supposed to draw the focus on a control?
I'm referring to controls on dialog boxes.
At first when you click on a control with the mouse, the control doesn't draw the focus rectangle. It only draws it once you start navigating the dialog with the keyboard.
I'd like to be able to find out from within the control using its hWnd.
(I'm trying to skin Win98-style buttons in VC++ 6.0 to give them a WinXP look by overriding drawing messages [WM_PAINT, BM_SETSEL, etc.] in the button's windows-class wnd proc. I've got everything done so far, except this, and I've spent hours searching MSDN and the internet trying to find it.)
|
|
|
|
|
The DRAWITEMSTRUCT passed to OnDrawItem() contains this information. Specifically, the itemState member, whose ODS_FOCUS bit indicates the item has focus. You may also want to check the ODS_SELECTED bit to render the control appropriately. See this[^] MSDN article.
Alternatively, you could call GetFocus() to determine if the control has focus, although the preferred way is to check the DRAWITEMSTRUCT .
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
|
does anyone know of a function that i can pass 2 numbers to ie, 1 and 3, which would return a random float between the 2 parameters ie 2.3. perhaps a function that would return a random int would also work, i could just pass it 1000 and 3000 instead. any ideas? thanks.
|
|
|
|