View Single Post
Re: Running an application
Old
  (#9)
koraX
Member
 
koraX's Avatar
 
Status: Offline
Posts: 145
Join Date: Jan 2004
Location: Slovak Republic
Default Re: Running an application - 04-12-2004

well I have found the ultimate guide to executing stuff about year ago, but is down now, so I'll post here important things

Code:
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,sDisplay,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,lpParameters,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)GetProcAddress(m_hShell32,"Shell_NotifyIconA");
BOOL bSuc=m_p_fnShell_NotifyIcon(dwMessage,lpData);
FreeLibrary(m_hShell32);
return bSuc;
}
-----------------------------------------------------------------------------


kXBot
koraX's utils
- see my homepage for other projects (OpenGL CSG Editor, FAT16 Sim, NNetwork Sim, ...)
  
Reply With Quote