Article View: borland.public.cppbuilder.nativeapi
Article #15711Re: Run program against file
From: Ed Mulroy [TeamB]
Date: Mon, 16 Jun 2003 21:06
Date: Mon, 16 Jun 2003 21:06
205 lines
7237 bytes
7237 bytes
====================================
Cut-and-paste error. If you saw my original reply
before I cancelled it, then ignore it and read this one
====================================
I copied that code to my machine, built it into a program and ran
it. It worked although the title of the file selection dialog was
"somefile.exe", a rather unusual title for such a thing.
If GetDesktopWindow is a problem for you then you could use
NULL for that calling parameter. However it should work as is.
Why do you think GetDesktopWindow is a problem? What
indications do you have of that?
However if that window handle is a problem, then make your
own window and use its handle. Code to do that is below.
When saved as q395d.cpp and built with the following command
line it created a 6,656 byte executable that did the job.
bcc32 -RT- -x- -WR q395d noeh32.lib
Or you could use a simpler command line such as the one below
which static links and does not stub out exception handling and
runtime type id to get a 51,712 byte version that does the same
thing
bcc32 -W q395d
Note that I have kept the changes you made, copying almost
exactly what you posted into the program.
---------------------------------
#include <windows.h>
WORD WINAPI SelectFile(
HWND parent,
const char *initial_directory,
const char *
title,
char *full_name)
{
OPENFILENAME ofn;
char file_name[MAX_PATH + 1];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = parent;
ofn.lpstrTitle = title;
ofn.nMaxFileTitle = lstrlen(title);
*full_name = '\0';
ofn.lpstrFile = full_name;
ofn.nMaxFile = MAX_PATH + 1;
ofn.lpstrFileTitle = file_name;
ofn.nMaxFileTitle = MAX_PATH + 1;
ofn.lpstrInitialDir = initial_directory;
ofn.lpstrFilter = "Executable Files\0*.EXE\0\0";
ofn.Flags = OFN_FILEMUSTEXIST | OFN_LONGNAMES |
OFN_NOREADONLYRETURN | OFN_HIDEREADONLY |
OFN_PATHMUSTEXIST | OFN_EXPLORER;
SetLastError(0);
if (!GetOpenFileName(&ofn))
{
DWORD errnum = CommDlgExtendedError();
char err_buf[512];
wsprintf(err_buf, "File Selection Error %08X/%u", errnum);
MessageBox(parent, err_buf, "", MB_OK);
*full_name = '\0';
*file_name = '\0';
ofn.nFileOffset = (WORD) -1;
}
else
{
// create a pointer to the 1st char following the directory
lstrcpy(file_name, full_name + ofn.nFileOffset);
}
return ofn.nFileOffset;
}
const char window_caption[] = "Main Window";
const UINT SELECT_FILE_MSG = WM_USER + 200;
LRESULT CALLBACK MainProc(HWND hwnd, UINT msge,
WPARAM wParam, LPARAM lParam)
{
switch (msge)
{
case SELECT_FILE_MSG :
{
char file_name[MAX_PATH + 1];
int result = SelectFile(hwnd, "C:\\",
"sumfile.exe", file_name);
if (result != -1)
{
MessageBox(HWND_DESKTOP, file_name,
"Selected File", MB_OK);
}
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
}
case WM_PAINT :
{
PAINTSTRUCT ps; // info structure used by BeginPaint
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
}
case WM_CLOSE :
{
DestroyWindow(hwnd); // when it's closed, we want
break; // to discard the window
}
case WM_DESTROY :
{
PostQuitMessage(0); // send message to main loop
return 0; // ordering the program to end
}
}
return DefWindowProc(hwnd, msge, wParam, lParam);
}
HWND Init() // register a window class and create a window
{
WNDCLASS wc; // holds data used to set up the window's class
HWND hwnd = NULL;
// program's instance handle/module handle
extern HINSTANCE _hInstance; // startup code created this variable
wc.lpfnWndProc = MainProc;
wc.hInstance = _hInstance;
// also using the window caption as the class name
wc.lpszClassName = window_caption;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbClsExtra = wc.cbWndExtra = 0;
if (RegisterClass(&wc))
{
// our window class is registered
// create a window of that class
hwnd = CreateWindow(window_caption, window_caption,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
HWND_DESKTOP, NULL, _hInstance, NULL);
}
return hwnd;
}
int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
MSG msg;
HWND hwnd = Init();
if (hwnd) // if window creation was successful
{
ShowWindow(hwnd, SW_SHOW); // show it
UpdateWindow(hwnd); // cause contents to be painted
// tell the window procedure to select a file
PostMessage(hwnd, SELECT_FILE_MSG, 0, 0);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg); // handle messages sent to
DispatchMessage(&msg); // program & its windows
}
}
return msg.message;
}
---------------------------------
. Ed
> cpp3003 wrote in message
> news:3eee1a55$1@newsgroups.borland.com...
>
> Yes, I have compiled other programs which do work.
> The coding below is what I am attempting to run. Have
> I misapplied this code in any way ? It compiles
> without error. GetDesktopWindow does not appear to
> be working, for some reason. I have no clue what to do
> for this.
Technical Details
Message-ID:
<3eee316c$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mailReferences:
<3ee8f082$1@newsgroups.borland.com> <3ee8fc5c@newsgroups.borland.com> <3ee9dde6$1@newsgroups.borland.com> <3eea79f9$1@newsgroups.borland.com> <3eedd330$1@newsgroups.borland.com> <3eedf7f3@newsgroups.borland.com> <3eee0645$1@newsgroups.borland.com> <3eee1411$1@newsgroups.borland.com> <3eee1a55$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,7bb2502e61022b33 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-06-16 14:12:10 PST From: "Ed Mulroy [TeamB]" <ed@mulroyREMOVETHESECHARS.org> Newsgroups: borland.public.cppbuilder.nativeapi References: <3ee8f082$1@newsgroups.borland.com> <3ee8fc5c@newsgroups.borland.com> <3ee9dde6$1@newsgroups.borland.com> <3eea79f9$1@newsgroups.borland.com> <3eedd330$1@newsgroups.borland.com> <3eedf7f3@newsgroups.borland.com> <3eee0645$1@newsgroups.borland.com> <3eee1411$1@newsgroups.borland.com> <3eee1a55$1@newsgroups.borland.com> Subject: Re: Run program against file Date: Mon, 16 Jun 2003 17:06:38 -0400 Lines: 207 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 24.74.175.102 Message-ID: <3eee316c$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1055797612 24.74.175.102 (16 Jun 2003 14:06:52 -0700) X-Authenticated-User: edmulroy Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail