加载exe文件并在dos中调用它们的函数
我有一个程序(A),同一文件夹中有另一个可执行文件(B)。我必须在我的程序(A)中调用另一个程序(B)中的函数。而这一切都必须在dos下完成。我该怎么做或者我应该阅读什么才能做到这一点?请帮忙。
I have a program(A) and there is anather executable file(B) in the same folder. I must call function from this anther program(B) in my program(A). And all this must be done in dos. How can i do it or what i should read to do this? Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的两个程序是单独的可执行文件,那么很可能会在两个不同的进程中运行,您不能只跨两个不同的进程调用函数,您需要使用一些进程间通信机制。
您需要开始了解基础知识和知识。从某个地方开始,这里似乎是一个好地方这样做。
既然你提到DOS作为目标平台,DOS是一个非抢占的单用户单处理环境,但DOS环境中的TSR仍然模拟多处理现象。要在 DOS 中实现 IPC,您必须安排 TSR 来捕获软件中断,然后通过它与其进行通信。
If your two programs are separate executables files then will most likely run in two different processes, You cannot just call functions accross two different processes, you need to use some Inter Process communication mechansim.
You need to start understanding the basics & make a start somewhere and this seems to be a good place to do so.
Since you mention DOS as the target platform, DOS is a non-preempted single user single processing environment but still TSR's in DOS environment emulate the phenomenon of multiprocessing. To implement IPC in DOS you will have to arrange for the TSR to collar a software interrupt, and then communicate with it through that.
MS-Dos 是一个 16 位操作系统。在 MS-Dos 中运行的可执行文件有两种类型:“.exe”和“.com”。将“.com”视为具有操作系统假定的许多默认值的“.exe”。 “.exe”文件包含一个标头,操作系统读取该标头以确定各种参数。这些参数之一是入口点地址。仅定义了一个入口点地址(对于“.com”,它始终是 cs:0x100),这是加载程序时操作系统跳转到的地址。
MS-Dos 具有加载另一个可执行文件并运行它的功能,但它只能从标头中给出的地址运行。没有导出其他函数地址,因此您不能仅调用其他可执行文件中的某个任意函数。 MS-Dos 中没有 DLL 系统。
因此,为了调用第二个可执行文件中的某些任意函数,您需要创建自己的 DLL 样式系统。这并不简单,因为操作系统使用分段内存模型,即内存被分为 64k 页,并且地址是由段地址加上偏移量形成的,例如段*16 + 偏移量。所以,同一个物理地址有2^12种表示方式。在加载过程中,MS-Dos 必须修复这些段值以反映程序已加载到的内存中的实际位置。请记住,在 MS-Dos 中没有虚拟内存。如果您要创建自己的 DLL 系统,则需要自行修复大于 64k 的代码(小于 64k 的代码+数据可以忽略段并将所有地址视为 16 位偏移量)。
如果您知道地址,使用 MS-Dos API 加载“.exe”仍然会很棘手,因为您需要知道可执行文件已加载到的 CS(代码段)地址。
MS-Dos is a 16 bit OS. The executables that run in MS-Dos come in two flavours: ".exe" and ".com". Think of the ".com" as a ".exe" with lots of default values assumed by the OS. The ".exe" files contain a header which is read by the OS to determine various parameters. One of these parameters is the entry point address. Only one entry point address is defined (and for a ".com" it is always cs:0x100) and that is the address the OS jumps to when the program has been loaded.
MS-Dos has functions to load another executable and run it, but it can only run from the address given in the header. No other function address is exported so you can't just call some arbitrary function in the other executable. There is no DLL system in MS-Dos.
So, in order to call some arbitrary function in the second executable, you need to create your own DLL style system. This is not trivial since the OS uses a segmented memory model, that is, the memory is divided into 64k pages and addresses are formed from the segment address added to an offset, e.g. segment*16 + offset. So, there are 2^12 ways to express the same physical address. During the loading process, MS-Dos has to fix-up these segment values to reflect the actual location in memory the program has been loaded to. Remember, in MS-Dos there is no virtual memory. If you were to create your own DLL system, you will need to do this fixing-up yourself for code that's bigger than 64k (code+data less than 64k can ignore segments and treat all address as just 16bit offsets).
If you knew the addres, loading the ".exe" using the MS-Dos API would still be tricky as you'd need to know the CS (code segment) address the executable has been loaded to.