如何修改按值传递的原始变量的内容? [英] How to modify content of the original variable which is passed by value?

查看:17
本文介绍了如何修改按值传递的原始变量的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个现有的 API 函数,它只允许插件(DLL)接收三个参数并执行一些操作:

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);

现在,从主程序(exe)中,想将两个变量传递给插件,并要求插件修改其内容,主程序将再次读取它们,以执行某些任务.

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?

示例:

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;
}

推荐答案

将要修改的变量放在一个结构体中,并将​​指向结构体的指针传递给插件:

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;
}

像这样使用它:

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.

注意:我在这里使用了一个结构体,因为这样你还可以将更多的变量或更大的变量(例如 double)传递给函数.

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

这篇关于如何修改按值传递的原始变量的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆