?? 第20章 雜項.txt
字號:
這里唯一的區別是"exec”變為"spawn",并且增加了模式(mode)參數。spawn()函數有覆蓋和等待兩種相對立的功能,它使你可以在spawn()運行期間做出是等待還是離開的決定。實現上,P_WAIT參數回答了下一個問題。
請參見:
20.11 怎樣在一個程序執行期間運行另一個程序?
20.11 怎樣在一個程序執行期間運行另一個程序?
正如你在20.10的例子中看到的那樣,spawn()函數族允許在一個程序中啟動另一個程序,并在后者結束后返回到前者之中。有關spawn()函數的背景知識和例子(你只需把其中的_P_OVERLAY改為_P_WAIT) 請參見20.10。
然而,還有另外一種方法可以完成這項工作,即使用system()函數。system()函數與exec()或spawn()函數相似,但也不有同之處。除了掛起(而不是結束)當前程序去執行新程序外,system()還要啟動COMMAND.COM命令翻譯程序(或者其它任何運行在你的計算機上的命令翻譯程序)。如果它找不到COMMAND.COM或類似的程序,那么它就不會去執行所要求的程序(這一點與exec()或spawn()函數不同)。下例是調用EDIT.COM打開一個文件的另一個程序版本,其中的文件名也來自該例的命令行:
# include <stdio. h>
# include <process. h>
# inclued <stdlib. h>
char argStr[255] ;
void
main(int argc, char **argv)
int ret ;
/ * Have EDIT open a file called HELLO if no arg given * /
sprintf (argStr ,"EDIT %s", (argv[1] == NULL?"HELLO" :argyll3) ) ;
/ * Call the one with variable arguments and an environment * /
ret = sytem (argStr) ;
printf("system() returned %d\n" ,ret) ;
}
與20.10中的例子一樣(使用_P_WAIT),在system()調用后面的print{()語句會被執行,因為原來的程序只是被掛起而不是被終止。在每一種情況下,system()都會返回一個表示是否成功地運行了所指定的程序的值,而不會返回所指定的程序的返回值。
請參見:
20.10怎樣在一個程序后面運行另一個程序?
20.12 怎樣把數據從一個程序傳給另一個程序?
有好幾種基本的方法可以完成這項任務----你可以通過文件或內存來傳遞這些數據。這些方法的步驟都相當簡潔:首先,定義在何處存放數據,如何獲取數據,以及如何通知另一個程序來獲取或設置數據;然后,你就可以獲取或設置數據了,盡管使用文件的技術定義和實現起來都比較簡單,但它的速度往往比較慢(并且容易引起混亂)。因此,這里重點討論內存數據轉移技術。下面將依次詳細地分析這一過程的每一個環節:
定義在何處存放數據。當你編寫要共享數據的兩個程序時,你應該讓程序知道要訪問的數據存放在何處。這個環節同樣有幾種實現方法:你可以在一個(或每個)程序中建立一個固定的內部緩沖區,并在兩個程序之間傳遞指向這個緩沖區的指針;你也可以為數據分配動態內存,并在兩個程序之間傳遞指向該數據的指針;如果要傳遞的數據很小,你還可以通過CPU的通用寄存器來傳遞數據(這種可能性很小,因為x86結構的寄存器很少)。分配動態內存是最靈活和模塊性最強的方法。
定義獲取數據的方法。這個環節非常簡潔——你可以使用fmemcpy()或等價的內存拷貝函數。顯然,在獲取和設置數據時都可以使用這個函數。
定義通知另一個程序的方法。因為DOS并不是一個多任務操作系統,所以其中一個(或兩個)程序的一部分必須已經駐留在內存中,并且可以接受來自另一個程序的調用。同樣,這個環節也有幾種方法可供選擇:第一個程序可以是一個列入CONFIG.SYS中的驅動程序,它在系統啟動時就被裝入內存;第一個程序也可以是一個TSR(終止并駐留)程序,在它退出時會把與第二個程序相互作用的那部分程序駐留在內存中;此外,你也可以在第一個程序中利用system()或spawn()函數(見20.11)來啟動第二個程序。你可以根據需要選擇合適的方法。因為有關DOS驅動程序的數據傳遞在DOS文檔中已經有詳盡的描述,而有關system()和spawn()函數的內容也已經在前文中介紹過,因此下面介紹TSR方法。
下面的例子給出了兩個程序:第一個程序是一個完整的TSR程序,但為了突出整個過程中的關鍵環節,它寫得比較單薄(見20.15中的解釋)。這個TSR程序先是安裝了一個中斷63H的中斷服務程序,然后調用終止并駐留退出函數,在執行這個TSR程序后,執行下文給出的另一個程序。這個程序只是簡單地初始化一個對中斷63H的調用(類似于使用中斷21H調用),并且把“Hello There”傳送給上述TSR程序
# include <stdlib. h>
# include <dos. h>
# include <string. h>
void SetupPointers (void) ;
void OutputString(char * );
# define STACKSIZE 4096
unsigned int near OldStackPtr;
unsigned int near OldStackSeg;
unsigned int _near MyStackOff ;
unsigned int _near MyStackSeg;
unsigned char_near MyStack[STACKSIZE];
unsigned char far * MyStackPtr= (unsigned char_far * )MyStack;
unsigned short AX, BX,CX, DX,ES;
/ * My interrupt handler * /
void_interrupt_far_cdecl NewCommVector (
unsigned short es, unsigned short ds, unsigned short di,
unsigned short si, unsigned short bp, unsigned short sp,
unsigned short bx, unsigned short dx, unsigned short cx,
unsigned short ax, unsigned short ip, unsigned short cs,
unsigned short flags) ;
/ * Pointers to the previous interrupt handier * /
void(_interrupt_far_cdecl * CommVector)();
union REGS regs;
struet SREGS segregs ;
# define COMM_VECTOR 0x63 / * Software interrupt vector * /
/ * This is where the data gets passed into the TSR * /
char_far * eallerBufPtr;
char localBuffer[255]; / * Limit of 255 bytes to transfer * /
char_far * localBufPtr=(ehar_far * )loealBuffer;
unsigned int ProgSize= 276; / * Size of the program in paragraphs * /
void
main(int argc,char * * argv)
{
int i, idx;
/ * Set up all far pointers * /
SetupPointers () ;
/ * Use a cheap hack to see if the TSR is already loaded
tf it is, exit,doing nothing * /
comm_veetor =_dos_getvect (COMM_VECTOR) ;
if(((long)eomm_vector & 0xFFFFL) ==
((long) NewCommVector & OxFFFFL ) ) {
OutputString("Error :TSR appears to already be loaded. \n");
return ;
/ * If everything's set,then chain in the TSR * /
_dos_setvect (COMM_VECTOR ,NewCommVector) ;
/ * Say we are loaded * /
OutputString("TSR is now loaded at 0x63\n");
/ * Terminate, stay resident * /
dos_keep (0, ProgSize ) ;
}
/ * Initializes all the pointers the program will use * /
void
Set upPointers ( )
{
int idx ;
/ * Save segment and offset of MyStackPtr for stack switching * /
MyStackSeg = FP_SEG (MyStackPtr) ;
MyStackOff = FP_OFF (MyStackPtr) ;
/ * Initialize my stack to hex 55 so I can see its footprint
if I need to do debugging * /
for (idx = 0 ;idx<STACKSIZE ; idx ++ ) {
MyStack [idx] = 0x55 ;
}
}
void _interrupt_ far_cdecl NewCommVector (
unsigned short es, unsigned short ds, unsigned short di,
unsigned short si, unsigned short bp, unsigned short sp,
unsigned short bx, unsigned short dx, unsigned short cx,
unsigned short ax, unsigned short ip, unsigned short cs,
unsigned short flags)
{
AX = ax;
BX = bx ;
CX = cx;
DX = dx ;
ES = es ;
/ * Switch to our stack so we won't run on somebody else's * /
_asm {
;set up a local stack
eli ; stop interrupts
mov OldStackSeg,ss ; save stack segment
mov OldStackPtr,sp ; save stack pointer (offset)
mov ax,ds ; replace with my stack s
mov ss,ax ; ditto
mov ax,MyStackOff ; replace with my stack s
add ax,STACKSIZE-2 ;add in my stack size
mov sp ,ax ; ditto
sti ; OK for interrupts again
}
switch (AX) {
case 0x10; / * print string found in ES:BX */
/ * Copy data from other application locally * /
FP_ SEG (callerBufPtr) = ES ;
FP_OFF (callerBufPtr) = BX ;
_fstrcpy (localBufPtr, callerBufPtr ) ;
/ * print buffer 'CX' number of times * /
for(; CX>0; CX--)
OutputString (localBufPtr) ;
AX=1; /* show success */
break ;
case 0x30: /* Unload~ stop processing interrupts * /
_dos_setvect (COMM_VECTOR ,comm_vector) ;
AX=2; /* show success */
break ;
default :
OutputString (" Unknown command\r\n" ) ;
AX= 0xFFFF; / * unknown command-1 * /
break ;
}
/ * Switch back to the caller's stack * /
asm {
cli ;turn off interrupts
mov ss,OldStackSeg ;reset old stack segment
mov sp,OldStackPtr ;reset old stack pointer
sti ;back on again
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -