|
This is code ripped from one of my apps. I think I got everything.
In the header file, I have this:
const int UWM_FOUNDHWNDADMIN = WM_APP + 1;
class CDlg : public CDialog
{
afx_msg LRESULT OnFoundHwnd(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()
private:
PROCESS_INFORMATION* m_pProcessInfo;
HWND m_hwnd;
void RunComponent();
void GetAppHWnd();
};
static BOOL CALLBACK EnumWindowsProcGetHwnd(HWND hwnd, LPARAM lParam);
In the CPP file, I have this:
void CDlg::RunD2RemoteAdmin()
{
CString sFileName = GetProgramPath();
AddBackSlash(sFileName);
sFileName += "MyProgram.exe";
if (FileExists(sFileName))
{
char cmdLine[1024];
strcpy(cmdLine,(const char*)sFileName);
m_pProcessInfo = new PROCESS_INFORMATION;
if (!m_pProcessInfo)
{
CString sMsg;
sMsg = "Could not allocate memory for component process info (not critical)";
AfxMessageBox(sMsg);
return;
}
STARTUPINFO StartupInfo = {0};
StartupInfo.cb = sizeof(STARTUPINFO);
if (::CreateProcess(NULL, cmdLine, NULL, NULL, FALSE,
0, NULL, NULL, &StartupInfo, m_pProcessInfo))
{
WaitForInputIdle(m_pProcessInfo->hProcess, 15000);
GetAppHWnd();
}
}
else
{
CString sMsg;
sMsg = "Could not find executable component";
AfxMessageBox(sMsg);
}
}
void CDlg::GetAppHWnd()
{
DWORD pid = m_pProcessInfo->dwProcessId;
HANDLE ps = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, pid);
EnumWindows(EnumWindowsProcGetHwnd, pid);
CloseHandle(ps);
}
LRESULT CDlg::OnFoundHwnd(WPARAM wParam, LPARAM lParam)
{
wParam;
lParam;
m_hwnd = (HWND)wParam;
DWORD dwPID = (DWORD)lParam;
if (dwPID == m_pProcessInfo->dwProcessId)
{
}
else
{
}
return 1L;
}
BOOL CALLBACK EnumWindowsProcGetHwnd(HWND hwnd, LPARAM lParam)
{
CString sTitle;
CWnd::FromHandle(hwnd)->GetWindowText(sTitle);
DWORD wndPid;
::GetWindowThreadProcessId(hwnd, &wndPid);
if (wndPid == (DWORD)lParam && !sTitle.IsEmpty())
{
if (sTitle.Find("Remote Admin Monitor") >= 0)
{
theApp.m_pMainWnd->SendMessage(UWM_FOUNDHWNDADMIN, (WPARAM)hwnd, lParam);
}
return FALSE;
}
else
{
return TRUE;
}
}
|
|
|
|
|
I'm using a CListCtrl to display logged data and it can come into the view
quickly. The scroll bar however will jump to the previously highlighted postision or to the 0th Sel each time new data comes in. I would like for the
scroll bar to default to the 0th position but remain where the user left it
if its position changed.
Thankz,
Tim
|
|
|
|
|
Firstly, thanks for all who gave me some ideas in the previous posting. I know it is because there is some problem in GDI resource or something.
Here is the problem...(say again...)
Everything fines in NT...but when i move to Win9x, strange things happened. After the program runs for awhile, fonts change, cannot display colour when i do drawing.....even the Windows system message box font change(i know it because later Windows force me to terminate the program....so u know.....liked some memory problem..)...but everything fines in NT
I didn't use CreateDC. I just use the OnDraw() function provided by MFC in View.cpp file to do drawing(mainly), and i use a GLOBAL CDC pointer to let other file to do drawing too:
CDCPointer->m_hDC = pDC->m_hDC;
CDCPointer->m_hAttribDC = pDC->m_hAttribDC;
And i delete the CDCPointer in the destructor in View.cpp (delete CDCPointer;)
So....i really need a solution...because I need to implement some graphic codes now.
Again...thanks for all giving me advice......thanks alot.!!!!
|
|
|
|
|
What you're describing seems like major GDI resource leak. I'm not sure it's related to your global device context variable. You should get rid of the global variable - just pass CDC pointer as function parameter.
Tomasz Sowinski -- http://www.shooltz.com.pl
|
|
|
|
|
Hello:
I built a map<...> in the way showed below. After running program, I do not know if
data was loaded correctly. To verify this, I wanted to use "cout", but I do
not know how to reference, for example, to the content of dMonto[3] that is in
mpCont.
Is there a charitable codeguru soul by there?
Thanks in advance!!!!
#include <iostream>
#include <string>
#include <utility>
#include
using namespace std;
const int tam=10;
struct stTot {
string stLey;
double dMonto[tam];
};
stTot myTot;
map<string, sttot=""> mpCont;
int main()
{
myTot.stLey = "Text one";
for(int i=0; i<tam; i++)
="" mytot.dmonto[i]="i+11;
" mpcont["one"]="myTot;
" map<string,sttot="">::iterator it = mpCont.find("one");
// I want to put the "cout" here
return 0;
}
|
|
|
|
|
The folowing should do the trick;
cout << (*yourIterator).second << endl;
-Ben
On the topic of code with no error handling -- It's not poor coding, it's "optimistic"
|
|
|
|
|
So far I've seen two examples using the syntax:
(*iter).second
to look at the content of a map. Is there something
wrong with using the -> operator as in:
iter->second
Thanx
|
|
|
|
|
My STL book ("The C++ Standard Library: A Tutorial and Reference" by Nicolai Josuttis) tells that:In some older environments, operator -> might not work yet for iterators MSVC's STL implementation seems to be an "older environment", using (*iter).member is always safe.
|
|
|
|
|
I have a custom database file (a simple character-delimited text file, if you would like to know) I need to read/write to in a Wizard-made Database program in MSVC++. The project was specified as a single-document database program. I'm having trouble figuring out how to read and write to the file. I assigned a CArchive Serialization function to a button, and when I hit it, I get an Invalid Page Fault. If you feel like laughing, here's the function:
void CDCDBIIView::OnButtonReadFile(CArchive &ar)
{
ar.ReadString(m_FILE); //CArchive object ar created in CDCDBIIDoc::Serialize(CArchive &ar)
UpdateData (FALSE);
}
Something's wrong with my logic in the ar.ReadString line, because that's the only line in the function doing any serious work. (UpdateData just...well, updates the data with the new info.) Right now, I'm not worried about parsing the input I'm getting in the archive, I just have m_FILE assigned to a read-only edit box so I can see if the program is reading from the file at all.
What I need to know is do I need to add a custom function to load/read/write to the open file, or do I add it to one of the many classes the MSVC++ Wizard built? I've read as many tuts as possible on CFile and CArchive, and MSDN isn't helping much. Any help would be greatly appreciated.
-Trying to be as informative as possible while knowing almost nothing at all
|
|
|
|
|
I have done a simial thing. The only difference is that i have used a structured ascii file instead of comma delim. This function uses CFile.
CFile rfile.Open(filename,CFile::modeRead | CFile::shareDenyWrite, &ex);
LogCnt = rfile.GetLength()/sizeof(LOGDATA);//returns how many records in the file
for (int i = 0;i< LogCnt;i++){
rfile.Read(&logdata[i],sizeof(LOGDATA));
///////if you want to use comma delim you would parse your data here
///////////////////////I put a return char on the end if the string in the write function.
rfile.Read(tmp,1);/////////////////used to hold return char
}
|
|
|
|
|
Hi all.
I want to know, for getting data from mscomm i must use of GetInput() or GetOutput() ???
My month article: Game programming by DirectX by Lan Mader.
Please visit in: www.geocities.com/hadi_rezaie/index.html
Hadi Rezaie
|
|
|
|
|
Does anyone know how to load small (16x16) versions of the built-in icons, like the information ('i' in a bubble), warning ('!' in a triangle) and error ('X' in a circle) icons?
LoadIcon will only load 32x32 images.
LoadImage loads the icons in question as 32x32 even if I request 16x16 and doesn't load them at all if I don't specify LR_SHARED.
I'm trying to avoid putting the icons in my project as a resource. I'd also rather not load the 32x32 versions and scale them myself. There's gotta be a way, Shirley?
|
|
|
|
|
Try,
HICON hSmallIcon = (HICON) ::LoadImage ( AfxGetInstanceHandle (), MAKEINTRESOURCE(IDR_MY_SMALL_ICON), IMAGE_ICON, 16, 16, LR_VGACOLOR );
it always works for me. But, remember that unlike LoadIcon, this one does require a subsequent call to DestroyIcon when your're done with the it.
-Ben
On the topic of code with no error handling -- It's not poor coding, it's "optimistic"
|
|
|
|
|
Thanks for the reply, but I want to load built-in Windows icons and avoid including resources in my project. (Because it's a class I plan on using in several projects.)
For example:
HICON hIcon = (HICON)LoadImage(NULL, IDI_INFORMATION, IMAGE_ICON, 16, 16, LR_SHARED);
This always loads a 32x32 image and fails if LR_SHARED is removed.
BTW, IDI_INFORMATION is the same as MAKEINTRESOURCE(OIC_INFORMATION) but you don't need to #define OEMRESOURCE and can leave out the MAKEINTRESOURCE macro.
|
|
|
|
|
BTW, I know I can load the 32x32 icon and paint it as 16x16 using DrawIconEx but that does an absolutely appalling job of scaling the image. I've seen the OS use 16x16 versions of them that look good, for example in Win2k tray icon bubbles, but I can't work out how to load them...
Maybe I need to load them directly from the DLL which contains them... Gah, nasty and not guaranteed to work in the future, but luckily the code will only be run on NT4 machines so it could be a safe bet.
(The code is to emulate Win2k tray icon bubbles when running on NT4 machines.)
|
|
|
|
|
I'd say, save yourself some greaf and just copy them into your applications resources. This will not only work, but be will garonted to work in the future.
BTW: I found this in MSDN
In LoadImage:
When loading a system icon or cursor, you must use LR_SHARED or the function will fail to load the resource.
Windows 95/98/Me: The function finds the first image with the requested resource name in the cache, regardless of the size requested.
The second part does not seem to apply to you, but it seems you must use LR_SHARED on all Windows flavors.
-Ben
On the topic of code with no error handling -- It's not poor coding, it's "optimistic"
|
|
|
|
|
Hi all - I hope you can help
I have a multi frame app eg like an MDI but each document is in it own mainframe - however whenever I try printpreview it always uses the first main frame and not the one I am trying to print preview - can anyone help me on this?
Holy Handgrenade of Antioch instructions
|
|
|
|
|
Everything I seem to see on this site is C++ and not C. Can someone please give me a hint as how to implement a static splitter window using just window API instead of MFC.
Thank you in advance,
SAK
|
|
|
|
|
You can create splitter in C. Below I'm assuming that you want simple two-way vertical splitter, you can generalize this solution to horizontals and combined splitters.
1) Registering your main window class use your own cursor looking more or less like this: <-|->
2) create your child windows in WM_CREATE handler. Use WS_EX_CLIENTEDGE style for 3D look.
3) in WM_SIZE handler ensure that height of these windows is equal to the height of main window's client area. Set that widths are set to the value that leaves some small area between two child windows. This area is your splitter bar.
3) your main window will receive mouse-related messages only if cursor is placed in the 'splitter' - the area between child windows. In WM_LBUTTONDOWN handler capture the mouse, resize child windows in subsequent WM_MOUSEMOVEs, and release mouse in WM_LBUTTONUP.
Tomasz Sowinski -- http://www.shooltz.com.pl
|
|
|
|
|
I have a group of about 30 checkboxes.
I would like to put them into an array so i can easily get and set the values.
What process is required to do this?
Thank You
|
|
|
|
|
Save in the array a reference of each checkbox.
good Luck
Carlos Antollini.
|
|
|
|
|
Previously I asked if there was a way to separate the colors for stdout & stderr when writing to a console. I never got a response. So in the unlikely event someone else was wondering about this, check it:
You need these headers:
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
When you fire up a console (using AllocConsole(), and when you're done call FreeConsole()), get the stdout & stderr handles by doing:
HANDLE hStdOutput;
HANDLE hStdError;
Then you can use the following routine just like fprintf. Now, if your routine is doing a ton of processing and suddenly an error pops up, your machine hangs, then reboots itself, you'll have a shot at seeing what went wrong! (especially when the reboot tends to wipe your logs)
//only goes to stdout or stderr!!!
int myfprintf(FILE *stream, const char *format, ...)
{
DWORD numwritten;
char *str;
va_list args; //variable argument list
va_start(args, format);
str = new char[strlen(format)*10 + 1]; // <- you're screwed if this isn't enough mem
//sprintf(str, "%s", format);
memset(str, '\0', sizeof(char));
vsprintf(str, format, args);
if (stream == stdout)
{
SetConsoleTextAttribute (hStdOutput, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
WriteConsole (hStdOutput, str, strlen (str), &numwritten, NULL);
}
else if (stream == stderr)
{
SetConsoleTextAttribute (hStdError, FOREGROUND_GREEN);
WriteConsole (hStdError, str, strlen (str), &numwritten, NULL);
}
delete[] str;
return (numwritten);
}
I've only tried this on NT Workstation 4 & Win98se, lemme know what you think...
--
Peace,
Amit Jain
|
|
|
|
|
This raises a subtle point. I am guilty of not responding to your original post. The reason? I'd answered the same question twice already. Sigh...
This is not to say that you should have read every post to see if there was an answer. (btw lets have a rockin party when the VC forum hits 10000), but you can see where it gets tiresome. I'm not suggesting a search is the best way either - sometimes its difficult to know what to key on (if you knew what you were looking for... )
So - my point? Well, the thought of a FAQ does arise... Is it feasable? How much work would it be to take the wealth of stuff we've got in the forums and make a FAQ? Chris? Anyone?
One thing I have noticed is that people do check the articles - once a relevant article exists, the forum querys on that topic abate.
Anyway, I'm think I'm posting a lounge article in the wrong place here.
BTW you addressed the issue better than I did (hey, they're not all gems! )
Cheers
T
|
|
|
|
|
An open moderated FAQ would probably help... Like interesting questions and good answers, modded by either the community or the mods. You're absolutely right though, I hate searching for the answer to a specific programming question, it's hard to narrow it down properly, and usually my terminology is wrong.
--
Peace,
Amit Jain
|
|
|
|
|
Hi,,
each class can serialize its own data memebers, is that right?
ex:
Class myclass1
{
int x;
}
class myclass2
{
myclass1 item;
}
I like to know if myclass2 can serialize the data member (int x) of myclass1 ??
Thanks
Ehsan Behboudi
|
|
|
|
|