Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify content of the original variable which is passed by value?

There is an existing API function which does only allow the plugin(DLL) to receive three parameters and perform some action:

int ProcessMe(int nCommand, unsigned int wParam, long lParam);

Now, from the main program(exe), would like to pass two variables to the plugin, and require plugin to modify their content, and main program will read them again, to perform some task.

My question is, from the above function, can I perform this, without changing the function parameters?

Example:

int ProcessMe(int nCommand, unsigned int wParam, long lParam)
{
  // modify the parameters//
  return 0;
}

int main()
{
  BOOL bSave = TRUE;
  int nOption = 0;
  ProcessMe(0, (unsigned int)(&bSave), (long)(&nOption));
  if(FALSE==bSave)
    printf("bSave is modified!");
  return 1;
}
like image 768
wengseng Avatar asked Mar 30 '12 06:03

wengseng


People also ask

Does passing by value change the original variable?

Pass By Value: In Pass by value, function is called by directly passing the value of the variable as an argument. So any changes made inside the function does not affect the original value. In Pass by value, parameters passed as an arguments create its own copy.

Can a Java method change the contents of any variable passed as an argument?

Pass-by-value means that when you call a method, a copy of each actual parameter (argument) is passed. You can change that copy inside the method, but this will have no effect on the actual parameter. Unlike many other languages, Java has no mechanism to change the value of an actual parameter.

Does pass by reference change value?

Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in.

Can you change the value in a variable?

You can "assign a new value" to your variable, but you can't change the value itself, unlike an array or object, which you can modify without affecting the variable binding.


2 Answers

Place the variables to modify in a struct and pass the pointer to the sturuct to the plug in:

struct MyStruct
{
    BOOL bSave;
    int nOption;
};

int ProcessMe(int nCommand, unsigned int wParam, long lParam)
{
    ((MyStruct*)lParam)->nOption = ...;
    return 0;
}

Use it like this:

int main()
{
  MyStruct struct;
  struct.bSave = TRUE;
  struct.nOption = 0;
  ProcessMe(0, 0, (long)(&struct));
  if(FALSE==struct.bSave)
    printf("bSave is modified!");
  return 1;
}

Strictly speaking this is undefined behavior. You need to check, whether it works on your platform.

Note: I used a struct here, because this way you can also pass more variables or larger variables such as double to the function.

like image 53
Henrik Avatar answered Oct 12 '22 23:10

Henrik


No, there's no way to do that.

Passing by value means copies of the original ones are created. In the method scope, you will have no information about the original variables.

You need to either pass by reference or by value.

like image 41
Luchian Grigore Avatar answered Oct 13 '22 00:10

Luchian Grigore