Solved

WinExec

Posted on 1998-03-03
7
1,362 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
  • 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now