Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

WinExec

Posted on 1998-03-03
7
Medium Priority
?
1,420 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 200 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

609 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