NO

Author Topic: WinExec / ShellExecute functions  (Read 17002 times)

liut

  • Guest
WinExec / ShellExecute functions
« on: April 16, 2012, 05:39:25 AM »
Hi, I'm going to write code to enable a mailto window with info. But no matter whether I use WinExec or ShellExecute, there are a lot of errors in 'shellapi.h' when compiling. I have enable the option '-Ze'.

Any comments? Here is the code:

#include <stdio.h>
#include <shellapi.h>
#include <winbase.h>

int main()
{
   static char *cmdstr="mailto:myself@abc.com?subject=test&body=line 1\nline 2\nlast line\n";

   if(ShellExecute(NULL,"open",cmdstr,NULL,NULL,SW_SHOWNORMAL)<=32) printf("fail to execute!\n");
   else printf("success to execute!\n");
   return 0;
}


CommonTater

  • Guest
Re: WinExec / ShellExecute functions
« Reply #1 on: April 16, 2012, 07:37:39 AM »
First... include Windows.h  .... not winbase.h .... and include shellapi *after* windows.h ....

On the first line of your source code add ...  #define WIN32_DEFAULT_LIBS .... to avoid linker errors.

Project-> Project Options -> Compiler -> Enable Microsoft Extensions = checked.

There's probably more but this should cut most of it down for you...


liut

  • Guest
Re: WinExec / ShellExecute functions
« Reply #2 on: April 16, 2012, 09:11:04 AM »
It works now! Thanks a lot!

But interesting, ShellExecute is no problem. WinExec looks doesn't work. It hasn't been supported? Anyway it's enough for me. :)


CommonTater

  • Guest
Re: WinExec / ShellExecute functions
« Reply #3 on: April 16, 2012, 04:59:58 PM »
That's because they do different things.... 
 
WinExec runs programs... but you have to name the program to be run.  It's also a depricated function left over from 16 bit windows.
 
ShellExecute or ShellExecuteEx run programs and will also search the file assosiations data base to find the correct program to use for non-specific url or path specifications performing actions such as Open or Edit that you find on the right click (context) menus.
 
In your command line you are not naming the program to use... so you need ShellExecute.
 
If you don't have the Windows Software Development Kit, which documents all public API calls, you can download an ISO of it from HERE so you can install it on your own system or you can use the MSDN online version HERE (I recommend the ISO since the online information is becomming ridiculously hard to navigate.)
 
 

liut

  • Guest
Re: WinExec / ShellExecute functions
« Reply #4 on: April 17, 2012, 08:28:51 AM »
Thanks again! I know the difference and used an actual program in WinExec. Maybe something wrong in my prg. I do have the ISO of SDK and VS2005 Express. But they are too huge, and extremely complicated. I still like PellesC much better!

CommonTater

  • Guest
Re: WinExec / ShellExecute functions
« Reply #5 on: April 17, 2012, 11:50:25 AM »
If you have the sdk that I linked for you, install only the "documentation" part for "win32 and com"... That will give you the information for the windows headers included with Pelles C.   There's no reason to install anything more (.net etc), since Pelles C doesn't support it.

As for the extremely complicated part... It's Windows, get used to it  ;)   The Windows API is a collection of 20,000+ function calls and programming with it requires a lot of "looking stuff up".  No offense is intended but if you're going to shy away from complexity you're not going to write much code.
 
You may also want to get a copy of TheForgers tutorial for Win32 from HERE.  This is an entry level tutorial to show you the general form and style of Windows GUI mode programming... I strongly suggest working through it page by page, read everything, try all the examples, it should give you enough to start writing some simple programs. The documentation you need for windows controls etc is in the SDK.
 

liut

  • Guest
Re: WinExec / ShellExecute functions
« Reply #6 on: April 20, 2012, 02:59:10 PM »
Thanks for your links! I find my previous iso doesn't contain the document part:
http://www.microsoft.com/download/en/details.aspx?id=8442
although it looks newer... :(

Actually I don't think the win functions too complicated, all look "important", although I like unix native function much more. I just think the visual c++ making the thing totally complicated, perhaps I used to use the cc in *nix system...  :) Therefore PellesC is an excellent thing to build up win app rapidly, easily, and powerfully. I have created some apps by it, and used poinst to build up the distribution package successfully...  ;) As my thought, if there is a msi generator inside, that will be better! Now I use free exe2msi tool to convert the package.

CommonTater

  • Guest
Re: WinExec / ShellExecute functions
« Reply #7 on: April 20, 2012, 04:53:31 PM »
Thanks for your links! I find my previous iso doesn't contain the document part:
http://www.microsoft.com/download/en/details.aspx?id=8442
although it looks newer... :(

The link I use for the SDK is one generation older than the newest Win7 version.  You'll note the version numbers are different...  The new ISO is wired up so that the API browser goes online to present function call information.  The ISO sizes are a dead giveaway :D

MS says it's so you always have the latest information.  I say it's a terrible mistake since a programmer would be crippled if the internet shut down or he was required to work on an isolated system for security reasons... That kind of reliance upon external technologies, not under one's control, is wrong in so many ways.

Quote
Actually I don't think the win functions too complicated, all look "important", although I like unix native function much more. I just think the visual c++ making the thing totally complicated, perhaps I used to use the cc in *nix system...  :) Therefore PellesC is an excellent thing to build up win app rapidly, easily, and powerfully. I have created some apps by it,

The reason I pointed you to the forger's tutorial is that, as you appear to already know, windows GUI mode programs have a whole different way of doing things, unlike any console program you're likely to find.  I noticed your example was using main() as it's entry point so I included the commentary mostly because I wasn't sure what your skill level was.
 
Many *nix and console programmers find the jump to windows GUI mode somewhat confusing... Typical of MS, Windows is a horse of a completely different colour.
 
Quote
and used poinst to build up the distribution package successfully...  ;) As my thought, if there is a msi generator inside, that will be better! Now I use free exe2msi tool to convert the package.

If you're looking for a nice, scriptable and easy to use installer packate give THIS a try.  I've been using it more or less from it's inception and have never had any problems with it.  (Never did like the MSI method.)
 

 
« Last Edit: April 20, 2012, 05:17:51 PM by CommonTater »

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: WinExec / ShellExecute functions
« Reply #8 on: April 20, 2012, 07:13:39 PM »
The latest WDK/SDK for Windows allows to download the help from MSDN directly to have it locally available.
It even includes an update feature.
This allows to install only the parts of the help you really need.
---
Stefan

Proud member of the UltraDefrag Development Team

CommonTater

  • Guest
Re: WinExec / ShellExecute functions
« Reply #9 on: April 20, 2012, 10:32:43 PM »
The latest WDK/SDK for Windows allows to download the help from MSDN directly to have it locally available.
It even includes an update feature.
This allows to install only the parts of the help you really need.

I had it here for a bit and never found any option to download the documentation... It just went online to MSDN and looked stuff up in a nice wrapper that basically resembled the older version. 

The one I linked to is an ISO... burn it to a DVD then install from there.  Everything is included on the disk. 

FWIW... I never use the MSInstaller versions... you download a 400k installer that then downloads the 1.4gb ISO and takes a very long time installing it... Or you can download the ISO and do the install in a couple of minutes.


liut

  • Guest
Re: WinExec / ShellExecute functions
« Reply #10 on: April 21, 2012, 04:10:17 AM »
About the installer, I think poinst already powerful enough, cause I can create and remove all the things I want within a single exe file. To have a msi generator, I can deploy the installation to all pc in the domain simply if I want.   :)

Pure Win programming actually is not a complicated thing. When I used Win3.1, I had already a book how to write code. The basic concept has never been changed so far. Of course, as I am not a professional coder and do like *nix system much more, I never pick it up and deep into it. But recently I want to create some small apps for my daily works, so I tried all kinds of c compilers including MSVC. And since I only require a pure c compiler with enough functions what I require, PellesC became the final choice of mine.  ;D

CommonTater

  • Guest
Re: WinExec / ShellExecute functions
« Reply #11 on: April 21, 2012, 06:01:35 AM »
About the installer, I think poinst already powerful enough, cause I can create and remove all the things I want within a single exe file. To have a msi generator, I can deploy the installation to all pc in the domain simply if I want.   :)

Yes it is.  POINST is a pretty good tool.  I went with Inno mainly because of it's Wizards.  They pretty much pre-script the install, making it painfully easy.  (I'm lazy, what can I say :D )

Quote
Pure Win programming actually is not a complicated thing. When I used Win3.1, I had already a book how to write code. The basic concept has never been changed so far.

True enough... fill in the struct call the function... pretty much as always.
 
However, as you study the SDK looking for new weapons for your washing machine, you will rapidly discover that it's grown in leaps and bounds as new technologies are added... Windows Networking, Windows Internet, COM, Winsock, WMI and on and on and on... The basics remain but oh boy is it getting complicated... Especially so with Win7 where you have to contend with Virtualization, Manifests, Code Signing, etc.  And it's only going to become more so with Windows 8 (which I will *never* actually use or program for) 
 
In my usual work, writing small aps that bigger programming houses won't touch, I doubt that I use more than 10% of what's there and it still feels like a lot!

Quote
Of course, as I am not a professional coder and do like *nix system much more, I never pick it up and deep into it.

You will find that your *nix experience isn't much help beyond windows console.  In GUI mode even the entry point is different (WinMain() ) and from there it just gets more and more different as you delve deeper.  It's C code but it's not like any other C code you'll find.  I like programming at the Windows API level, don't use 3rd party libraries unless I have to (which has only happened once so far) and prefer to build things from a white page as much as I can.  Tried some *nix programming and found it ok but definately not what I'm used to ... especially when you end up with 30% CPU usage just to play an MP3 file that windows runs with less than 5% ... I think although windows is more complex, it's also more efficient.

Quote
But recently I want to create some small apps for my daily works, so I tried all kinds of c compilers including MSVC. And since I only require a pure c compiler with enough functions what I require, PellesC became the final choice of mine.  ;D

Yep... I started out on Borland's Turbo Basic, moved to Turbo Pascal after about a week and then, more than a decade later, I moved to C after they mucked everything up with Delphi (lord I hated that language!).  From time to time I will try different IDEs and different compilers but somehow I always end up back on Pelles C... I believe it's currently the best setup you can get, at any price.  Pelle is to be congratulated for his skill and generosity.




liut

  • Guest
Re: WinExec / ShellExecute functions
« Reply #12 on: April 21, 2012, 02:37:45 PM »
It's truth that Win's complexity is 100 times than *nix. MS always think they can do better than user, and they always keep patching for their mistakes on the designing... For myself I think native win programming is not so terrible, but if lack of document or description, that's really nightmare. Recently I feel it strongly: when I want to get some info from the system but I don't know using which api to do, I always became crazy to seek and read any things on MSDN and Google...  >:(
Fortunately so far, I could always find a way to complete the things I expect finally  ;D

I think I'm somewhat lucky, since I needn't code for my life, just for efficiency and pure interest. C is the only language I know at present, x86asm / pascal / basic / foxpro were tried long ago when in school. I still remember many details in Turbo C 2.0 (even I should still have a copy of it on 360K floppy  :)). And then all experiences were from cc in *nix in the past years.

At the beginning when I tried PellesC, I also tried several free c compiler under win system including Lccwin, CC386, Tcc. I compared the generated execution code's size and performance by using the same source code, PellesC looks the best one on the overall optimization of code size and speed.

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: WinExec / ShellExecute functions
« Reply #13 on: April 21, 2012, 04:33:01 PM »
I had it here for a bit and never found any option to download the documentation...
Use the Help Library Manager installed with the documentation to turn offline mode on.
This application will allow downloading and updating of the offline help too.

This application pops up after installation, so you can configure it directly.
---
Stefan

Proud member of the UltraDefrag Development Team

CommonTater

  • Guest
Re: WinExec / ShellExecute functions
« Reply #14 on: April 21, 2012, 05:21:22 PM »
It's truth that Win's complexity is 100 times than *nix. MS always think they can do better than user, and they always keep patching for their mistakes on the designing... For myself I think native win programming is not so terrible, but if lack of document or description, that's really nightmare.
This is where having the SDK on your local disk comes in real handy... It comes at it from both an index of functions and table of contents, so that you can start from "I need a ______ type function" and drill down in to find it.  The documentation is VERY complete but it does take a bit of time to get used to the way it's organized.
 
The online MSDN version is a dog's breakfast... I don't know what they think they're doing but the downloadable SDK (esp. the version I linked to) is FAR easier to navigate.

In my experience, Windows documentation is far better than Linux or even BSD... It's just a matter of having the right tools on your system.

Quote
Recently I feel it strongly: when I want to get some info from the system but I don't know using which api to do, I always became crazy to seek and read any things on MSDN and Google...  >:(
Fortunately so far, I could always find a way to complete the things I expect finally  ;D

Not to worry... I figure "writing source code" is quite a ways down the list of a programmer's most important skills... Analysing the problem and "looking stuff up" are probably the two things we do the most.

Quote
I think I'm somewhat lucky, since I needn't code for my life, just for efficiency and pure interest. C is the only language I know at present, x86asm / pascal / basic / foxpro were tried long ago when in school. I still remember many details in Turbo C 2.0 (even I should still have a copy of it on 360K floppy  :) ). And then all experiences were from cc in *nix in the past years.

Ahhh... so getting back into Windows is going to be a bit of an adjustment.  Just like me moving over to Linux would be...  I think the trick is to not be repelled by the difference, to take it on as a challenge...

Quote
At the beginning when I tried PellesC, I also tried several free c compiler under win system including Lccwin, CC386, Tcc. I compared the generated execution code's size and performance by using the same source code, PellesC looks the best one on the overall optimization of code size and speed.

No argument there.  :D


 


@Stephan ...
Thank you.  Noted for future.