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

后端 未结 4 739
独厮守ぢ
独厮守ぢ 2020-12-19 11:01

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, unsi         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-12-19 11:55

    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.

提交回复
热议问题