PDA

View Full Version : Running an application


Rifleman
02-12-2004, 09:26
Is there any function that I can called to run a program at a specifc directory ?

Pierre-Marie Baty
02-12-2004, 11:22
On Windows, use CreateProcess.

On Linux, use the execv family of functions.

These functions allow you to specify the working directory.

Rifleman
03-12-2004, 12:56
gosh , I nearly fainted looking at the function's agruement
:( , how about some example ? Like launching hl ?

Whistler
03-12-2004, 13:35
search for MSDN...
http://msdn.microsoft.com

koraX
04-12-2004, 11:36
gosh , I nearly fainted looking at the function's agruement
:( , how about some example ? Like launching hl ?

Instead of almighty CreateProcess, use ShellExecute.

example of opening .rtf document :
ShellExecute(theApp.m_pMainWnd->GetSafeHwnd(),NULL,"document.rtf",NULL,"C:\\foo",SW_SHOW);

Rifleman
04-12-2004, 12:20
thanks for the reply kx , but I think shellexcute cant specify a specific directory of the program

Pierre-Marie Baty
04-12-2004, 14:59
CreateProcess is not that complicated. There are good examples on MSDN, look it up, it's well explained.

koraX
04-12-2004, 15:00
thanks for the reply kx , but I think shellexcute cant specify a specific directory of the program
You mean working directory ? It is 5. parameter in shellexecute

koraX
04-12-2004, 18:47
well I have found the ultimate guide to executing stuff about year ago, but is down now, so I'll post here important things :)

Execute Program Way 1
-----------------------------------------------------------------------------
WinExec("d:\\vc\\finished\\mqyvc\\release\\mqyvc.exe",SW_SHOW);
-----------------------------------------------------------------------------

Execute Program Way 2
-----------------------------------------------------------------------------
ShellExecute(NULL,NULL,_T("1.txt"),NULL,_T("c:\\temp"),SW_SHOW);
ShellExecute(NULL, _T("open"), "www.alsk.net", NULL,NULL,SW_SHOW);
ShellExecute(NULL, _T("explore"), "d:\\vc\\temp", NULL,NULL,SW_SHOW);
-----------------------------------------------------------------------------

Execute Program Way 3
-----------------------------------------------------------------------------
char szCmdLine[100];
STARTUPINFO StartupInfo;
PROCESS_INFORMATION ProcessInfo;
GetStartupInfo(&StartupInfo);
StartupInfo.dwFlags=STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow=SW_HIDE;
CreateProcess("d:\\vc\\finished\\mqyvc\\release\\mqyvc.exe",
szCmdLine,NULL,NULL,0,0,NULL,NULL,&StartupInfo,&ProcessInfo);
WaitForSingleObject(ProcessInfo.hProcess,5000);//or INFINITE
-----------------------------------------------------------------------------

Execute Program Way 4
-----------------------------------------------------------------------------
SHELLEXECUTEINFO seInfo;
ZeroMemory(&seInfo, sizeof(SHELLEXECUTEINFO));
seInfo.cbSize=sizeof(SHELLEXECUTEINFO);
seInfo.lpFile=strBrowserPath;
seInfo.lpParameters="D:\\Mqy\\Prj\\RunBrowser\\ExeFiles\\AutoLauchIndex .htm";
seInfo.nShow = SW_SHOWNORMAL;
ShellExecuteEx(&seInfo);
WaitForSingleObject(seInfo.hProcess,INFINITE);
-----------------------------------------------------------------------------

Use Multi-Language Dll
-----------------------------------------------------------------------------
In EPFApp.h:
HINSTANCE m_hLanDLL;

BOOL CEPFApp::InitInstance()
{
WORD wLangPID=PRIMARYLANGID(::GetSystemDefaultLangID()) ;
switch(wLangPID)
{
case LANG_CHINESE:
m_hLanDLL=::LoadLibrary("EPResCn.dll");
break;
case LANG_JAPANESE:
m_hLanDLL=::LoadLibrary("EPResJp.dll");
break;
case LANG_ITALIAN:
m_hLanDLL=::LoadLibrary("EPResIt.dll");
break;
}
if(!m_hLanDLL)
m_hLanDLL=AfxGetResourceHandle();
AfxSetResourceHandle(m_hLanDLL);

.....
}

LoadString(theApp.m_hLanDLL,IDS_STR_TabRoll,sDispl ay,256);
-----------------------------------------------------------------------------

Use Dll's Function in Two Way
-----------------------------------------------------------------------------
In dll's Exemid_M.h:
#ifdef EXEMID_MDLL_EXPORTS
#define EXEMID_MDLL_API extern "C" __declspec(dllexport)
#else
#define EXEMID_MDLL_API extern "C" __declspec(dllimport)
#endif
EXEMID_MDLL_API int __stdcall DllExemidInit(int iP1,int iP2);

In dll's Exemid_M.cpp:
#define EXEMID_MDLL_EXPORTS
#include "Exemid_M.h"
int __stdcall DllExemidInit(int iP1,int iP2)
{
...
return 0;
}

In dll's Exemid_M.def:
EXPORTS DllExemidInit;
-------------------------------------
Way1:Run time load
-------------------------------------
In EPFApp.h:
#include "EPDllDef.h"
HINSTANCE m_hImgDLL;

In EPDllDef.h:
typedef int (__stdcall *DllExemidInitDef)(int iP1,int iP2);
extern DllExemidInitDef DllExemidInit;

In EPFApp.cpp:
DllExemidInitDef DllExemidInit;
int CEPFApp::LoadDllFunc(void)
{
m_hImgDLL=::LoadLibrary("Exemid_M.dll");
DllExemidInit = (DllExemidInitDef)GetProcAddress(m_hImgDLL,"DllExemidInit");
}

In use place:
#include "EPFApp.h"
DllExemidInit(10,20);
-------------------------------------
Way2:Ini load together
-------------------------------------
In EPFApp.h:
#include "../Exemid_M/Exemid_M.h"
#pragma comment(lib,"../Exemid_M/Temp/Exemid_M.lib")

In use place:
#include "EPFApp.h"
DllExemidInit(10,20);
-----------------------------------------------------------------------------

Declare Standard Dll
-----------------------------------------------------------------------------
int HXSmtp.h:

#define HXSMTP_API extern "C" __declspec(dllexport)

HXSMTP_API void SmtpInform(char* strSubjet, char* strBody);
HXSMTP_API void CloseSmtp();

in HXSmtp.cpp:

HXSMTP_API void SmtpInform(char* strSubjet, char* strBody){}
HXSMTP_API void CloseSmtp(){}

in HXSmtp.def:

LIBRARY "HXSmtp"
DESCRIPTION 'HXSmtp Windows Dynamic Link Library'

EXPORTS
EXPORTS SmtpInform;
EXPORTS CloseSmtp;
-----------------------------------------------------------------------------

In Dll Use Outside Function
-----------------------------------------------------------------------------
in the dll:

typedef int( __cdecl *LPWriteLocalLog )(void *, unsigned long);
LPWriteLocalLog g_hWriteLocalLog = NULL;

HXSLLFUNC_API int NetWorkInit(LPWriteLocalLog)
{
g_hWriteLocalLog = hWriteLocalLog;
}

if( g_hWriteLocalLog )
g_hWriteLocalLog(bufExcept,iSizeExcept);

out the dll:
NetWorkInit(&WriteLocalLog);
-----------------------------------------------------------------------------

Use Dll Function
-----------------------------------------------------------------------------
typedef int MyTextOutDef(HDC hdc,int nXStart,int nYStart, LPCTSTR lpString,int cbString);
HINSTANCE hinst=LoadLibrary("a.dll");
MyTextOutDef *MyTextOut=(MyTextOutDef*)GetProcAddress(hinst,"MyTextOut");
MyTextOut(GetDC()->GetSafeHdc(),1,1,"aaa",3);
FreeLibrary(hinst);

to use this way, must have line in a.del of this dll project:
EXPORTS MyTextOut;
-----------------------------------------------------------------------------

Use Class in Dll
-----------------------------------------------------------------------------
1. Generate dll project
New-> MFC AppWizard(dll)->MFC Extension Dll(Using shared MFC dll)->
Add Class CMDXRowset as the following way:
in .h:
#define CLASS_EXPORT AFX_CLASS_EXPORT
class CLASS_EXPORT CMDXRowset
{
public:
......
}

2. Use dll class in other project
Copy and insert the class's .h file.
Link with xxx.lib.
Run with xxx.dll.
-----------------------------------------------------------------------------

Self Defined Dll Functions
-----------------------------------------------------------------------------
void CEPApp::MyShellExecute(HWND hwnd, LPCSTR lpOperation, LPCSTR lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, INT nShowCmd)
{
typedef HINSTANCE (WIEPPI *SHELLEXECUTE)(
HWND hwnd,
LPCSTR lpOperation,
LPCSTR lpFile,
LPCSTR lpParameters,
LPCSTR lpDirectory,
INT nShowCmd);
HMODULE m_hShell32=LoadLibrary("Shell32.dll");
SHELLEXECUTE m_p_fnShellExecute=(SHELLEXECUTE)GetProcAddress(m_ hShell32,"ShellExecuteA");
m_p_fnShellExecute(hwnd,lpOperation,lpFile,lpParam eters,lpDirectory,nShowCmd);
FreeLibrary(m_hShell32);
}
BOOL CEPApp::MyShell_NotifyIcon(DWORD dwMessage, PNOTIFYICONDATAA lpData)
{
typedef BOOL (WIEPPI *SHELLNOTIFYICON)(
DWORD dwMessage, PNOTIFYICONDATAA lpData);
HMODULE m_hShell32=LoadLibrary("Shell32.dll");
SHELLNOTIFYICON m_p_fnShell_NotifyIcon=(SHELLNOTIFYICON)GetProcAdd ress(m_hShell32,"Shell_NotifyIconA");
BOOL bSuc=m_p_fnShell_NotifyIcon(dwMessage,lpData);
FreeLibrary(m_hShell32);
return bSuc;
}
-----------------------------------------------------------------------------

Rifleman
05-12-2004, 07:04
I see , thanks !

Rifleman
07-12-2004, 07:30
Final question , what function should I use to create a folder ?

sPlOrYgOn
07-12-2004, 07:36
mkdir()
:)

Pierre-Marie Baty
07-12-2004, 09:49
mkdir() on windows does not correspond to mkdir() on Linux.
Use something like that:

#ifdef WIN32
#define POSIX_mkdir(a,b) _mkdir(a)
#else
#define POSIX_mkdir(a,b) mkdir(a,b)
#endif

and call POSIX_mkdir instead.

Rifleman
07-12-2004, 10:45
wow ! Pierre , I will marry you if you're a female :D

Pierre-Marie Baty
07-12-2004, 12:27
no, thank you. I'm not, and I'm not gay either >:(

Rifleman
07-12-2004, 15:57
wow ! Pierre , I will marry you if you're a female :D
I suppose you see to word "if" :D

Whistler
08-12-2004, 08:08
well I have grepped my /usr/include directory in my Debian GNU/linux 3.0 and found no .h files has "mkdir()"

and this also doesn't work:

#include <unistd.h>
mkdir("this_is_a_dir", 777);

Rifleman
08-12-2004, 11:46
#include <direct.h>

in my vc++ 6.0

Whistler
08-12-2004, 14:33
well I suppose you don't know what GNU/Linux operating system is...
It's "not unix", but it's also absolutely not Windows. :D
the GNU/Linux operating system, which is commonly mis-named as "Linux", is a free UNIX clone operating system. It's mostly using GNU software with Linux as the kernel. As it's free everyone can make their own version of system, currently the most popular version is Red Hat Linux (which is only available for purchasing). If you want to download a version, you can download Debian GNU/Linux 3.0 r3 at:
http://www.debian.org (the size is as big as 7 cds as it contains tons of software)
There's also a version at free software foundation's ftp server, which is much smaller (only 1 cd):
ftp://ftp.gnu.org/gnu+linux-distros/ututo-e/ututo-e-v1.2-i486-pentium.iso
to install it, you'll need a CD-RW drive to burn the ISO images to CDs. then boot from the CD and follow the instructions.

(another thing is MS Visual C++ doesn't work on GNU/Linux :D)

Pierre-Marie Baty
08-12-2004, 15:29
Ahhhhh, Linux.
Crappy Linux.


/* mkrmdir.c -- BSD compatible directory functions for System V
Copyright (C) 1988, 1990 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#ifndef STDC_HEADERS
extern int errno;
#endif
/* mkdir and rmdir adapted from GNU tar. */
/* Make directory DPATH, with permission mode DMODE.
Written by Robert Rother, Mariah Corporation, August 1985
(sdcsvax!rmr or rmr@uscd). If you want it, it's yours.
Severely hacked over by John Gilmore to make a 4.2BSD compatible
subroutine. 11Mar86; hoptoad!gnu
Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
subroutine didn't return EEXIST. It does now. */
int
mkdir (dpath, dmode)
char *dpath;
int dmode;
{
int cpid, status;
struct stat statbuf;
if (stat (dpath, &statbuf) == 0)
{
errno = EEXIST; /* stat worked, so it already exists. */
return -1;
}
/* If stat fails for a reason other than non-existence, return error. */
if (! existence_error (errno))
return -1;
cpid = fork ();
switch (cpid)
{
case -1: /* Cannot fork. */
return -1; /* errno is set already. */
case 0: /* Child process. */
/* Cheap hack to set mode of new directory. Since this child
process is going away anyway, we zap its umask.
This won't suffice to set SUID, SGID, etc. on this
directory, so the parent process calls chmod afterward. */
status = umask (0); /* Get current umask. */
umask (status | (0777 & ~dmode)); /* Set for mkdir. */
execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
_exit (1);
default: /* Parent process. */
while (wait (&status) != cpid) /* Wait for kid to finish. */
/* Do nothing. */ ;
if (status & 0xFFFF)
{
errno = EIO; /* /bin/mkdir failed. */
return -1;
}
return chmod (dpath, dmode);
}
}
/* Remove directory DPATH.
Return 0 if successful, -1 if not. */
int
rmdir (dpath)
char *dpath;
{
int cpid, status;
struct stat statbuf;
if (stat (dpath, &statbuf) != 0)
return -1; /* stat set errno. */
if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
{
errno = ENOTDIR;
return -1;
}
cpid = fork ();
switch (cpid)
{
case -1: /* Cannot fork. */
return -1; /* errno is set already. */
case 0: /* Child process. */
execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
_exit (1);
default: /* Parent process. */
while (wait (&status) != cpid) /* Wait for kid to finish. */
/* Do nothing. */ ;
if (status & 0xFFFF)
{
errno = EIO; /* /bin/rmdir failed. */
return -1;
}
return 0;
}
}