2010-08-01 19 views

回答

3

是的,但这是hackish。

在DLL:

typedef void(*funcPtr)(void); 
// Declare a variable named "ptrFunction" which is a pointer to a function which takes void as parameter and returns void 
funcPtr ptrFunction; 
// setter for ptrFunction; will be called from the .exe 
void DLL_EXPORT setFunction(funcPtr f){ptrFunction f;} 
// You can now use function() through ptrFunction 
foo(){ 
    ptrFunction(); 
} 

,你从.exe调用setFunction。

void function(){ 
    // implementation here 
}  
int main(){ 
    setFunction(&function); // now the dll knows the adress of function() 
    foo(); // calls foo() in dll, which calls function() in the .exe 
    .... 
} 

太好了吧? :/ 你应该重构你的代码,以便函数()在另一个DLL中,但是它依赖于它。

+0

typedef行返回错误:预期的标识符或'(''''''')标记之前。 15: – 2010-08-01 12:38:50

+1

@Lela Dax:我认为Calvin1602的意思是'typedef void(* funcPtr)(void);'。 – 2010-08-01 12:41:40

+0

什么是'* f'? – 2010-08-01 12:51:56

2

是你可以,但我强烈建议反对,如果你不就得了。这很烦琐,感觉就像你需要更好地理清你的依赖关系。

要做到这一点,你必须使用LIB.EXE你实际链接之前创建从一个二进制的目标文件导入库;使用此导入库链接其他二进制文件并为其他二进制文件创建导入库;最后使用其他库的导入库来链接原始二进制文件。

E.g.

exe.c:

#include <stdio.h> 

void __declspec(dllimport) dllfn(void); 

void __declspec(dllexport) exefn(void) 
{ 
    puts("Hello, world!"); 
} 

int main(void) 
{ 
    dllfn(); 
    return 0; 
} 

编译器cl /c exe.cexe.obj已创建。

exe.def:

LIBRARY exe.exe 

lib /def:exe.def exe.obj创建导入库。 exe.libexe.exp已创建。

dll.c:

void __declspec(dllimport) exefn(void); 

void __declspec(dllexport) dllfn(void) 
{ 
    exefn(); 
} 

编译cl /c dll.cdll.obj已创建。

链接DLL与link /dll dll.obj exe.libdll.dlldll.libdll.exp被创建。

链接EXE与link exe.obj dll.libexe.exe已创建。 (exe.libexe.exp也被重新创建。)

运行exe,注意Hello, world!的输出。

相关问题