Solved

WinExec

Posted on 1998-03-03
7
1,384 Views
Last Modified: 2013-12-03
What am I doing wrong?  I want to run a Windows program from a windows program.  When ever I run the following code I get an error 2 (file not found).

#include <windowsx.h>
#include <windows.h>
#include <winbase.h>
#include <stdio.h>

#pragma argsused


/**************************************************************/
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
           LPSTR lpCmdLine, int nCmdShow)
{
WORD wReturn;
char szMsg[80];

wReturn = WinExec("DRAW", SW_SHOWNORMAL );

if (wReturn < 32) {
    sprintf(szMsg, "WinExec failed; error code = %d", wReturn);
    MessageBox(NULL, szMsg, "Error", MB_ICONSTOP);
}
else {
    sprintf(szMsg, "WinExec returned %d", wReturn);
    MessageBox(NULL, szMsg, "", MB_OK);

}


This is the example out of the help file, yet I can't get it to do any thing other than return a "file not found" error.

I have also tried LoadModule() with no sucess.

Thanks for your help.

AJY
0
Comment
Question by:youngs101497
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 4

Accepted Solution

by:
tflai earned 50 total points
ID: 1411677
It looks to me that your application and the EXE that you want to launch are not in the same directory.
0
 
LVL 4

Expert Comment

by:tflai
ID: 1411678
Use GetModuleFileName(NULL, szCurrentPath, sizeof(szCurrentPath)) to get your application's work directory, then append that with "DRAW".
(That's if you want the application and "DRAW" in the same directory.)
0
 
LVL 4

Expert Comment

by:tflai
ID: 1411679
Oops!  The "proposed answer" should read:
"It looks to me that your application's current work directory is not the same as the directory that DRAW is in."
Or, if you know where DRAW is always:
WinExec("c:\drawpath\DRAW", SW_SHOWNORMAL);
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:youngs101497
ID: 1411680
WinExec is described to use a search pattern which in the case of draw, does include thw Windows directory.  I have used absolute paths and relitive paths with no success.  The problem does not seem to be in the path paramater.

AJY
0
 
LVL 4

Expert Comment

by:tflai
ID: 1411681
How'd about SetCurrentDirectory() to the path that DRAW is in?
0
 

Author Comment

by:youngs101497
ID: 1411682
I found my problem.  But when I call the program the two run concurrently.  I need the calling program to wait until the called program concludes before the calling program continues.  Can I do this with WinExec?  If not what can I use.

AJY
0
 
LVL 4

Expert Comment

by:tflai
ID: 1411683
Launch DRAW with CreateProcess() or ShellExecuteEx(), then wait on it to finish with WaitForSingleObject().
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question