Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest WME version: WME 1.9.1 (January 1st, 2010) - download

Pages: [1] 2  All

Author Topic: Executing files  (Read 9270 times)

0 Members and 1 Guest are viewing this topic.

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Executing files
« on: September 24, 2007, 10:11:43 PM »

Hi all,

I have the following problem. I need my game to execute an exe file in the beginning (CP) and then continue analyzing its return code.
I've call it through apifunction like this:

external "shell32.dll" int ShellExecuteA(int hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);

var a = ShellExecuteA(Game.Hwnd, "open", Filename, "", "", 0);
Game.Msg(a);

Although this seems to work, I need this window to be hidden and not throw the game to the desktop. I already tried putting 0 instead of Hwnd, but to no avail.
Any help please? :)
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

sychron

  • Wanderer zwischen den Welten
  • Global Moderator
  • Regular poster
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 223
  • There is no spoon. The enemy gate is down!
    • View Profile
Re: Executing files
« Reply #1 on: September 24, 2007, 10:45:11 PM »

I won't do something like this. It can cause nasty crashes when desktop firewalls pull your game out of graphics mode to show their "may this program execute another program" requesters.
Logged
... delete the inner sleep ...

odnorf

  • w00t?
  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 7
  • Offline Offline
  • Gender: Male
  • Posts: 1456
  • Lamp dog!
    • View Profile
Re: Executing files
« Reply #2 on: September 24, 2007, 10:53:37 PM »

It's a necessity for some copy protections that publishers *demand*. There are plenty of games that do that. Firewalls have to live with that ;) as it's a one time question. And wme doesn't usually crash if it looses focus.
Logged
fl*p

Daniel

  • Regular poster
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 124
  • I'm *not* a llama!
    • View Profile
Re: Executing files
« Reply #3 on: September 24, 2007, 11:40:30 PM »

What are you trying to launch? I'm asking because launching notepad or mspaint, for example, worked just fine for me. I tried the following:

var a = ShellExecuteA(Game.Hwnd, "open", "c:\windows\notepad.exe", "", "", 8 );

and:

var a = ShellExecuteA(Game.Hwnd, "open", "c:\windows\system32\mspaint.exe", "", "", 8 );

and both opened the respective application in the background while the game remained on screen.

Did I miss something?  ???

Btw, I used 8 (SW_SHOWNA) instead of 0 (SW_HIDE) as the nShowCmd parameter but I don't think that's what's causing your problem.
Logged

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Executing files
« Reply #4 on: September 25, 2007, 08:16:38 AM »

I'm launching copy protection exe file. I tried to call it with 8, but it's the same. Game is again minimized. Btw. it's on VISTA business edition.
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Executing files
« Reply #5 on: September 25, 2007, 08:51:06 AM »

But what kind of application is that? Console/GUI? Does it normally open some window?
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Executing files
« Reply #6 on: September 25, 2007, 08:54:37 AM »

It doesn't open any kind of window. It's console application which only returns a number.

Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Executing files
« Reply #7 on: September 25, 2007, 09:04:11 AM »

If it doesn't open a window, it shouldn't shift the focus. I'll have to test it (->later).
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

Daniel

  • Regular poster
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 124
  • I'm *not* a llama!
    • View Profile
Re: Executing files
« Reply #8 on: September 25, 2007, 12:43:25 PM »

It doesn't open any kind of window. It's console application which only returns a number.
But a console application *does* open a window (the console itself) so I'm not sure I understand... If a console application opens, it will indeed shift the focus from the game.

Anyway, did you try 7 (SW_SHOWMINNOACTIVE) as the nShowCmd parameter? It should work even if a console opens.

If it still doesn't work you can try:

Code: [Select]
external "user32.dll" int LockSetForegroundWindow(int uLockCode);

and call the function with a value of 1 (LSFW_LOCK) to disable subsequent calls to SetForgroundWindow(). Like this:

Code: [Select]
var retval = LockSetForegroundWindow(1);
var a = ShellExecuteA(Game.Hwnd, "open", Filename, "", "", 8);
Game.Msg(a);

Hope it helps...
Logged

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Executing files
« Reply #9 on: September 25, 2007, 02:37:30 PM »

Daniel, thank you so much!! Locking works great. I owe you  ::beer ::beer ::beer
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Executing files
« Reply #10 on: September 25, 2007, 03:01:44 PM »

LockSetForegroundWindow doesn't seem to be supported by Win98 though.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

Daniel

  • Regular poster
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 124
  • I'm *not* a llama!
    • View Profile
Re: Executing files
« Reply #11 on: September 25, 2007, 03:37:46 PM »

LockSetForegroundWindow doesn't seem to be supported by Win98 though.
You seem to be right, Mnemonic. I'm so not used to coding for older systems that I didn't even pay attention... Sorry about that...

Does this prevent you from using this function then, meta?
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Executing files
« Reply #12 on: September 25, 2007, 08:41:42 PM »

Eventually it turned out to be a bit more complex, because we needed to fetch the return value of the called process. Some WinAPI trickery translated to WME scripting resulted in the following... thing. Maybe one day someone will find it useful. If anything, the code demonstrates the use of WME's MemBuffer object for simulating structures and passing them to Windows API functions.

Code: WME Script
  1. external "kernel32.dll" int CreateProcessA(string lpApplicationName, int lpCommandline, int lpProcessAttributes, int lpThreadAttributes, bool bInheritHandles, int dCreationFlags, int lpEnvironment, int lpCurrentDirectory, membuffer lpStartupInfo, membuffer lpProcessInformation);
  2. external "kernel32.dll" int GetLastError();
  3. external "kernel32.dll" int WaitForSingleObject(int hObject, int dwTimeout);
  4. external "kernel32.dll" int CloseHandle(int hObject);
  5. external "kernel32.dll" int GetExitCodeProcess(int hProcess, membuffer lpExitCode);
  6.  
  7. ////////////////////////////////////////////////////////////////////////////////
  8. function ApiTest()
  9. {
  10.   var ShowWindow = 7; // SW_SHOWMINNOACTIVE
  11.        
  12.   var StartupInfo = new MemBuffer(68);
  13.   StartupInfo.SetInt(0, 68);   
  14.   StartupInfo.SetInt(44, 1); // STARTF_USESHOWWINDOW
  15.   StartupInfo.SetShort(48, ShowWindow);
  16.  
  17.   var ProcessInfo = new MemBuffer(16);
  18.  
  19.   var Result = CreateProcessA("c:\path\some_application.exe", 0, 0, 0, 0, 0, 0, 0, StartupInfo, ProcessInfo)
  20.   if(Result != 0)
  21.   {
  22.     var hProcess = ProcessInfo.GetLong(0);
  23.     var hThread  = ProcessInfo.GetLong(4);
  24.    
  25.     WaitForSingleObject(hProcess, -1);
  26.    
  27.     var ExitCodeBuf = new MemBuffer(4);
  28.     GetExitCodeProcess(hProcess, ExitCodeBuf);
  29.    
  30.     CloseHandle(hThread);
  31.     CloseHandle(hProcess);
  32.    
  33.     var ExitCode = ExitCodeBuf.GetLong(0);
  34.    
  35.     Game.LOG("ExitCode: " + ExitCode);
  36.  
  37.   }
  38.   else Game.Msg("CreateProcess failed, LastError = " + GetLastError());
  39. }
  40.  
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Executing files
« Reply #13 on: September 25, 2007, 08:45:01 PM »

as crazy as it looks, this piece of code saved my butt again! Thank you!  ::beer ::beer ::beer
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

Daniel

  • Regular poster
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 124
  • I'm *not* a llama!
    • View Profile
Re: Executing files
« Reply #14 on: September 25, 2007, 10:56:29 PM »

Thanks Mnemonic, I learned something new today. I didn't realize before how extensive the WinAPI (and other libraries, I guess) support is from within the script engine. And I didn't even know about the MemBuffer object before, and how it can be used to fill WinAPI (and other) structures.

Once again, I'm amazed!

By the way, just out of curiosity, any idea why ShellExecute with SW_SHOWMINNOACTIVE didn't work for meta, while CreateProcess with the same parameter did?
Logged
Pages: [1] 2  All
 

Page created in 0.054 seconds with 25 queries.