NO

Author Topic: Should I give up? Run win32 app from c .exe w/o Console Window Flash ??  (Read 10334 times)

EdPellesC99

  • Guest
  Hi,

  I like to script using wscripthost and work with html applications. For computers without access to IE, I must stay away from making windows using .hta files.
 
  The drab dialog boxes available to me using wscript.exe, drive me nuts. I have learned enough in C to make some nice dialog box windows with buttons. I was happy.

  I want to be able to call these dialogbox.exe files either by double click or script. Buttons would give me choices as to which scripts to run. I can customize these dialog boxes, make them look nice putting my own icons in the title bar etc.

  However I think I have run into a brick wall.

  From a vbscript file, I can call another script be it a .cmd or others Without seeing the console window at all. I am able to execute the script in a minimized window. This looks good, and not weird with the console window jumping out at the user etc.


  So from my c file compiled .exe file I want to be able to get the user's choice, then start the script and end the dialog box.

  I have banged my head on the wall with the system() function, the Shellexecute, the ShellexecuteEx, and seeing the most potential ... with the CreateProcess function.

  I have spent over 20 hrs searching and trying code. It seems that the leads I find that indicate the forum discussion is about eliminating the console window, the answers seem to be all about running the app itself in a hidden or minimized mode (which is NOT what I care about !).

  Here is some code that runs notepad in a hidden process. It compiles in Pelles C without errors or warning. I have to compile first as a win32 Gui then recompile after setting the linker to subsystem console.

  After scanning my code, does anyone have any suggestions for me or should I just give up my dream? If it cannot be done, for security or other limitations, I will give up.  I just hate to give up, cause usually it is "my personal limitations" that obstruct me !!!!

  Example Code that works:

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

int main()
{
  STARTUPINFO StartupInfo;
   memset(&StartupInfo, 0, sizeof(StartupInfo));
// set the size of the structure

  StartupInfo.cb = sizeof(STARTUPINFO);
  // tell the application that we are setting the window display
  // information within this structure

  StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
  // set the window display to HIDE
  StartupInfo.wShowWindow = SW_HIDE;  

  PROCESS_INFORMATION pi;

    CreateProcess(NULL,
                   "notepad.exe",
                   NULL, NULL, 0, 0, NULL, NULL, &StartupInfo, &pi);
// // ~ Above works and notepad is a hidden process, but you see the console flash.
}

=======================================================================

  I have tried many variations, using "C:\\Windows\\System32\\cmd.exe" for the first parameter .... and many others.

  Would apppreciate all thoughts, leads, advice, and maybe sympathy.

  Tx, Ed
  


« Last Edit: February 02, 2010, 06:31:20 PM by EdPellesC99 »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Should I give up? Run win32 app from c .exe w/o Console Window Flash ??
« Reply #1 on: February 02, 2010, 07:54:43 PM »
Don't give up.

I use this code from GUI program.

Perhaps you want to call VBS-script like this:

StartPrg("wbscript.exe test.vbs");

Uncomment this if want to wait until child process exits.
Code: [Select]
/*
    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );
*/

Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

void StartPrg(char *szCmdLine)
{
STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi = { 0 };
char szBuff[260];

si.cb = sizeof(si);

// Start the child process.
if (!CreateProcess(NULL, // No module name (use command line)
szCmdLine, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
wsprintf(szBuff, "CreateProcess failed (%d).\n", GetLastError());
//MessageBox(0, szBuff, "StartPrg", MB_OK);
return;
}
/*
    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );
*/
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

}

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//StartPrg("NotePad.exe test.txt");
if (*lpCmdLine)
StartPrg(lpCmdLine);
return 0;
}
May the source be with you

EdPellesC99

  • Guest
Re: Should I give up? Run win32 app from c .exe w/o Console Window Flash ??
« Reply #2 on: February 02, 2010, 09:22:16 PM »
 ;D :D ;D :D ;D

   Wow !!!!! Timo that is Fantastic. I am in shock!!!. I will bet over two days I thought of and tried 300 different search phrases in google for what I was looking for.

Finally, no matter WHAT I searched on, I was seeing the same hits.

   Thank you Sooo much. Just Great, and incredibly useful to me.

   Really, this is spectacular, I have to pinch myself to see if it is still last night and I am still dreaming of figuring this out!!!


Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank youThank you Thank you Thank you Thank you.


   I will play with this for a couple days, and maybe leave a few comments ( or at least a few more appreciative words !).

After I had reached the end of my rope yesterday, I decided to place a post on the subject .... I was getting the impression that maybe no one using c cared to do what I was trying to do. The program author of SavageEd.exe told me this was a great forum to post in C.

  On a side note, do you know last night, my cat like yours in the pic stowed away in my truck, and I drove over twenty miles went in a couple stores and five hours later went to my truck and he was standing outside my truck ...... I do not even know where he stowed away ! He never made a sound.

  Ok I have to maybe get a beer to celebrate this, and know that in spirit I am buying you a beer or more too !!!!

  Very impressively solved, and I can't thank you enough, and REALLY encourages me to learn more about C and that darned win32 api !

  Best Regards Timo, Ed




« Last Edit: February 03, 2010, 07:36:52 PM by EdPellesC99 »

EdPellesC99

  • Guest
Re: Should I give up? Run win32 app from c .exe w/o Console Window Flash ??
« Reply #3 on: February 03, 2010, 07:35:12 PM »

   Hi Timo,
 
To solve all my run app problems, I need to be able to run a .cmd script without a console window opening.

e.g.

File:  "RunNP.cmd" is in current folder.

Code:
echo off
cls
title=RunNP
notepad.exe
=====================================
In your c program, I have tried these variations:

[each with line: WaitForSingleObject( pi.hProcess, INFINITE ); commented out.]

StartPrg("cmd.exe /c RunNP.cmd");
Above opens console, window (Title: "RunNp"), and then notepad.exe opens with console staying open until notepad instance is closed.

StartPrg("RunNP.cmd");
// Above has the same results as prior.

Is there a way to Not see the console at all, only the notepad instance ?

Thanks,
   Ed

EdPellesC99

  • Guest
Re: Should I give up? Run win32 app from c .exe w/o Console Window Flash ??
« Reply #4 on: February 04, 2010, 03:14:20 AM »


   I have tried many variations on the next code line, it is the only line that comes close.

       StartPrg("cmd.exe /c start /min RunNP.cmd");
// Above gave me a console flash,  the notepad instance was visible. Alt-Tab showed me that the console window Title "RunNP" WAS mininimized.

    I can't tell if the window title of the console flash was "C:\Windows\System32\cmd.exe" or it was "RunNP", but I assume it was the first.

    I tried all switch variations I could find (/i and /b) to try with the "start" command without luck.


   I noticed that:

StartPrg("HiEd.vbs");  //Did nothing.

StartPrg("wscript.exe HiEd.vbs");  // Does work.

So I see that the action involved in the StartPrg function is not a shell execution based on file extent.

  I have tried to vary some of the parameters of the CreateProcess function without any luck.


  Tx, Ed



   

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: Should I give up? Run win32 app from c .exe w/o Console Window Flash ??
« Reply #5 on: February 04, 2010, 07:46:37 AM »
have you tried the function WinExec?

Code: [Select]
#include <Windows.h>

int main(int argc, char *argv[])
{
int i;
char cmd[256] = "";

for (i=1; i<argc; i++)
{
strcat(cmd, argv[i]);
strcat(cmd, " ");
}

WinExec(cmd, SW_HIDE);

return 0;
}
best regards
 Alex ;)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Should I give up? Run win32 app from c .exe w/o Console Window Flash ??
« Reply #6 on: February 04, 2010, 08:25:30 AM »
Or ShellExecute function.
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>

#pragma lib "shell32.lib"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
int rc;
char szTmp[100];

if ((rc = (int)ShellExecute(0, "open", "test.cmd", NULL, NULL, SW_HIDE)) > 32)
MessageBox(0, "Open", "ShellExecuteTest", MB_OK);
else {
wsprintf(szTmp, "Error: %d", rc);
MessageBox(0, szTmp, "ShellExecuteTest", MB_OK);
}

return 0;
}
May the source be with you

EdPellesC99

  • Guest
Re: Should I give up? Run win32 app from c .exe w/o Console Window Flash ??
« Reply #7 on: February 04, 2010, 06:04:40 PM »
Alex,
#include <Windows.h>

int main(int argc, char *argv[])
{
   int i;
   char cmd[256] = "";

   for (i=1; i<argc; i++)
   {
      strcat(cmd, argv);
      strcat(cmd, " ");
   }

   WinExec(cmd, SW_HIDE);

   return 0;
}

This is not working for me, and everything I have read says "do not use the WinExec" function as there is no guarantee it will be supported in the future.


Timo,

Your program brings up the Messagebox for me, then notepad opens with my file on top of the Messagebox. I never see the console flash. But I do not know how to modify your code to a use simple Shellexecute function (without If/Else Messageboxes) that will work like your code works (notepad up with file loaded and no console flash), I have tried.

RunNP.cmd contents:
title=RunNP
notepad.exe "SayHi.txt"

c file:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>

#pragma lib "shell32.lib"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
   int rc;
   char szTmp[100];

   if ((rc = (int)ShellExecute(0, "open", "RunNP.cmd", NULL, NULL, SW_HIDE)) > 32)
   
      MessageBox(0, "Open", "ShellExecuteTest", MB_OK);
   else {
      wsprintf(szTmp, "Error: %d", rc);
      MessageBox(0, szTmp, "ShellExecuteTest", MB_OK);
   }

   return 0;
}


==============================

Tx to both, Ed

nitex

  • Guest
I have written a similar tool for PHP some time back, which is basically a dynamic GUI generator for scripts. Maybe you can adjust it to your needs and rework it for VBScript? No need to code a new GUI for each script then. Basically a GUI defintion is stored inside a *.cfg file, which is just a renamed *.ini file with a few macro keywords for customizing the GUI. The tool and the sourcecode can be grabbed from SourceForge here if you like:

http://xrns-php.sourceforge.net/

EdPellesC99

  • Guest
Thanks for the reply,

   I downloaded and will look into it.

My solution of using a shellexecute function properly is working for me now.

... Ed