Execute C++ from String Variable

execute C++ from String variable

You will need to invoke a compiler to compile the code. In addition, you will need to generate some code to wrap the string in a function declaration. Finally, you'll then somehow need to load the compiled code.

If I were doing this (which I would not) I would:

  1. Concatenate a standard wrapper function header around the code
  2. Invoke a compiler via the command line (system()) to build a shared
    library (.dll on windows or .so on linux)
  3. Load the shared library and map the function
  4. Invoke the function

This is really not the way you want to write C code in most cases.

Call a function named in a string variable in C

C does not support this kind of operation (languages that have reflection would). The best you're going to be able to do is to create a lookup table from function names to function pointers and use that to figure out what function to call. Or you could use a switch statement.

Execute a c# statement stored in a string variable

Looking at your comments and that you have 80 controls requiring very similar action, dynamic compilation may be an overkill for this purpose. You can use use Controls collection of the parent container along with the Tag property of your buttons to achieve it. A single event handler would suffice.

You can use LINQ members like OfType and Cast to make your code even smaller.

Edit

After looking at your latest comment, what you should do is to programmatically create your buttons and add them to your Form or whatever container you have. You can then keep a Dictionary<string, Button> that will let you either iterate over the collection, or access an individual button through its name. Something like:

//Declare this globally
Dictionary<string, Button> Dic = new Dictionary<string, Button>(81);

//put this in the constructor
for(int i=0; i<81; i++)
{
Button b = new Button();
b.Text = i; //or Random or whatever
b.Name = "btn" + i.ToString();
this.Controls.Add(b);
Dic.Add(b.Name, b);
}

Later you can do both iteration like this:

foreach(var item in Dic)
{
item.Value.Visible = true; //or false
}

and key-based access like this:

Dic["btn45"].Visible = true; //or false

Since you're creating Sudoku, i probably think you should use TableLayoutPanel with appropriate number of rows and columns at design-time and then add your buttons to the panel and set their Row/Column property in the loop. This will help better respond to resizing events etc.

executing variable in system() in C++

Use a std::string for the command:

std::string cmd = "iptables -i input -s ";
std::string ipaddr = "192.168.11.22";
cmd += ipaddr;
cmd += " -j drop";
system(cmd.c_str());

Or a bit simpler using std::ostringstream:

std::string ipaddr = "192.168.11.22";
std::ostringstream oss;
oss << "iptables -i input -s " << ipaddr << " -j drop";
system(oss.str().c_str());

How to assign string passed through a function call to a variable in c

if you want to copy the string you have to give data it's place. i'm assuming your struct has a member called data and its char*.

you can do this:

size_t len_of_str = strlen(title);

newnode->data = (char *)malloc(len_of_str + 1);
if (NULL == newnode->data)
{
return (NULL);
}

strncpy(newnode->data, title, len_of_str);
newnode->data[len_of_str] = '\0';

assuming your struct looks like this:

struct node{
char *data;
more members...
};

Can't execute inline assembler with the instructions in a string variable

The instructions must be in a form of a string literal(actual written string, the name of a char array is a pointer btw).
Other than that you got the general idea:)

#include <stdio.h>

int main(int argc, char ** argv){
char a[20] = "nice try:)";
char * dst;

asm("mov %[dst], %[src]\n\t"
: [dst]"=r" (dst) : [src]"r"(a));

printf("%s\n", dst);
return 0;
}

and a useful link:
https://dmalcolm.fedorapeople.org/gcc/2015-08-31/rst-experiment/how-to-use-inline-assembly-language-in-c-code.html



Related Topics



Leave a reply



Submit