How to change the contents of the original variable, which is passed by value?

There is an existing API function that allows the plug-in (DLL) to receive three parameters and perform some actions:

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

Now, from the main program (exe), I would like to pass two variables to the plugin and require the plugin to change their contents, and the main program will read them again in order to perform some task.

My question is: from the above function, can I do this without changing the parameters of the function?

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;
}
+5
source share
4 answers

Put the variables you want to change in the structure and pass a pointer to 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 as follows:

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

, undefined. , .

. , , , .

+5

, .

, . .

.

+3

... !!!

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

{

int *nCommand_ptr = (int*)nCommand;
unsigned int *wParam_ptr = (unsigned int*)wParam;
long *lParam_ptr = (long*)lParam;
*nCommand_ptr = 10;
*wParam_ptr = 10;
*lParam_ptr = 10;
return 0;

}

int main() {

int nCommand = 5;
unsigned int wParam = 5;
long lParam = 5;
printf("\n %d %u %lu",nCommand,wParam,lParam);
ProcessMe((int)&nCommand,(unsigned int)&wParam,(long)&lParam);
printf("\n %d %u %lu",nCommand,wParam,lParam);
return 0;

}

:

$ > g++ passbyref.cc

$ > ./a.out

5 5 5

10 10 10 $ >

+1

Compilation for a 32-bit platform, which you can use to specify a parameter:

#include <stdio.h>
typedef int BOOL;
#define FALSE 0
#define TRUE 1

int ProcessMe(int nCommand, unsigned int wParam, long lParam)
{
  // cast to match what you passed in
  *((BOOL*)wParam) = FALSE;
  return 0;
}

int cast_arguments_main(int, char **)
{
  BOOL bSave = TRUE;
  int nOption = 0;
  ProcessMe(0, (unsigned int)(&bSave), (long)(&nOption));
  if(FALSE==bSave)
    printf("bSave is modified!");
  return 1;
}

On a 64-bit platform, the compiler complains about the impossibility of representing a pointer:

cast_arguments.cpp:17:37: error: cast fromBOOL*’ tounsigned intloses precision

I had to change the declaration of the parameter unsigned int wParamto unsigned long wParamand a similar change at the point of call: ProcessMe(0, (unsigned long)(&bSave), (long)(&nOption)); but then you can simply declare the correct type for your parameter to be BOOL *. So feasibility depends on your target machine architecture ...

+1
source

All Articles