From what I have read, command line arguments are only char* separated by space.
However feel compelled to ask if anyone knows of a way to get a pointer across to
an executable in any fashion?

The goal is to pass int *pointers for reasons of inter process communication across
different language apps.

As always I appreciate any input at all.

Recommended Answers

All 16 Replies

It doesn't make sense to pass memory pointers between address spaces.

I was under the impression that a child process shares memory space.

I pass an int pointer to one app at runtime via command line and the second app modifies it. Since both apps know the address they can share the variable between them.

At least that is how I thought it to work, I have certainly done this in another scripting language, passing a whole array of pointers.

I was under the impression that a child process shares memory space.

I pass an int pointer to one app at runtime via command line and the second app modifies it. Since both apps know the address they can share the variable between them.

At least that is how I thought it to work, I have certainly done this in another scripting language, passing a whole array of pointers.

No a child process(at least a child process in Linux) has its own address space so passing pointers from its parent will be meaningless.

Maybe you could pass along some specifics of what your trying to do.

Sorry, I'm working with windows > xp.

I have executable one.exe which I want to launch executable two.exe and pass it pointers to variables so they can communicate via them.

one.exe is written in a language that is not capable of carrying out the tasks C++
is, which is what two.exe is developed in, and I want two.exe to communicate these variables to one.exe via memory.

I don't think I'll have a problem with the communication as I have done it before as I said, except that time the loosely types scripting language was capable of handling the passed args as pointers. So with them and also armed with the process id of the parent exe is quite capable of sharing the passed pointers and there values.

I am pretty sure I can also achieve the same now, but not unless I can pass two.exe a pid and a pointer.

On the same subject of command line, I would also like to get the name of the parent process that spawned it, which I'm not having much luck with either :(

I am pretty sure I can also achieve the same now, but not unless I can pass two.exe a pid and a pointer.
(

Sounds like your accessing the parent or client address space via its pid and a pointer which sounds like your processes may not be sharing an address space.

Note: I have programmed on Windows system infrequently, so I'm ignorant of its functionality.

After a while researching this I think I am a little closer to my goal, but alas still not in the back of the net.

The key I think here is the type to receive the pointer, and I think my salvation might lie in intptr_t which if I read correctly is guaranteed to be able to hold a pointer.

Now the next part of my plan is a way to convert this char* representation of a pointer which will be passed in, to the actual numeric value of it.

Any ideas about that anyone?

here's hoping.

(edit)

I mean I can canvert it to its _W64 intptr_t value just by using a string streem but I'm not sure that will be enough to be used to point to
the correct address.

I think you should consider a few problems with your approach.

1. What if your pointer, points to dynamic memory that's been freed.
2. What if your pointer, points to memory on the stack that's been released.

>>Now the next part of my plan is a way to convert this char* representation of a pointer which will be passed in, to the actual numeric value of it.

Forget that idea, it won't, and can't work for reasons previously given. Pointers can not be passed between processes because each process has its own address space. Process A can not write into process B's address space, the operating system will prevent it by producing an Access Violation type of error.

Hi, and thank you for the replies.

please believe me, I am not being awkward, and I respect your views and answers, but I'm
having trouble understanding why I cannot share memory space.

I'm looking at an API function on MSDN WriteProcessMemory

Writes data to an area of memory in a specified process. The entire area to be written to must be accessible or the operation fails.

In a similar fashion we have ReadProcessMemory

(edit)

oops I hit submit instead of preview.

So if I pass to a process I spawn, the pid of the parent process and a pointer to an address in it, I cannot get my
head around why I would not be able to read and write to that address. I have looked inside my noobness but I'm
really having a tough time moving on here.

why don't you just try it and see for yourself if it works or not.

It is my intention.

But my problem is converting the input to pointer

I would be passing via command line a literal string which would resemble "0x00F1DC00"
and be the string representation of a pointer.

My woe is getting it to be a pointer to the address it represents. :(

As you might imagine this does not work and results in a crash

stringstream ss;
int *ptr;
ss << argv[1];
ss >> *ptr;

call strtol() to convert the string to an unsigned int, then assign that to a pointer of type unsigned char*.

unsigned char* ptr;
char* p;
unsigned int x = strtol(argv[1], &p, 16);
ptr = (unsigned char*)x;

or more simply

char* p;
unsigned char* ptr = (unsigned char*)strtol(argv[1],&p, 16);

Thanks, I'll have a go at that and also this.

stringstream ss;
int *iInt;
void *vVoid;
ss << argv[1];
ss >> vVoid;
iInt = (int*)vVoid;

you don't need vInt if all you are going to do is pass it to the Windows function. I don't know without testing if stringstream will work correctly like that, it may just return the char* instead of converting it to an integer.

Now that I think about it

stringstream ss;
int *iInt;
unsinged int x;
ss << argv[1];
ss >> x;
iInt = (int*)x;

Well I finally gave up walking down this road after not being able to
set sedebugprivileges while opening the process. :(

For the record I ended up using filemapping to the page file.

Thanks again for all of your time and help.

Suze.

(edit)

But I suppose I still managed to pass a pointer via command line so this matter is solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.