|
Casting hProcess to an HWND will certainly not do what you want.
You may need to EnumWindows and examine each one somehow to see if it's the one you want. I can't think of a way to get a process handle or a process id from a window handle but there must be a way. Then for each window handle EnumWindows gives you you could just see if it belongs to the process you just started. Simple!
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).
|
|
|
|
|
GetWindowThreadProcessId will give you the process ID/thread ID of the process/thread that created the window handle.
Chris Richardson
|
|
|
|
|
Chris,
Well, I tried enumerating all the windows and using GetWindowThreadProcessId() and it doesn't seem to give me the same process id as obtained from the SHELLEXECUTEINFO structure (updated by ShellExecuteEx()):
The enumeration callback:
class WININFO
{
public:
CWnd *hWnd;
HINSTANCE hInst;
CString title;
WININFO(CWnd *wnd = NULL, HINSTANCE inst = NULL) {hWnd = wnd; hInst = inst; title = "";}
};
typedef CArray<WININFO,WININFO> CArrayWinInfo;
BOOL CALLBACK CIOGuidesView::enumwndfn(HWND hWnd, LPARAM lParam)
{
CIOGuidesView *me = (CIOGuidesView *)lParam;
return me->enumwndfn(CWnd::FromHandle(hWnd));
}
BOOL CIOGuidesView::enumwndfn(CWnd * wnd)
{
WININFO wininfo(wnd);
wnd->GetWindowText(wininfo.title);
DWORD ProcessId;
GetWindowThreadProcessId (wnd->GetSafeHwnd(), &ProcessId );
wininfo.hInst = (HINSTANCE)ProcessId;
m_WinInfoArray.Add(wininfo);
return TRUE;
}
The ShellExecuteEx() call:
void CIOGuidesView::OnButton1()
{
CString filename("PDF Files\\751018a.pdf");
HANDLE hProcess = NULL;
SHELLEXECUTEINFO shellInfo;
::ZeroMemory(&shellInfo, sizeof(shellInfo));
shellInfo.cbSize = sizeof(shellInfo);
shellInfo.lpVerb = "open";
shellInfo.lpFile = filename;
shellInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
if(::ShellExecuteEx(&shellInfo))
{
hProcess = shellInfo.hProcess;
}
else
{
ShowLastError("ShellExecuteEx Failed");
return;
}
int reason = WaitForInputIdle(hProcess,INFINITE);
And then I dumped all the windows whose title began with "Acro" OR those whose hInst matched my hProcess:
hProcess=0x01D8 hInst=0x045C title='Acrobat Reader'
If I understand this, the hProcess and hInst values should have been identical.
Ideas?
|
|
|
|
|
The HINSTANCE is not the same as the ID of the process. What you need to do, is use the ID of the process to get a handle to the process, which can be compared directly to the hProcess member of SHELLEXECUTEEX. Something like this:
BOOL CIOGuidesView::enumwndfn(CWnd * wnd)
{
WININFO wininfo(wnd);
wnd->GetWindowText(wininfo.title);
DWORD ProcessId;
GetWindowThreadProcessId (wnd->GetSafeHwnd(), &ProcessId );
HANDLE a_hProcess = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, ProcessID );
wininfo.hInst = (HINSTANCE)a_hProcess;
m_WinInfoArray.Add(wininfo);
return TRUE;
}
Chris Richardson
|
|
|
|
|
Chris Richardson wrote:
Why not close it immediately? Once you got the handle all you need it its value to use for comparison, you don't need to do anything else with it.
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).
|
|
|
|
|
Whoops. I guess I had a momentary lapse of brain activity .
Chris Richardson
|
|
|
|
|
Chris,
I suspected I was mixing apples and oranges. But it still doesn't seem to work.
BOOL CIOGuidesView::enumwndfn(CWnd * wnd)
{
WININFO wininfo(wnd);
wnd->GetWindowText(wininfo.title);
DWORD ProcessId;
GetWindowThreadProcessId (wnd->GetSafeHwnd(), &ProcessId );
HANDLE a_hProcess = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, ProcessId );
wininfo.hInst = (HINSTANCE)a_hProcess;
CloseHandle(a_hProcess);
m_WinInfoArray.Add(wininfo);
return TRUE;
}
This still yields no exact matches on hProcess (from ShellExecuteEx) and a_hProcess (from OpenProcess).
I am beginning to wonder if AcroRd32.exe does something especially weird. I'll try something with another type of file...
I appreciate you looking at this. It has me bamboozled!
|
|
|
|
|
It seems that on my machine, OpenProcess is always returning the same value, no matter which ID you give it. That's pretty strange, but there's another way to do this. You could use CreateProcess, instead of ShellExecuteEx. CreateProcess will give you the ID of the process as well as it's handle, so you could directly compare the process ID gotten from GetWindowThreadProcessId with the process ID returned in the PROCESS_INFORMATION struct (from CreateProcess). I'll mess with this approach for a little while longer, and if I can get it to work, I'll let you know.
Chris Richardson
|
|
|
|
|
Chris,
I was just about to post a message here saying pretty much the same thing: OpenProcess() is giving the same value for different windows. I checked that they were truly different processes via the Task Manager.
I was hoping to avoid CreateProcess() as I like the idea of ShellExecuteEx()'s looking up the default program based on the file type. So I just use an lpVerb of "open" and the lpFile points to the .pdf file and ShellExecuteEx() takes care of everything else.
I suppose I can write the code to search the registry and duplicate this but it doesn't sound fun.
I don't suppose CreateProcess() does this kind of thing, does it?
|
|
|
|
|
CreateProcess won't do it for you, but you can use a function called SHGetFileInfo to get the exe path for you. Take a look at the SHGFI_ICONLOCATION flag of that function. It will get you the path to the .exe file containing the icon for the passed in .pdf file. Almost assuredly Acrobat Reader stores it's icon inside it's exe file, but this could be verified. If this sounds a little hackish and risky, then the registry stuff isn't all that bad anyways.
Good luck with it,
Chris Richardson
|
|
|
|
|
You might find the FindExecutable function useful.
|
|
|
|
|
OK, thanks to all you folks giving me help, I think I have it.
I now use FindExecutable() to get the default program, then CreateProcess() to spawn it:
char *filename = "C:\\src\\IOGuides\\PDF Files\\751018a.pdf";
TCHAR szExe[MAX_PATH];
::FindExecutable(filename, _T(""), szExe);
HANDLE hProcess = NULL;
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInfo;
::ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
::ZeroMemory(&processInfo, sizeof(processInfo));
char cmd[MAX_PATH + MAX_PATH + 5];
sprintf(cmd,"\"%s\" \"%s\"", szExe, filename);
BOOL startedOK = CreateProcess(
NULL,
cmd,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&startupInfo,
&processInfo);
if(!startedOK)
{
ShowLastError(filename);
return;
}
hProcess = processInfo.hProcess;
WaitForInputIdle(hProcess,INFINITE);
I then get the process ID from processInfo.dwProcessId and enumerate all the windows, using GetWindowThreadProcessId() to get the associated process ID.
BOOL CIOGuidesView::enumwndfn(CWnd * wnd)
{
WININFO wininfo(wnd);
wnd->GetWindowText(wininfo.title);
DWORD ProcessId;
GetWindowThreadProcessId (wnd->GetSafeHwnd(), &ProcessId );
wininfo.Pid = ProcessId;
m_WinInfoArray.Add(wininfo);
return TRUE;
}
Comparing processInfo.dwProcessId against the value from GetWindowThreadProcessId() gives me the match-up I needed.
Unfortunately, AcroRd32.exe is cutesy and allows only one instance to run at a time. If I already have the Reader running, the new process just closes down. Presumably after talking to its counterpart and giving the new filename.
Sigh.
I guess I can kill the old one first, but that won't alway work if the user manually started up AcroRd32.
Thanks for all your help. I'll probably make a "beginner" level article showing all of this in the next few weeks since doing this is not so obvious. I suspect the trick in making the article will be coming up with keywords and phrases to allow people to find it when they are looking to do this kind of thing. All the information I needed was in the articles; I just couldn't find it.
Again, many thanks for all of your patience and help. Now my one-day project (now in its fourth day) can be completed.
|
|
|
|
|
I feel like I should know this question, but it seems to me to be harder than I want it too.
Preface: I am working on a windows GUI interface for a embedded system. My boss wants to save off the current state of the system in to a file. However he has laid some requirements on the file format 1) it should be hand editable, 2) other applications should be able to open it, and 3) we don't need to rely on the having an outside application. My boss understands enough about software to be annoying and he is insisting that the file save be ODBC, however I admit I am not familiar enough with it. I do realize that you need to register a datasource and I am not sure how to do this on a different machine. My predecessor wrote the GUI in VB and he saved the file in Access format which was not hand editable.
I have cruised the site here and found some nice demonstrations. I had orginally thought that I would save the file in an Excel format. I noticed it used ODBC and my boss would be pleased. However I began to be concerned that it might fail on machines that don't have excel. We cannot require installation of Excel on a machine to run our GUI.
I think automation solutions I found on Microsofts site will also have this problem. I am not sure how the orginal VB code got around this problem with the Access format file using type libraries. (Not real familiar with those either.)
Does anyone have any advice on what to do and where to find documentation on doing it? I mean I can use ODBC if I can find a way to automate the registration of the data source.. but I need to brush up on this stuff again..
Thanks for any help,
Brian
If you start a fire for a man, he will be warm for a day. If you start that same man on fire, he will be warm for the rest of his life.
|
|
|
|
|
I seem to recall seeing a recent article that addressed creating a .XLS file even if there wasn't an ODBC driver for Excel installed. However, all I can find was the following:
http://www.codeproject.com/database/excel_odbc_write.asp
Try that other site, codeguru.com, too.
|
|
|
|
|
Thanks for the suggestion, but I checked out the "other site" and the articles on Excel. They all require excel ODBC drivers to read and write files.
I need to learn how to automated create ODBC or DAO drivers. I just need a method of letting others use additional software to edit the file outside of the actual GUI program.
I will keep looking...
Brian
If you start a fire for a man, he will be warm for a day. If you start that same man on fire, he will be warm for the rest of his life.
|
|
|
|
|
It depends on how complicated the state information is. Simple options would be:
1. Use ini files (WriteProfileXxxx and GetProfileXxxx)
2. Write csv files. They can be opened in Notepad, Excel, Access etc., etc. Get some code from CP to handle csv files or write your own, it's pretty easy.
3. Use XML. There are several examples on CP of loading and saving configuration to XML. It's not so easy to hand edit (and fairly easy to screw it up) but it can be done.
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).
|
|
|
|
|
Phil J Pearson wrote:
. Use XML. There are several examples on CP of loading and saving configuration to XML. It's not so easy to hand edit (and fairly easy to screw it up) but it can be done.
you can use MC++ and DataSet
but then you need to use MC++
Rickard Andersson@Suza Computing
C# and C++ programmer from SWEDEN!
UIN: 50302279
E-Mail: nikado@pc.nu
Speciality: I love C#, ASP.NET and C++!
|
|
|
|
|
Requirement 3 would seem to limit you to flat ASCII files
that could be viewed/edited in Notepad or Wordpad.
You can then create an ODBC data source that will let you
manipulate the file via ODBC and satisfy that requirement.
Strange though. The requirements seem to have arisen from
different goals: hand editability and independence from
outside application vs. standardization of the interface
allowing substitution in the data source.
|
|
|
|
|
Tell me about it. My boss laid down the requirments at just the right level to be dangerous without exactly knowing what he wants. Now I am just researching what I can actually give and then counter proposal time...
If you start a fire for a man, he will be warm for a day. If you start that same man on fire, he will be warm for the rest of his life.
|
|
|
|
|
Just write to a plain ASCII text file.
It can be edited with notepad, wordpad, UltraEdit, ... you name it.
Then tell your boss that you use something like Online Dirty Binary Conversion (=ODBC) logic (or invent a nice acronym yourself).
Enjoy life, this is not a rehearsal !!!
|
|
|
|
|
I have a CListView where I want to set the top index explicitly.
How can I do that?
/Per
|
|
|
|
|
Here's one way to do it. I'm not sure why MS didn't include a SetTopIndex with the SysListView, but it seems silly...
void CYourListView::SetTopIndex( int p_iDesiredTopIndex )
{
CListCtrl & a_roList = GetListCtrl();
CRect a_oRect( 0, 0, 0, 0 );
a_roList.GetItemRect( 0, &a_oRect, LVIR_BOUNDS );
a_roList.Scroll( CSize( 0, p_iDesiredTopIndex * a_oRect.Height() ) );
}
Chris Richardson
|
|
|
|
|
Hello again,
I have a bunch of dialogs that I use as modals but I want to now bunch them together as child windows in one big dialog. In the process of doing this (without even getting to the make my dialog a child window yet), I am trying to alter the properties of my dialog at run-time. My problem is that when I change the properties by ModifyStyle() and ModifyStyleEx() it behaves (when I click the mouse etc..) as though the properties have changed but it still has the border and title etc. What am I doing wrong?
heres a snippet of the code thats causing the prob. I have tried all sorts of permutations and combinations of ModifyStyle(). It works if I change all the properties in the resource editor..
m_pProjInfoDlg = new CProjInfoDlg(this,m_pDoc);<br />
m_pProjInfoDlg->Create(IDD_PROJINFO,this);<br />
m_pProjInfoDlg->ModifyStyle(WS_POPUP|WS_CAPTION|WS_BORDER|WS_DLGFRAME|WS_THICKFRAME|WS_BORDER,0,SWP_NOACTIVATE);<br />
m_pProjInfoDlg->ShowWindow(SW_SHOW);
help!! Steadily going bananas.
thanks!
Adam.
www.beachwizard.com/travelogue[^]
"I spent a lot of my money on booze, birds and fast cars. The rest I just squandered"
George Best.
|
|
|
|
|
I´m trying to convert a float to a char with the following code:
double Total;
char buffer[50];
Total = GetDouble(dbf, 2) + GetDouble(dbf, 3) + GetDouble(dbf, 4);
_gcvt(Total,10,buffer);
But the result is allways the same, even if the value of Total equals cero.
Can some body help me.
|
|
|
|
|
I think you probably want to be using sprintf(). See MSDN.
Signature space for rent. Apply by email to....
|
|
|
|
|