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

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

问题描述

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

  INT ProcessMe(INT nCommand,无符号整型的wParam,lParam的长);

现在,从主程序文件(EXE),想两个变量传递给插件,并要求插件修改他们的内容,并且主程序会再次阅读,执行一些任务。

我的问题是,从上面的功能,我可以执行此,在不改变函数的参数?

例如:

  INT ProcessMe(INT nCommand,无符号整型的wParam,lParam的长)
{
  //修改参数//
  返回0;
}诠释的main()
{
  BOOL BSAVE = TRUE;
  INT nOption = 0;
  ProcessMe(0,(无符号整数)(安培; BSAVE),(长)(安培; nOption));
  如果(FALSE == BSAVE)
    的printf(BSAVE被修改!);
  返回1;
}


解决方案

将变量在结构修改和指针传递到sturuct的插件:

 结构MYSTRUCT
{
    BOOL BSAVE;
    INT nOption;
};INT ProcessMe(INT nCommand,无符号整型的wParam,lParam的长)
{
    ((* MYSTRUCT)的lParam) - GT; nOption = ...;
    返回0;
}

使用这样的:

  INT的main()
{
  MYSTRUCT结构;
  struct.bSave = TRUE;
  struct.nOption = 0;
  ProcessMe(0,0,(长)(安培;结构));
  如果(FALSE == struct.bSave)
    的printf(BSAVE被修改!);
  返回1;
}

严格地说,这是不确定的行为。你需要检查,无论你的工作平台上。

请注意:我使用的结构在这里,因为这样一来,你也可以传递更多的变量或变数较大,如双待功能

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

解决方案

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.

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

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