开发者

How do I send or save a function and recv or restore a function and execute it in C++?

Is there any way I can send or save a function as file? I want to do this in C++ on a Windows platform.

I assume that there is some convention between reader and sender or saver

Is it OK if I send like below? server side:

label1:
int func1(int somevar){
    int a = 1;
    return somevar+a;
}
label2:

send(sockfd, label2-label1, sizeof(int),开发者_JS百科 0) //send a size first
send(sockfd, label1, label2-label1, 0); // send a "function" 

thanks in advance!


No, there's no way to do that in C++. Languages which could allow transfer of executable code include Erlang, Ruby, TCL.

Chris


There already exists a mechanism for this in windows for C++. Your functions are "saved" when compiling in a dll. You can then "retrieve" your functions from the dll using GetProcAddress().


With C++ this is definitely not possible.

You could send a precompiled dynamically linked library containing the functions and load and execeute them at the end point.


C++ is a static language - you can't modify the executable you're running and add in new functions. Languages like Ruby would allow you to do this, but its just not a feature of C++.

You could, if really really needed, possibly put your function in a file, send it over the network, and have your program on the other end compile the file and run it with a popen from your process or something... but that's getting pretty ridiculous by that point.

I'd say redesign so you don't need this.


YOU CAN DO IT! Just write your own compiler that instead of compiling files, it waits for signals, builds files, and then compiles them. And then possibly runs them.

Basically:

  1. send function text
  2. receive function text
  3. compile with your program the text
  4. execute the compiled code
  5. ???
  6. profit!


No, that's not at all valid. That's basically quite impossible- you would need to use some sort of intermediary language or express the function in assembly or disassemble the resulting binary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜