?
Solved

WinExec

Posted on 1998-03-03
7
Medium Priority
?
1,404 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
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.

 

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 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