首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C语言操作其他其他软件的有关问题

2012-02-24 
C语言操作其他其他软件的问题我想用C实现如下功能:我现在有一个播放器软件,我想用c操作它,要如何实现?我的

C语言操作其他其他软件的问题
我想用C实现如下功能:
我现在有一个播放器软件,我想用c操作它,要如何实现?我的程序要求在程序开始执行时打开这个软件,然后再执行过程中不断的播放数据,等执行完了再将软件关闭。



[解决办法]
哦,我前几天刚这样做过.你现要确定这个播放器软件有没有命令行模式.
如果有的话,直接system()调用就行了.

memset(playcommand,0,64);
strcpy(playcommand, "splay /etc/music/ ");
strcat(playcommand,command);
strcat(playcommand, ".mp3 & "); //后台提交
// printf( "sound=%s\n ",command);
system(playcommand);

在程序中如果想中止这个播放软件,可以kill它

[解决办法]
一般情况下需要播放器软件支持,即它提供的外部接口,比如楼上说的命令行控制或者支持进程间通信。
若是软件不提供这些功能,仅有一个GUI则是很难用程序控制的,特例是那些游戏外挂,那需要仔细研究那个软件了(包括进行反汇编等逆向工作),然后模拟鼠标键盘操作或者直接修改程序内存,总之很麻烦
[解决办法]
很多软件都有命令行模式,不然你无法将参数传递进去,比如播放什么歌曲之类的
[解决办法]
函数名称: spawnl、spawnle、spawnlp、spawnlpe、spawnv、spawnve、spawnvp、spawnvpe
函数原型: int spawnl(int mode, char *path, char *arg 0, … )
int spawnle(int mode, char *path, char *arg0, … )
int spawnlp(int mode, char *path, char *arg0, … )
int spawnlpe(int mode, char *path, char *arg0, … )
int spawnv(int mode, char *path, char *argv[])
int spawnve(int mode, char *path, char *argv[], char **env)
int spawnvp(int mode, char *path, char *argv[])
int spawnvpe(int mode, char *path, char *argv[], char **env)
函数功能: 在一个程序中调用另外一个程序
函数返回: -1:调用失败,0:调用成功
参数说明: path-被调用程序路径,arg-调用的参数,mode-调用模式,具体如下:
P_WAIT 0 将父过程挂起,直到子过程执行完毕
P_NOWAIT 1 父子过程同时执行,Turboc不支持
P_OVERLAY 2 子过程覆盖父过程
所属文件: <process.h>

#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <errno.h>
#include <string.h>

int main()
{
int process_id;
#if defined(__OS2__)||defined(__NT__)
int status, rc;
#endif

process_id=spawnl(P_NOWAIT, "child.exe ", "child ", "5 ",NULL);
if(process_id==-1){
printf( "spawn failed-%s\n ",strerror(errno));
exit(EXIT_FAILURE);
}
printf( "Process id=%d\n ",process_id);

#if defined(__OS2__)||defined(__NT__)
rc=cwait(&status,process_id,WAIT_CHILD);
if(rc==-1){
printf( "wait failed-%s\n ",strerror(errno));
}else{
printf( "wait succeeded-%x\n ",status);
switch(status&0xff){
case 0:
printf( "Normal termination code=%d\n ",status> > 8);
break;
case 1:
printf( "Hard-error abort\n ");
break;
case 2:
printf( "Trap operation\n ");
break;
case 3:
printf( "SIGTERM signal not intercepted\n ");
break;
default:
printf( "Bogus return status\n ");
}
}
#endif
printf( "spawn completed\n ");
return 0;
}

/*
[child.c]
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>

int main(int argc,char *argv[])
{
int delay;
if(argc <=1)
exit(EXIT_FAILURE);
delay=atoi(argv[1]);
printf( "I am a child going to sleep for %d seconds\n ",delay);


sleep(delay);
printf( "I am a child awakening\n ");
exit(123);
return 0;
}
*/

[解决办法]
int exec…装入和运行其它程序

int execl( char *pathname,char *arg0,char *arg1,…,char *argn,NULL)

int execle( char *pathname,char *arg0,char *arg1,…,

char *argn,NULL,char *envp[])

int execlp( char *pathname,char *arg0,char *arg1,…,NULL)

int execlpe(char *pathname,char *arg0,char *arg1,…,NULL,char *envp[])

int execv( char *pathname,char *argv[])

int execve( char *pathname,char *argv[],char *envp[])


int execvp( char *pathname,char *argv[])


int execvpe(char *pathname,char *argv[],char *envp[])

exec函数族装入并运行程序pathname,并将参数arg0(arg1,arg2,argv[],envp[])传递给子程序,出错返回-1
在exec函数族中,后缀l、v、p、e添加到exec后,所指定的函数将具有某种操作能力
有后缀 p时,函数可以利用DOS的PATH变量查找子程序文件。
l时,函数中被传递的参数个数固定。
v时,函数中被传递的参数个数不固定。
e时,函数传递指定参数envp,允许改变子进程的环境,
无后缀e时,子进程使用当前程序的环境。
==============================
参考一下

热点排行